Building Smart LEGO MINDSTORMS EV3 Robots
上QQ阅读APP看书,第一时间看更新

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 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 value. The suggested value for the turret turntable is 1.6.

Although the  k value for the turret ( 1.6) is smaller than the  value used for steering ( 5), the turntable responds faster than the steering. It is important to consider the mechanism attached to each motor when considering  values. Despite having a smaller  value, the turntable rotates faster because the hardware between it and the motor controlling it makes it respond more quickly than the steering on the drive motors.

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.