Dependencies:   MODSERIAL mbed Servo

Fork of juggler_mbed_position_control by Robert Katzschmann

Revision:
1:153ce28ac5ed
Parent:
0:1b69f8402884
Child:
2:b4aef96e0cd7
--- a/main.cpp	Mon Jan 09 00:33:14 2017 +0000
+++ b/main.cpp	Sun Jan 15 02:43:07 2017 +0000
@@ -1,6 +1,9 @@
 #include "mbed.h"
 #include "MODSERIAL/MODSERIAL.h"
 #include "SerialComm/SerialComm.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
 
 #define NUM_FLOATS 10
 
@@ -9,9 +12,10 @@
 
 // Initialize a pins t//o perform analog and digital output fucntions
 AnalogOut  aout(p18);
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
+PwmOut led1(LED1);
+// DigitalOut led2(LED2);
 //DigitalOut led3(LED3);
+Ticker aTimer;
 
 //Serial pc(USBTX, USBRX);
 float valueFloats[NUM_FLOATS];
@@ -19,32 +23,60 @@
 
 SerialComm serialComm(&pcSerial);
 
+bool readyToSendNext;
 
+void timerFcn()
+{
+    readyToSendNext = true;
+}
 
 
 int main(void)
 {
+    
+    float fReceived = 0;
+    float fToSend = 0;
+    pcSerial.baud(115200);
     pcSerial.printf("Start!\n");
-    //pcSerial.baud(19200);
-
+    
+    /*
     while(true) {
         if(serialComm.checkIfNewMessage()) {
             valueFloats[0] = serialComm.getFloat();//valueFloats, NUM_FLOATS);
             led2 = !led2;
-            //pcSerial.printf("Result: ");
-            //pcSerial.printf("%f", valueFloats[0]);
 
-            // change the voltage on the digital output pin by 0.1 * VCC
-            //  and print what the measured voltage should be (assuming VCC = 3.3v)
-            //for (float i = 0.0f; i < 1.0f; i += 0.1f) {
             voltage = (abs(valueFloats[0]) > 3.3f) ? 3.3f : abs(valueFloats[0]);
             aout = voltage / 3.3f;
-            //pc.printf("aout = %1.2f volts\n", aout.read() * 3.3f);
             // turn on the led if the voltage is greater than 0.5f * VCC
             led1 = (aout > 0.5f) ? 1 : 0; // voltage larger than 0.5*3.3
-            //led3 = !led3;
 
         }
         wait(1.0f);
+    }*/
+    //int i = 0;
+    readyToSendNext = false;
+    aTimer.attach(&timerFcn, 0.002); // the address of the function to be attached (flip) and the interval (2 seconds)
+
+    while(1) {
+        if(serialComm.checkIfNewMessage()) 
+        {
+            fReceived = serialComm.getFloat();
+        }
+//        
+        if(readyToSendNext == true)
+        {
+            fToSend = fReceived; // / 3.3f;
+            //led1 = fToSend; // voltage larger than 0.5*3.3
+            
+            //i++;
+            // led1 = (ftemp > 0.5f) ? !led1 : 0; // voltage larger than 0.5*3.3
+            serialComm.sendFloat(fToSend); // write it back
+            readyToSendNext = false;    
+        }
+        
+        // float testFloat = 0.1*i;
+        
+             
+        //wait(0.01f);
     }
 }
\ No newline at end of file