Gerhard Berman / Mbed 2 deprecated prog_pract3_v2

Dependencies:   MODSERIAL QEI mbed HIDScope

Fork of prog_pract3 by Marieke M

Revision:
1:6be8bcde9f05
Parent:
0:8f8157690923
Child:
2:ee821b9bf42b
--- a/main.cpp	Mon Oct 10 10:06:57 2016 +0000
+++ b/main.cpp	Mon Oct 10 12:07:25 2016 +0000
@@ -2,30 +2,34 @@
 #include <math.h>
 #include "MODSERIAL.h"
 
-AnalogIn potMeterIn(A0);
+AnalogIn potMeterIn(PTB2);
 DigitalIn button1(D7);
 DigitalOut motor1DirectionPin(D4);
 PwmOut motor1MagnitudePin(D5);
 
 Serial pc(USBTX,USBRX);
-Ticker MeasureTicker;
+Ticker MeasureTicker, TimeTracker;
+
+float referenceVelocity = 0;
 
-volatile float referenceVelocity = 0;  // in rad/s
+volatile bool MeasureTicker_go=false, TimeTracker_go=false;
+
+void MeasureTicker_act(){MeasureTicker_go=true;}; // Activates go-flags
+void TimeTracker_act(){TimeTracker_go=true;};
 
 float GetReferenceVelocity()
 {
     // Returns reference velocity in rad/s. 
     // Positive value means clockwise rotation.
-    const float maxVelocity=8.4; // in rad/s of course!
-    
+    const float maxVelocity = 8.4; // in rad/s of course!
     if (button1)
     {
         // Clockwise rotation
-        referenceVelocity = potMeterIn * maxVelocity;
+        referenceVelocity = potMeterIn.read() * maxVelocity;
     } else
     {
         // Counterclockwise rotation
-        referenceVelocity = -1*potMeterIn * maxVelocity;
+        referenceVelocity = -1*potMeterIn.read() * maxVelocity;
     }
     return referenceVelocity;
 }
@@ -55,19 +59,34 @@
     // This function measures the potmeter position, extracts a
     // reference velocity from it, and controls the motor with 
     // a simple FeedForward controller. Call this from a Ticker.
-    volatile float referenceVelocity = GetReferenceVelocity();
+    float referenceVelocity = GetReferenceVelocity();
     float motorValue = FeedForwardControl(referenceVelocity);
     SetMotor1(motorValue);
 }
 
+void TimeTrackerF(){
+     wait(1);   
+     pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);
+}
 
 int main()
 {
- 
  //Initialize
- MeasureTicker.attach(&MeasureAndControl, 1.0/100); 
- pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);
+ MeasureTicker.attach(&MeasureTicker_act, 0.1f); 
+ TimeTracker.attach(&TimeTracker_act, 1.0f);
  pc.baud(115200);  
- while(true)
- {}
-}
\ No newline at end of file
+ pc.printf("Reference velocity: %f rad/s \r\n", referenceVelocity);  
+ while(1)
+    {
+        if (MeasureTicker_go){
+            MeasureTicker_go=false;
+            MeasureAndControl();
+        }
+        if (TimeTracker_go){
+            TimeTracker_go=false;
+            TimeTrackerF();
+        }
+    }
+}
+
+