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

Dependencies:   ros_lib_kinetic

Committer:
WD40andTape
Date:
Mon Jan 28 21:24:48 2019 +0000
Revision:
26:7c59002c9cd7
Parent:
25:88e6cccde856
Child:
27:6853ee8ffefd
Working on updates to move trajectory planning to low level. Updated to consistently use mm.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WD40andTape 7:5b6a2cefbf3b 1 // STANDARD IMPORTS
WD40andTape 7:5b6a2cefbf3b 2 #include "math.h"
WD40andTape 7:5b6a2cefbf3b 3 // MBED IMPORTS
dofydoink 0:607bc887b6e0 4 #include "mbed.h"
WD40andTape 6:f0a18e28a322 5 #include "mbed_events.h"
WD40andTape 7:5b6a2cefbf3b 6 // CUSTOM IMPORTS
dofydoink 12:595ed862e52f 7 #include "MLSettings.h"
WD40andTape 7:5b6a2cefbf3b 8 #include "HLComms.h"
WD40andTape 8:d6657767a182 9 #include "LLComms.h"
dofydoink 0:607bc887b6e0 10
dofydoink 12:595ed862e52f 11 // Maximum achievable mm path tolerance plus additional % tolerance
WD40andTape 26:7c59002c9cd7 12 //const float FLT_PATH_TOLERANCE_MM = MAX_SPEED_MMPS * (1/LL_DEMANDS_FREQ_HZ) * (1.0f+FLT_PERCENT_PATH_TOLERANCE);
dofydoink 0:607bc887b6e0 13
WD40andTape 21:0b10d8e615d1 14 // DEMAND VARIABLES
WD40andTape 26:7c59002c9cd7 15 double dblDemandSpeed_mmps[N_CHANNELS] = { 0.0 }; // The linear path velocity (not sent to actuator)
WD40andTape 26:7c59002c9cd7 16 double dblDemandPosition_mm[N_CHANNELS] = { 0.0 }; // The final target position for the actuator
dofydoink 11:7029367a1840 17
WD40andTape 7:5b6a2cefbf3b 18 Serial pc(USBTX, USBRX); // tx, rx for usb debugging
dofydoink 12:595ed862e52f 19 LLComms llcomms;
WD40andTape 16:1e2804a4e5bd 20 HLComms hlcomms(SERVER_PORT);
WD40andTape 3:c83291bf9fd2 21
WD40andTape 10:1b6daba32452 22 Thread threadLowLevelSPI(osPriorityRealtime);
WD40andTape 21:0b10d8e615d1 23 Thread threadSetDemands(osPriorityNormal);
WD40andTape 4:303584310071 24 Thread threadReceiveAndReplan(osPriorityBelowNormal);
WD40andTape 17:bbaf3e8440ad 25 Thread threadSensorFeedback(osPriorityBelowNormal);
dofydoink 0:607bc887b6e0 26
WD40andTape 4:303584310071 27 Mutex mutPathIn;
WD40andTape 21:0b10d8e615d1 28 Semaphore semLLcomms(1);
WD40andTape 17:bbaf3e8440ad 29 Semaphore semSensorData(1);
dofydoink 0:607bc887b6e0 30
WD40andTape 21:0b10d8e615d1 31 Ticker setDemandsTicker;
WD40andTape 17:bbaf3e8440ad 32 Ticker SendSensorDataTicker;
WD40andTape 17:bbaf3e8440ad 33
WD40andTape 17:bbaf3e8440ad 34
WD40andTape 17:bbaf3e8440ad 35 void sendSensorData() {
WD40andTape 17:bbaf3e8440ad 36 while( true ) {
WD40andTape 17:bbaf3e8440ad 37 semSensorData.wait();
WD40andTape 26:7c59002c9cd7 38 int error_code = hlcomms.send_sensor_message(llcomms.positionSensor_mm,llcomms.pressureSensor_bar);
WD40andTape 17:bbaf3e8440ad 39 /*if( error_code < 0 ) {
WD40andTape 17:bbaf3e8440ad 40 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 17:bbaf3e8440ad 41 "Perhaps the server socket is not bound or not set to listen for connections? "
WD40andTape 17:bbaf3e8440ad 42 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 17:bbaf3e8440ad 43 hlcomms.close_server();
WD40andTape 17:bbaf3e8440ad 44 return;
WD40andTape 17:bbaf3e8440ad 45 }*/
WD40andTape 17:bbaf3e8440ad 46 }
WD40andTape 17:bbaf3e8440ad 47 }
WD40andTape 17:bbaf3e8440ad 48
WD40andTape 17:bbaf3e8440ad 49 void signalSendSensorData() {
WD40andTape 17:bbaf3e8440ad 50 semSensorData.release();
WD40andTape 17:bbaf3e8440ad 51 }
dofydoink 0:607bc887b6e0 52
WD40andTape 7:5b6a2cefbf3b 53 // This function will be called when a new transmission is received from high level
WD40andTape 7:5b6a2cefbf3b 54 void ReceiveAndReplan() {
WD40andTape 6:f0a18e28a322 55
WD40andTape 6:f0a18e28a322 56 int error_code;
WD40andTape 7:5b6a2cefbf3b 57 error_code = hlcomms.setup_server();
WD40andTape 6:f0a18e28a322 58 if( error_code == -1 ) return;
WD40andTape 7:5b6a2cefbf3b 59 error_code = hlcomms.accept_connection();
WD40andTape 7:5b6a2cefbf3b 60 if( error_code == -1 ) {
WD40andTape 7:5b6a2cefbf3b 61 hlcomms.close_server();
WD40andTape 7:5b6a2cefbf3b 62 return;
WD40andTape 7:5b6a2cefbf3b 63 }
WD40andTape 6:f0a18e28a322 64
WD40andTape 22:82871f00f89d 65 SendSensorDataTicker.attach(&signalSendSensorData, 1/(float)SENSOR_FEEDBACK_HZ); // Set up planning thread to recur at fixed intervals
WD40andTape 17:bbaf3e8440ad 66
WD40andTape 7:5b6a2cefbf3b 67 struct msg_format input; //hlcomms.msg_format
WD40andTape 4:303584310071 68
WD40andTape 6:f0a18e28a322 69 while( true ) {
WD40andTape 6:f0a18e28a322 70 // RECEIVE MESSAGE
WD40andTape 9:cd3607ba5643 71 error_code = hlcomms.receive_message();
WD40andTape 6:f0a18e28a322 72 if( error_code == NSAPI_ERROR_NO_CONNECTION ) { // -3004
dofydoink 12:595ed862e52f 73 if(IS_PRINT_OUTPUT) printf("Client disconnected.\n\r");
WD40andTape 7:5b6a2cefbf3b 74 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 75 return;
WD40andTape 6:f0a18e28a322 76 } else if( error_code < 0 ) {
dofydoink 12:595ed862e52f 77 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 6:f0a18e28a322 78 "Perhaps the server socket is not connected to a remote host? "
WD40andTape 6:f0a18e28a322 79 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 7:5b6a2cefbf3b 80 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 81 return;
WD40andTape 6:f0a18e28a322 82 }
WD40andTape 17:bbaf3e8440ad 83 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RECV MUTEX
WD40andTape 9:cd3607ba5643 84 input = hlcomms.process_message();
WD40andTape 1:2a43cf183a62 85
WD40andTape 7:5b6a2cefbf3b 86 // PROCESS INPUT
WD40andTape 19:e5acb2183d4e 87 if(IS_PRINT_OUTPUT) printf("REPLAN, %f\r\n",input.speed);
WD40andTape 14:54c3759e76ed 88
WD40andTape 19:e5acb2183d4e 89 double dblTarget_mm[N_CHANNELS]; // The currenly assigned final target position (actuator will reach this at end of path)
WD40andTape 19:e5acb2183d4e 90 // Update rear segment
WD40andTape 19:e5acb2183d4e 91 dblTarget_mm[3] = input.psi[0][0]*1000;
WD40andTape 19:e5acb2183d4e 92 dblTarget_mm[5] = 0.0; //input.psi[0][1]*1000;
WD40andTape 19:e5acb2183d4e 93 dblTarget_mm[6] = 0.0; //input.psi[0][2]*1000;
WD40andTape 19:e5acb2183d4e 94 // Update mid segment
WD40andTape 19:e5acb2183d4e 95 dblTarget_mm[4] = input.psi[1][0]*1000;
WD40andTape 19:e5acb2183d4e 96 dblTarget_mm[7] = dblTarget_mm[4]; // Same because two pumps are used
WD40andTape 14:54c3759e76ed 97 // Update front segment
WD40andTape 19:e5acb2183d4e 98 dblTarget_mm[0] = input.psi[2][0]*1000;
WD40andTape 19:e5acb2183d4e 99 dblTarget_mm[1] = input.psi[2][1]*1000;
WD40andTape 19:e5acb2183d4e 100 dblTarget_mm[2] = input.psi[2][2]*1000;
dofydoink 11:7029367a1840 101
WD40andTape 21:0b10d8e615d1 102 // Lock mutex, preventing setDemandsForLL from running
WD40andTape 19:e5acb2183d4e 103 mutPathIn.lock();
WD40andTape 19:e5acb2183d4e 104 // Limit requested speed
WD40andTape 19:e5acb2183d4e 105 double limitedSpeed_mmps = min( max( 0.0 , input.speed ) , (double)MAX_SPEED_MMPS );
WD40andTape 19:e5acb2183d4e 106 // For each actuator, limit the input position, calculate the position change, and select the absolute max
WD40andTape 19:e5acb2183d4e 107 double dblDisplacementToTarget_mm[N_CHANNELS];
WD40andTape 19:e5acb2183d4e 108 double maxDistanceToTarget_mm = 0.0;
WD40andTape 19:e5acb2183d4e 109 for(int i=0; i<N_CHANNELS; i++) {
WD40andTape 26:7c59002c9cd7 110 double dblCurrentPosition_mm = llcomms.positionSensor_mm[i];
WD40andTape 26:7c59002c9cd7 111 if(dblTarget_mm[i]<0 || dblCurrentPosition_mm<0) { // If requested position is negative or the sensor feedback is erroneous
WD40andTape 19:e5acb2183d4e 112 // Set actuator position change to 0
WD40andTape 19:e5acb2183d4e 113 dblDisplacementToTarget_mm[i] = 0.0;
WD40andTape 19:e5acb2183d4e 114 } else { // Requested position is positive
WD40andTape 19:e5acb2183d4e 115 // ? Limit requested chamber lengths
WD40andTape 19:e5acb2183d4e 116 // ? Convert from chamber length to actuator space
WD40andTape 19:e5acb2183d4e 117 // Limit actuator position
WD40andTape 26:7c59002c9cd7 118 dblTarget_mm[i] = min( max( 0.0 , dblTarget_mm[i] ) , (double)MAX_ACTUATOR_LIMIT_MM );
WD40andTape 19:e5acb2183d4e 119 // Calculate actuator position change
WD40andTape 26:7c59002c9cd7 120 dblDisplacementToTarget_mm[i] = dblTarget_mm[i] - dblCurrentPosition_mm;
WD40andTape 19:e5acb2183d4e 121 // Select the max absolute actuator position change
WD40andTape 19:e5acb2183d4e 122 if(fabs(dblDisplacementToTarget_mm[i])>maxDistanceToTarget_mm) {
WD40andTape 19:e5acb2183d4e 123 maxDistanceToTarget_mm = fabs(dblDisplacementToTarget_mm[i]);
WD40andTape 19:e5acb2183d4e 124 }
WD40andTape 19:e5acb2183d4e 125 }
dofydoink 11:7029367a1840 126 }
WD40andTape 19:e5acb2183d4e 127 // For max actuator position change, calculate the time to destination at the limited speed
WD40andTape 19:e5acb2183d4e 128 double maxTimeToTarget_s = fabs(maxDistanceToTarget_mm) / limitedSpeed_mmps;
WD40andTape 19:e5acb2183d4e 129 // For each actuator, replan target position and velocity as required
WD40andTape 19:e5acb2183d4e 130 for(int i=0; i<N_CHANNELS; i++) {
WD40andTape 19:e5acb2183d4e 131 // If requested actuator position change is already within tolerance, do NOT replan that actuator
WD40andTape 26:7c59002c9cd7 132 //printf("%d\t%0.5f\t%0.5f\r\n",i,dblDisplacementToTarget_mm[i],FLT_PATH_TOLERANCE_MM);
WD40andTape 26:7c59002c9cd7 133 //printf("%d\t%0.5f\t%0.5f\t%0.5f\r\n",i,dblTarget_mm[i],fabs(dblDisplacementToTarget_mm[i]),maxTimeToTarget_s);
WD40andTape 26:7c59002c9cd7 134 if( fabs(dblDisplacementToTarget_mm[i]) < FLT_PATH_TOLERANCE_MM ) continue;
WD40andTape 19:e5acb2183d4e 135 // Calculate velocity for each motor to synchronise movements to complete in max time
WD40andTape 26:7c59002c9cd7 136 // Set dblDemandPosition_mm and dblDemandSpeed_mmps
WD40andTape 26:7c59002c9cd7 137 //printf("%d\t%0.5f\t%0.5f\t%0.5f\r\n",i,dblTarget_mm[i],fabs(dblDisplacementToTarget_mm[i]),maxTimeToTarget_s);
WD40andTape 26:7c59002c9cd7 138 dblDemandPosition_mm[i] = dblTarget_mm[i];
WD40andTape 26:7c59002c9cd7 139 dblDemandSpeed_mmps[i] = fabs(dblDisplacementToTarget_mm[i]) / maxTimeToTarget_s;
WD40andTape 19:e5acb2183d4e 140 }
WD40andTape 21:0b10d8e615d1 141 // Unlock mutex, allowing setDemandsForLL to run again
WD40andTape 19:e5acb2183d4e 142 mutPathIn.unlock();
dofydoink 12:595ed862e52f 143
WD40andTape 6:f0a18e28a322 144 // SEND MESSAGE
WD40andTape 19:e5acb2183d4e 145 error_code = hlcomms.send_duration_message(&maxTimeToTarget_s);
WD40andTape 6:f0a18e28a322 146 if( error_code < 0 ) {
dofydoink 12:595ed862e52f 147 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 6:f0a18e28a322 148 "Perhaps the server socket is not bound or not set to listen for connections? "
WD40andTape 6:f0a18e28a322 149 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 7:5b6a2cefbf3b 150 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 151 return;
WD40andTape 6:f0a18e28a322 152 }
WD40andTape 6:f0a18e28a322 153 }
WD40andTape 6:f0a18e28a322 154
dofydoink 0:607bc887b6e0 155 }
dofydoink 0:607bc887b6e0 156
WD40andTape 24:bc852aa89e7a 157 void startLLcomms() { // Send new demands to LL after receiving new target data
WD40andTape 24:bc852aa89e7a 158 semLLcomms.release(); // Uses threadSetDemands which is normal priority
WD40andTape 24:bc852aa89e7a 159 }
WD40andTape 24:bc852aa89e7a 160
WD40andTape 26:7c59002c9cd7 161 Timer timerA;
WD40andTape 21:0b10d8e615d1 162 void setDemandsForLL() {
WD40andTape 21:0b10d8e615d1 163
WD40andTape 4:303584310071 164 while(1) {
WD40andTape 21:0b10d8e615d1 165 semLLcomms.wait();
WD40andTape 21:0b10d8e615d1 166 mutPathIn.lock(); // Lock relevant mutex
WD40andTape 21:0b10d8e615d1 167 for(short int i=0; i<N_CHANNELS; i++) { // For each LL
WD40andTape 21:0b10d8e615d1 168 llcomms.mutChannel[i].lock(); // MUTEX LOCK
WD40andTape 26:7c59002c9cd7 169 //llcomms.demandPosition_mm[i] = 2*sin(timerA.read())+4;
WD40andTape 26:7c59002c9cd7 170 //llcomms.demandSpeed_mmps[i] = 4.0;
WD40andTape 26:7c59002c9cd7 171 //printf("%d\t%0.5f\t%0.5f\r\n",i,dblDemandPosition_mm[i],dblDemandSpeed_mmps[i]);
WD40andTape 26:7c59002c9cd7 172 llcomms.demandPosition_mm[i] = dblDemandPosition_mm[i];
WD40andTape 26:7c59002c9cd7 173 llcomms.demandSpeed_mmps[i] = dblDemandSpeed_mmps[i];
WD40andTape 21:0b10d8e615d1 174 llcomms.mutChannel[i].unlock(); // MUTEX UNLOCK
WD40andTape 22:82871f00f89d 175 llcomms.isDataReady[i] = 1; // Signal that data ready
WD40andTape 21:0b10d8e615d1 176 } // end for
WD40andTape 21:0b10d8e615d1 177 mutPathIn.unlock(); // Unlock relevant mutex
WD40andTape 24:bc852aa89e7a 178 } // end while(1)
WD40andTape 26:7c59002c9cd7 179
WD40andTape 3:c83291bf9fd2 180 }
dofydoink 0:607bc887b6e0 181
WD40andTape 13:a373dfc57b89 182 int main() {
WD40andTape 7:5b6a2cefbf3b 183 pc.baud(BAUD_RATE);
WD40andTape 17:bbaf3e8440ad 184 printf("ML engage. Compiled at %s\r\n.",__TIME__);
dofydoink 5:712e7634c779 185 wait(3);
WD40andTape 9:cd3607ba5643 186
WD40andTape 26:7c59002c9cd7 187 timerA.start();
WD40andTape 26:7c59002c9cd7 188
WD40andTape 10:1b6daba32452 189 threadLowLevelSPI.start(callback(&llcomms.queue, &EventQueue::dispatch_forever)); // Start the event queue
WD40andTape 7:5b6a2cefbf3b 190 threadReceiveAndReplan.start(ReceiveAndReplan);// Start replanning thread
WD40andTape 21:0b10d8e615d1 191 threadSetDemands.start(setDemandsForLL); // Start planning thread
WD40andTape 17:bbaf3e8440ad 192 threadSensorFeedback.start(sendSensorData); // Start sensor feedback thread
WD40andTape 7:5b6a2cefbf3b 193
WD40andTape 24:bc852aa89e7a 194 setDemandsTicker.attach(&startLLcomms, 1/(float)LL_DEMANDS_FREQ_HZ); // Set up LL comms thread to recur at fixed intervals
dofydoink 0:607bc887b6e0 195
WD40andTape 7:5b6a2cefbf3b 196 Thread::wait(1);
WD40andTape 1:2a43cf183a62 197 while(1) {
WD40andTape 1:2a43cf183a62 198 Thread::wait(osWaitForever);
dofydoink 0:607bc887b6e0 199 }
WD40andTape 7:5b6a2cefbf3b 200 }