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

Set up the first switch (right side)

The program will check to see if the turret has swiveled too far away from the center. It will make use of the compare switch yet again, this time comparing the number of degrees on motor D to the threshold value that would indicate that it had rotated too far. The mode on the switch block should be set to Motor Rotation | Compare | Degrees.

When the turret swivels to the right of center, the degree count on motor D will be positive. When the turret swivels left, the degree count will be negative. Our robot will need to individually check to see if the turret is too far in each direction, so it will first check to see whether the turret is too far to the right. Therefore, this first switch will only be concerned with the positive direction. We will soon program a second switch that checks in the negative direction.

Set the compare type on the switch to > (type 2). The threshold value that we can say is too far is 250 degrees. Enter 250 into the second input of the switch:

Even though the maximum number of degrees we allow on motor D is  250 , this only translates to just under 90 degrees of rotation on the turret. This is because of the gear reduction on the turret, which we discussed earlier in this chapter. One degree of rotation on motor D results in a fraction of a degree's worth of rotation on the turret.

This switch will check to see if motor D has rotated more than 250 degrees to the right of center. If this ends up being true, then the tank will need to make an adjustment to its steering and its turret. We will now program in the code that will make that adjustment.