Mario Bambagini / Mbed 2 deprecated caterpillar

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Sat Jun 14 21:56:12 2014 +0000
Commit message:
example to test motion boards

Changed in this revision

dc_motor.cpp Show annotated file Show diff for this revision Revisions of this file
dc_motor.hpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dc_motor.cpp	Sat Jun 14 21:56:12 2014 +0000
@@ -0,0 +1,14 @@
+#include "dc_motor.hpp"
+
+DC_motor::DC_motor (PinName ppwm, PinName pdir): pwm(ppwm), dir(pdir)
+{
+    pwm.period(0.001);
+    pwm = 0;
+    dir = 0;
+}
+
+void DC_motor::speed (float speed)
+{
+    dir = speed>0.0;
+    pwm = abs(speed);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dc_motor.hpp	Sat Jun 14 21:56:12 2014 +0000
@@ -0,0 +1,19 @@
+#ifndef DC_MOTOR
+#define DC_MOTOR
+
+#include "mbed.h"
+
+class DC_motor
+{
+    PwmOut pwm;
+    DigitalOut dir;
+
+public:
+
+    DC_motor (PinName ppwm, PinName pdir);
+
+    void speed(float speed);
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Jun 14 21:56:12 2014 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "dc_motor.hpp"
+
+int main ()
+{
+    AnalogIn vert(p20);
+    AnalogIn horz(p19);
+    DC_motor m_sx(p21, p22);
+    DC_motor m_dx(p23, p24);
+
+    while(1) {
+        wait(0.2);
+        float v = vert.read();
+        float h = horz.read();
+        float speed_sx = (v-0.5)*2.0;
+        if (h>0.5)
+            speed_sx *= (1.0-(h-0.5)*2.0);
+        float speed_dx = (v-0.5)*2.0;
+        if (h<0.5)
+            speed_dx *= ((h+0.5)*2.0-1.0);
+        m_sx.speed(speed_sx);
+        m_dx.speed(speed_dx);
+//        printf("VERT(%f), HORZ(%f), sx(%f), dx(%f)\n\r",
+//                vert.read(), horz.read(), speed_sx, speed_dx);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Jun 14 21:56:12 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/024bf7f99721
\ No newline at end of file