Start van regelaar

Dependencies:   Encoder MODSERIAL mbed

Dependents:   K9motoraansturing_copy lichtpoortjes

Revision:
0:7bc93f851767
Child:
1:9d05c0236c7e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 08 21:57:18 2013 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#include "encoder.h"
+#include "MODSERIAL.h"
+
+/** Coerce -> float in, and coerce if less than min, or larger than max **/
+void coerce(float * in, float min, float max);
+AnalogIn potmeter(PTC2);
+volatile bool looptimerflag;
+
+
+void setlooptimerflag(void)
+{
+    looptimerflag = true;
+}
+
+
+int main() {
+    Encoder motor1(PTD0,PTC9);
+    MODSERIAL pc(USBTX,USBRX);
+    PwmOut pwm_motor(PTA12);
+    DigitalOut motordir(PTD3);
+    float setpoint;
+    float new_pwm;
+    pc.baud(115200);
+    Ticker looptimer;
+    looptimer.attach(setlooptimerflag,0.01);  
+    pc.printf("bla"); 
+    while(1) {
+        while(!looptimerflag);
+        looptimerflag = false;
+        setpoint = (potmeter.read())*2000;
+        pc.printf("s: %f, %d \n\r", setpoint, motor1.getPosition());
+        if(motor1.getPosition() > setpoint)
+            new_pwm = 0.5;
+        else
+            new_pwm = -0.5;
+
+        if(new_pwm > 0)
+            motordir = 0;
+        else
+            motordir = 1;  
+        pwm_motor.write(abs(new_pwm));
+    }
+}
+
+
+//coerces value 'in' to min or max when exceeding those values
+//if you'd like to understand the statement below take a google for
+//'ternary operators'.
+void coerce(float * in, float min, float max)
+{
+    *in > min ? *in < max? : *in = max: *in = min;
+}
+
+
+