Aiming the turret
The robot will now use the second infrared sensor, IR 2, to aim the turret in the direction of the beacon. We will use proportional logic to control the aiming of the turret, just as we did for the driving. The heading value measured by IR 2 is multiplied by a k value and will set the target power for motor D, the motor responsible for rotating the turret.
We are going to start this new segment of programming directly after the proportional beacon follower we just wrote. Make sure the new code blocks are still within the switch block. Add a new infrared sensor block with its mode set to Measure | Beacon. This time, the port should be set to 2.
Add a math block set to multiply after the new IR sensor block. As before, this block will multiply the heading measurement from the IR sensor by a k value. The suggested k value for the turret turntable is 1.6.
The medium motor in port D controls the swivel of the turntable, so the third block to add is a medium motor block. Be sure to set the port to D and the mode to On.
Finally, plug the heading output from the sensor block into the a input of the math block. Then, plug the result of the math block into the power input of the medium motor block:
Now we have programmed the turret to swivel according to the heading value measured by IR 2 using a proportional algorithm.