Hi Derek,
No worries about the name, I get called Dan all the time since my o's tend to look like a's to most people.
I took another look at Motor.cpp and noticed a few things:
You are realy only using the pwm out of the object and trying to override the direction bits, which get set back when you set speed.
Motor expects to set a forward or a reverse bit for direction, both on for break, of both off for coast. The controls for your driver are realy direction and break.
The pwm output gets set to the absolute value of the value passed, so always 0.0 to 1.0, and the sign is used to set the forward and reverse bits.
I suggest you use simple PwmOut()'s for the speeds and the direction bits you already have defined.
Change the range for speed to 0.0 to 1.0 for both directions.
If that works, you might want to restructure the code a bit to have a single RampUp and RampDown function since the forward and reverse routines will be almost identical. Set the direction bits, call ramp up, wait, call ramp down, change direction and repeat.
Cheers,
Don
Hi everyone, I'm trying to run two motors up to full speed in one direction then reverse them using an old Magnevation board I have. The problem I have is at full speed in one direction the output for speed direction changes hence not a nice result! The pwm voltage output ramps up nicely from 0v to +3.2v then ramps down again to 0v. Where am I going wrong in the code please??
// Sweep the motor speed from full-speed reverse (-1.0) to full speed forwards (1.0) // Thank you for the code :-) #include "mbed.h" #include "Motor.h" // For the Magnevation dual PWM module:- // Channel 1: p5 remains low on pin40(Magnevation) which is the brake pin to allow the motor to run. // When p6 is toggled between high/low motor direction is reversed. // Channel 2: p7 remains low on pin38(Magnevation) which is the brake pin to allow the motor to run. // When p8 is toggled between high/low motor direction is reversed. // p5 and p7 outputs are not connected - pin40 and pin38 tied to Gnd (Magnevation) Motor RightWheel(p23, p6, p5); // pwm, fwd/rev, brake Motor LeftWheel(p25, p8, p7); // pwm, fwd/rev, brake DigitalOut DirectionR=p6; DigitalOut DirectionL=p8; void DriveForward() { for (float s= -1.0; s < 1.0 ; s += 0.01) { DirectionR=1;//Set the direction Right motor DirectionL=1;//Set the direction Left motor RightWheel.speed(s); LeftWheel.speed(s); wait(0.2); } } void DriveReverse() { for (float s= -1.0; s < 1.0 ; s += 0.01) { DirectionR=0;//Set the direction Right motor DirectionL=0;//Set the direction Left motor RightWheel.speed(s); LeftWheel.speed(s); wait(0.2); } } void ZeroSpeedForward() { for (float s = 1.0; s = 0.0; s -= 0.01) { DirectionR=1;//Set the direction Right motor DirectionL=1;//Set the direction Left motor RightWheel.speed(s); LeftWheel.speed(s); wait(0.2); } } void ZeroSpeedReverse() { for (float s = -1.0; s = 0.0; s += 0.01) { DirectionR=0;//Set the direction Right motor DirectionL=0;//Set the direction Left motor RightWheel.speed(s); LeftWheel.speed(s); wait(0.2); } } int main() { while (1) { DriveForward(); wait(5); ZeroSpeedForward();//Before changing direction best to zero the speed on the motors! wait(5); DriveReverse(); wait(5); ZeroSpeedReverse(); } }