Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: motordrivertestprogram Initialmbedrobotprogram mbedrobot ipod ... more
Revision 2:2dc873322032, committed 2010-11-19
- Comitter:
- littlexc
- Date:
- Fri Nov 19 12:32:06 2010 +0000
- Parent:
- 1:3da7302dc9ae
- Child:
- 3:8822f4955035
- Commit message:
- allows more control over the motordriver. returns duty on pmw line
Changed in this revision
| motordriver.cpp | Show annotated file Show diff for this revision Revisions of this file |
| motordriver.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/motordriver.cpp Thu Nov 11 15:32:55 2010 +0000
+++ b/motordriver.cpp Fri Nov 19 12:32:06 2010 +0000
@@ -46,30 +46,36 @@
sign = 0;//i.e nothing.
}
-void Motor::speed(float speed) {
+float Motor::speed(float speed) {
+ float temp = 0;
if (sign == 0) {
_fwd = (speed > 0.0);
_rev = (speed < 0.0);
- _pwm = abs(speed);
+ temp = abs(speed);
+ _pwm = temp;
} else if (sign == 1) {
if (speed < 0) {
_fwd = (speed > 0.0);
_rev = (speed < 0.0);
_pwm = 0;
+ temp = 0;
} else {
_fwd = (speed > 0.0);
_rev = (speed < 0.0);
- _pwm = abs(speed);
+ temp = abs(speed);
+ _pwm = temp;
}
} else if (sign == -1) {
if (speed > 0) {
_fwd = (speed > 0.0);
_rev = (speed < 0.0);
_pwm = 0;
+ temp = 0;
} else {
_fwd = (speed > 0.0);
_rev = (speed < 0.0);
- _pwm = abs(speed);
+ temp = abs(speed);
+ _pwm = temp;
}
}
if (speed > 0)
@@ -79,7 +85,7 @@
} else if (speed == 0) {
sign = 0;
}
-
+ return temp;
}
// (additions)
void Motor::coast(void) {
@@ -89,14 +95,15 @@
sign = 0;
}
-void Motor::stop(void) {
+float Motor::stop(float duty) {
if (Brakeable == 1) {
_fwd = 1;
_rev = 1;
- _pwm = 0.5;
+ _pwm = duty;
sign = 0;
+ return duty;
} else
- return;
+ return 0;
}
/*
--- a/motordriver.h Thu Nov 11 15:32:55 2010 +0000
+++ b/motordriver.h Fri Nov 19 12:32:06 2010 +0000
@@ -42,35 +42,38 @@
* @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed
* @param fwd A DigitalOut, set high when the motor should go forward
* @param rev A DigitalOut, set high when the motor should go backwards
-* @param set if the motor driver is able to do braking 0 false 1 true. (addition)
+* @param set if the motor driver is able to do braking 0 false 1 true.
*/
Motor(PinName pwm, PinName fwd, PinName rev, int brakeable);
-/** Set the speed of the motor (addition)
+/** Set the speed of the motor
*
-* @param speed The speed of the motor as a normalised value between -1.0 and 1.0
+* @param speed The speed of the motor as a normalised value between -1.0 and 1.0.
+* @return the applied speed to the motor after checking to ensure motor doesn't switch from forward to reverse without stopping.
*/
- void speed(float speed);
+ float speed(float speed);
/** Set the the motor to coast
*
-* @param void motor coasts until another instruction is recived
+* @param void
+* @return motor coasts until another instruction is recived.
*/
void coast(void);
-/** Set the motor to dynamicaly brake (addition)
+/** Set the motor to dynamicaly brake
*
-* @param void motor dynamicaly brakes until another instruction is recived
+* @param float 0 - 1.0 provides some control over how hard the motor brakes.
+* @return duty applied to motor driver. -1 is error, motor driver can't brake.
*/
- void stop(void);
+ float stop(float duty);
protected:
PwmOut _pwm;
DigitalOut _fwd;
DigitalOut _rev;
- int Brakeable; // (addition)
+ int Brakeable; // cna the motor driver break
int sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
};
DC Motor