4 - Overview of the library (Part 2)

 

Motor.hpp

A motor is an abstract extension of an output pin that defines some methods that apply to all motors such as:-

SetSpeed(int speed); // Can take a value from MIN_SPEED to MAX_SPEED

SetSpeed(int speed, bool forwards); // The first parameter specifies a speed from 0 to MAX_SPEED and the second parameter is 'true' for forwards, or 'false' for backwards.

GetSpeed(); // will return the last value of speed that was set (ie its not the true speed the motor is running at - you would need an encoder for that!)

 

The Disconnect(); method is like disconnecting the wires from the motor so it will cruise to a halt and, if on a hill, may then start rolling backwards. Whereas SetSpeed(0); will cause the motor to brake and hold on a hill. Motors can be reconnected at a later date with the Reconnect() method and you can test if a motor is connected via the IsConnected() method.

 

Servo.hpp

This extends the Motor class and implements it for a modified Servo as per the $50 Robot Tutorial. A new servo on port D2 can be created via:-

SERVO g_servoLeft( 'D', 2 , &g_timer, true, 1500, 300);

Note that you pass a reference to the global timer because the Servo code makes sure that at least 20ms has elapsed since you last sent it a command. If your motor is running in the wrong direction then change the 'true' parameter to 'false' - you don't need to change anything else. The last two parameters specify the pulse width to center the servo (ie 1.5ms) and how much the pulse width varies on either side of that value. NB The last 3 parameters are optional and if they are missing then they will be assumed to be false,1500,500 respectively.

 

ServoPWM.hpp

This class is for controlling a modified servo motor by using PWM. Otherwise it is identical to Servo.hpp except that it doesn't need the timer to be passed on creation.