Basic Mid-Level control for the rebuilt MorphGI control unit, using PWM to communicate with the low level controllers.

Dependencies:   ros_lib_kinetic

Revision:
9:cd3607ba5643
Parent:
8:d6657767a182
Child:
10:1b6daba32452
--- a/main.cpp	Tue Aug 07 09:18:15 2018 +0000
+++ b/main.cpp	Tue Aug 07 14:15:37 2018 +0000
@@ -55,10 +55,7 @@
 Timer timer;
 Ticker PathCalculationTicker;
 
-bool isDataReady[N_CHANNELS]; // flag to indicate path data is ready for transmission to low level.
-
-/*double dblGlobalTest;
-int intGlobalTest;*/
+bool isDataReady[N_CHANNELS] = { 0 }; // flag to indicate path data is ready for transmission to low level.
 
 void startPathPlan() { // Plan a new linear path after receiving new target data
     semPathPlan.release(); // Uses threadReceiveAndReplan which is below normal priority to ensure consistent transmission to LL
@@ -78,15 +75,11 @@
     }
     
     int ii;
-    unsigned char recv_buffer[1024];
     struct msg_format input; //hlcomms.msg_format
-    char send_buffer[65];
-    char * message_to_send;
     
     while( true ) {
         // RECEIVE MESSAGE
-        memset(recv_buffer, 0, sizeof(recv_buffer));
-        error_code = hlcomms.interfaces.clt_sock.recv(recv_buffer, 1024); // Blocks until data is received
+        error_code = hlcomms.receive_message();
         if( error_code == NSAPI_ERROR_NO_CONNECTION ) { // -3004
             printf("Client disconnected.\n\r");
             hlcomms.close_server();
@@ -99,7 +92,7 @@
             return;
         }
         //printf("Message received.\r\n");
-        input = hlcomms.consume_message( recv_buffer );
+        input = hlcomms.process_message();
         
         // PROCESS INPUT
         double dblTargetChambLen_mm[N_CHANNELS]; // The currenly assigned final target position (actuator will reach this at end of path)
@@ -182,10 +175,8 @@
         
         printf("Sending message...\r\n");
         // SEND MESSAGE
-        memset(send_buffer, 0, sizeof(send_buffer));
-        _snprintf(send_buffer,64,"%f",dblMaxRecalculatedTime);
-        message_to_send = send_buffer;
-        error_code = hlcomms.interfaces.clt_sock.send(message_to_send, strlen(message_to_send));
+        hlcomms.make_message(&dblMaxRecalculatedTime);
+        error_code = hlcomms.send_message();
         if( error_code < 0 ) {
             printf("Error %i. Could not send data over the TCP socket. "
                 "Perhaps the server socket is not bound or not set to listen for connections? "
@@ -284,6 +275,7 @@
        // llcomms.ThreadID[channel] = queue.call(mbed::Callback<void()>(&llcomms,&LLComms::SendReceiveData),channel,intDemandPos_Tx,&isDataReady); // Schedule transmission
         //llcomms.ThreadID[channel] = queue.call(&llcomms,*llcomms.SendReceiveData,channel,intDemandPos_Tx,&isDataReady); // Schedule transmission
         llcomms.ThreadID[channel] = queue.call(&SendReceiveData,channel,intDemandPos_Tx,&isDataReady); // Schedule transmission
+        //llcomms.ThreadID[channel] = queue.call(&llcomms,&LLComms::dSendReceiveData,channel,intDemandPos_Tx,&isDataReady); // Schedule transmission
         //callback(&low_pass1, &LowPass::step)
     }
 }
@@ -311,26 +303,12 @@
 void fall6(void) { common_fall_handler(6); }
 void fall7(void) { common_fall_handler(7); }
 
-int main() {
-    int ii;
-    // Initialise relevant variables
-    for(ii = 0; ii<N_CHANNELS; ii++) {
-        // All chip selects in off state
-        *llcomms.cs_LL[ii] = 1;
-        *llcomms.cs_ADC[ii] = 1;
-        // Data ready flags set to not ready
-        isDataReady[ii] = 0;
-    }    
-    
-    llcomms.pinReset = 1; // Initialise reset pin to not reset the controllers.
-    wait(0.25);
-    llcomms.pinReset=0; // Reset controllers to be safe
-    wait(0.25);
-    llcomms.pinReset = 1; // Ready to go
-    
+int main() {    
     pc.baud(BAUD_RATE);
     printf("Hi, there! I'll be your mid-level controller for today.\r\n");
     wait(3);
+    
+    llcomms.reset();
     t.start(callback(&queue, &EventQueue::dispatch_forever)); // Start the event queue
     
     // Set up rise interrupts MIGHT NOT NEED TO BE POINTERS
@@ -344,6 +322,7 @@
     llcomms.pinGate7.rise(&rise7);
     // Set up fall interrupts MIGHT NOT NEED TO BE POINTERS
     llcomms.pinGate0.fall(&fall0);
+    //llcomms.pinGate0.fall(&llcomms,&LLComms::dfall0);
     llcomms.pinGate1.fall(&fall1);
     llcomms.pinGate2.fall(&fall2);
     llcomms.pinGate3.fall(&fall3);