I'm writing a module in Swordfish BASIC for the Mongoose robot kit. I've looked at the now discontinued Micro-Bot's PICBasic Pros "MotorDir" command for controlling a pair of servo motors.
Here's what I've come up with.
Code:
MotorLR(lm,rm,offset)
where:
lm = +/-1023 (sets left motor direction & 10bit PWM directly)
rm = +/-1023 (sets right motor direction & 10bit PWM directly)
offset = +/- 127 (subtracts from lm + and rm -) corrects motor difference
Also I'm trying to figure out a syntax for the wheel rotation sensors. Two integer variables +/- 32767 (approx +/-50m on the Mongoose) will be automatically updated using Swordfish's interrupt on change module.
When working with decoding a Vex xmitter and sending the info to a Tamiya dual gearbox motor, pwm seemed difficult to achieve. Ended up with just going on or off. The 10 bit pwm makes for a great led dimmers, but is it practical for a motor?
The offset is a good idea, because when my batteries get low its not going straight at all, although the vex xmitter can adjust for that. The Vex uses up and down buttons and a counter no doubt to achieve that.
When working with decoding a Vex xmitter and sending the info to a Tamiya dual gearbox motor, pwm seemed difficult to achieve. Ended up with just going on or off. The 10 bit pwm makes for a great led dimmers, but is it practical for a motor?
The offset is a good idea, because when my batteries get low its not going straight at all, although the vex xmitter can adjust for that. The Vex uses up and down buttons and a counter no doubt to achieve that.
I found this easy to use. I have the C code for the moongose H-Bridge working. This is from memory. Should have just pulled up the code...
EDIT: Move could also watch the rotation sensors and adjust PWM to ensure that each wheel moves the correct percentage of the other. The offset could be learned/calculated and the user would need not bother with it.
Code:
// command to move robot
void move(
int16 duration, // duration of command, IIRC in 10ths of a second
byte montion, // how to set the H-Bridge.
byte powerLeft, // % power for left motor
byte powerRight) // % power for right motor
The first 4 bits of the motion parameter apply to the left motor h-bridge. The last 4 the right. The power settings specify the PWM setting.
Where motion can be one of a set of defines that look like
I've setup the 18F2525 to run at 4MHz with a period of ~2KHz this allows the full 1024 step duty and the motors are happy with it.
This was easy in either assembler, BASIC and C just set the direction PORTB 0-3 write to a few SFRs and the motors run.
As for continous servos they use the same 1ms to 2ms to control speed and direction, 1.5 is stop (more or less sometimes they stutter)