Hey everyone!
So I’m learning about the BBB by using the Bad to the Bone book, so this question is directed specifically at anyone who has read or used this book. I was doing the chapter exercises for Chapter 2, and I was a little confused.
The questions are as follows:
14. Develop a series of functions to turn the Blinky 602A right and left. The motor duty cycle
and the motor on time should be passed to the function as a variable.
15. Equip the motor with a fourth IR sensor that looks down to the maze floor for “land mines.”
A land mine consists of a paper strip placed in the maze floor that obstructs a portion of
the maze. If a land mine is detected, the robot must deactivate it by rotating three times and
flashing a large LED while rotating
The original code given was the following:
`
//***********************************************************************
var b = require(’bonescript’);
//Old bonescript defines ’bone’ globally
var pins = (typeof bone != ’undefined’) ? bone : b.bone.pins;
var left_IR_sensor = pins.P9_39; //analog input for left IR sensor
var center_IR_sensor = pins.P9_40; //analog input for center IR sensor
var right_IR_sensor = pins.P9_37; //analog input for right IR sensor
var left_motor_pin = pins.P9_14; //PWM pin for left motor
var right_motor_pin = pins.P9_16; //PWM pin for right motor
var left_sensor_value;
var center_sensor_value;
var right_sensor_value;
b.pinMode(left_motor_pin, b.OUTPUT); //left motor pin
b.pinMode(right_motor_pin, b.OUTPUT);//right motor pin
while(1)
{
//read analog output from IR sensors
//normalized value ranges from 0…1
left_sensor_value = b.analogRead(left_IR_sensor);
center_sensor_value = b.analogRead(center_IR_sensor);
right_sensor_value = b.analogRead(right_IR_sensor);
//assumes desired threshold at
//1.25 VDC with max value of 1.75 VDC
if((left_sensor_value > 0.714)&&
(center_sensor_value <= 0.714)&&
(right_sensor_value > 0.714))
{ //robot continues straight ahead
b.analogWrite(left_motor_pin, 0.7);
b.analogWrite(right_motor_pin, 0.7);
}
else if
{
:
:
:
}
}
//***********************************************************************
`
I understand this code and it makes sense. For exercise #13, we had to actually fill in the rest of the if-else loop which I did below:
`
if((left_sensor_value > 0.714) && (center_sensor_value <= 0.714) && (right_sensor_value > 0.714))
{
//robot continues straight ahead
b.analogWrite(left_motor_pin, 0.7);
b.analogWrite(right_motor_pin, 0.7);
}
else if((left_sensor_value > 0.714) && (center_sensor_value > 0.714) && (right_sensor_value <= 0.714))
{
//The right side is clear, but left and front are obstructed.
//So turn right by turning the left wheel but not the right wheel.
b.analogWrite(left_motor_pin, 0.7);
b.analogWrite(right_motor_pin, 0);
}
else
{
//Turn left
//Default action seems to be to turn left even if the other ways
//are also free.
b.analogWrite(left_motor_pin, 0);
b.analogWrite(right_motor_pin, 0.7);
}
`
Anyways, according to the book, the second operand to the analogWrite() function is the duty cycle, so that is clear. But I don’t know what the motor on time is controlled by. Does anyone see, because it is not obvious to me at all.
I can’t write the function if I don’t know what the variable is or how it is being used. Thanks for reading and for any helpful replies. If you need more information, I’ll be happy to give them.