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.
Diff: Motor.cpp
- Revision:
- 0:9c44946754b6
- Child:
- 1:809c0963d521
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.cpp Sun Mar 10 15:45:59 2019 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+#include "Motor.h"
+
+Motor::Motor(PinName _pwm, PinName _dire, int freq) : pwm(_pwm), dire(_dire)
+{
+ pwm_mode = SMB;
+ abs_max_output = 1.0;
+ frequency = 1.0 / freq;
+}
+
+void Motor::period()
+{
+ pwm.period(frequency);
+}
+
+void Motor::drive(float output)
+{
+ switch(pwm_mode)
+ {
+ case SMB:
+ if (abs(output) > abs_max_output)
+ {
+ pwm = 0;
+ dire = 0;
+ }
+ else if (output > abs_max_output * 0.05)
+ {
+ pwm = output * abs_max_output;
+ dire = 0;
+ }
+ else if (output < -abs_max_output * 0.05)
+ {
+ pwm = -output * abs_max_output;
+ dire = 1;
+ }
+ else
+ {
+ pwm = 0;
+ dire = 0;
+ }
+ break;
+ case LAP:
+ if(abs(output) > abs_max_output)
+ {
+ pwm = 0;
+ dire = 0;
+ }
+ else if(output > abs_max_output * 0.05)
+ {
+ pwm = 0.5 + (output * abs_max_output / 2);
+ dire = 1;
+ }
+ else if(output < -abs_max_output * 0.05)
+ {
+ pwm = 0.5 - (output * abs_max_output / 2);
+ dire = 1;
+ }
+ else
+ {
+ pwm = 0;
+ dire = 0;
+ }
+ break;
+ }
+}
\ No newline at end of file