... | ... | @@ -250,16 +250,80 @@ The car behaves as expected. It starts driving according to the light readings, |
|
|
|
|
|
## Exercise 4: Add a third motor to turn the light sensor
|
|
|
|
|
|
#### Task:
|
|
|
Implement the Follow behavior using the motor and the light sensor so the car does not have to turn in order to make left and right reading.
|
|
|
|
|
|
#### Plan:
|
|
|
|
|
|
For this exercise we mounted the light sensor so it can be turned by a horizontal motor (Fig.11)
|
|
|
|
|
|
![IMG_8279](http://gitlab.au.dk/uploads/group-22/lego/2d20ae580c/IMG_8279.jpg)
|
|
|
|
|
|
##### Fig.11 - The light sensor is mounted with a horizontal motor.
|
|
|
|
|
|
#### Task:
|
|
|
Implement the Follow behavior using the motor and the light sensor so the car does not have to turn in order to make left and right reading.
|
|
|
For the motor to work we used a RegulatedMotor to control the light sensor (Fig.12). This is used to turn the motor to a specific point in angle:
|
|
|
|
|
|
#### Plan:
|
|
|
```
|
|
|
private RegulatedMotor sensorMotor = new NXTRegulatedMotor(MotorPort.A);
|
|
|
```
|
|
|
##### Fig.12 - Regulated motor.
|
|
|
|
|
|
|
|
|
To implement the intented behavior we modified the code as follows (Fig.13):
|
|
|
|
|
|
```
|
|
|
// Follow light as long as the light level is above the threshold
|
|
|
while ( frontLight > lightThreshold )
|
|
|
{
|
|
|
|
|
|
// Get the light to the left
|
|
|
// Turn light sensor to the left
|
|
|
sensorMotor.rotateTo(45);
|
|
|
leftLight = light.getLightValue();
|
|
|
LCD.drawString("LeftLightVal: ", 0, 5);
|
|
|
LCD.drawInt(leftLight, 13, 5);
|
|
|
|
|
|
// Get the light to the right
|
|
|
// Turn light sensor to the right
|
|
|
sensorMotor.rotateTo(-45);
|
|
|
rightLight = light.getLightValue();
|
|
|
LCD.drawString("RightLightVal: ", 0, 6);
|
|
|
LCD.drawInt(rightLight, 13, 6);
|
|
|
|
|
|
// Turn back straight
|
|
|
sensorMotor.rotateTo(0);
|
|
|
|
|
|
// Follow light for a while
|
|
|
delta = leftLight-rightLight;
|
|
|
car.forward(power-delta, power+delta);
|
|
|
Delay.msDelay(ms);
|
|
|
|
|
|
// Go strait again
|
|
|
car.forward(power, power);
|
|
|
|
|
|
frontLight = light.getLightValue();
|
|
|
}
|
|
|
```
|
|
|
##### Fig.13 - Code used to turn the light sensor
|
|
|
|
|
|
By using this code the robot should turn the light sensor in directions left and right while conducting readings. This means that the car does not have to change direction itself unless a higher light reading is made.
|
|
|
|
|
|
In order to make the light sensor and the motor work accordingly, we implemented the following code (Fig.14):
|
|
|
|
|
|
```
|
|
|
public Follow(SharedCar car)
|
|
|
{
|
|
|
this.car = car;
|
|
|
lightThreshold = light.getLightValue();
|
|
|
sensorMotor.resetTachoCount();
|
|
|
|
|
|
}
|
|
|
```
|
|
|
##### Fig.14 - Resetting the tacho count
|
|
|
|
|
|
The code makes sure that the tacho count is reset once the program is executed. This means that the motor will rotate the sensor from this starting position.
|
|
|
|
|
|
#### Result:
|
|
|
|
|
|
|
|
|
|
... | ... | |