
Basic Mid-Level control for the rebuilt MorphGI control unit, using PWM to communicate with the low level controllers.
Dependencies: ros_lib_kinetic
main.cpp@24:bc852aa89e7a, 2018-12-17 (annotated)
- Committer:
- WD40andTape
- Date:
- Mon Dec 17 14:34:37 2018 +0000
- Revision:
- 24:bc852aa89e7a
- Parent:
- 23:61526647cc8a
- Child:
- 25:88e6cccde856
Updating ML/LL communications.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
WD40andTape | 22:82871f00f89d | 11 | //!!!!!!!!!!!!!!!!!!!! CHECK POSITION SENSOR IS FROM 0 AND IN METERS |
WD40andTape | 22:82871f00f89d | 12 | |
dofydoink | 12:595ed862e52f | 13 | // Maximum achievable mm path tolerance plus additional % tolerance |
WD40andTape | 21:0b10d8e615d1 | 14 | const float FLT_PATH_TOLERANCE = MAX_SPEED_MMPS * (1/LL_DEMANDS_FREQ_HZ) * (1.0f+FLT_PERCENT_PATH_TOLERANCE); |
dofydoink | 0:607bc887b6e0 | 15 | |
WD40andTape | 21:0b10d8e615d1 | 16 | // DEMAND VARIABLES |
WD40andTape | 21:0b10d8e615d1 | 17 | double dblDemandVelocity_mmps[N_CHANNELS] = { 0.0 }; // The linear path velocity (not sent to actuator) |
WD40andTape | 21:0b10d8e615d1 | 18 | double dblDemandPosition_mtrs[N_CHANNELS] = { 0.0 }; // The final target position for the actuator |
dofydoink | 11:7029367a1840 | 19 | |
WD40andTape | 7:5b6a2cefbf3b | 20 | Serial pc(USBTX, USBRX); // tx, rx for usb debugging |
dofydoink | 12:595ed862e52f | 21 | LLComms llcomms; |
WD40andTape | 16:1e2804a4e5bd | 22 | HLComms hlcomms(SERVER_PORT); |
WD40andTape | 3:c83291bf9fd2 | 23 | |
WD40andTape | 10:1b6daba32452 | 24 | Thread threadLowLevelSPI(osPriorityRealtime); |
WD40andTape | 21:0b10d8e615d1 | 25 | Thread threadSetDemands(osPriorityNormal); |
WD40andTape | 4:303584310071 | 26 | Thread threadReceiveAndReplan(osPriorityBelowNormal); |
WD40andTape | 17:bbaf3e8440ad | 27 | Thread threadSensorFeedback(osPriorityBelowNormal); |
dofydoink | 0:607bc887b6e0 | 28 | |
WD40andTape | 4:303584310071 | 29 | Mutex mutPathIn; |
WD40andTape | 21:0b10d8e615d1 | 30 | Semaphore semLLcomms(1); |
WD40andTape | 17:bbaf3e8440ad | 31 | Semaphore semSensorData(1); |
dofydoink | 0:607bc887b6e0 | 32 | |
WD40andTape | 21:0b10d8e615d1 | 33 | Ticker setDemandsTicker; |
WD40andTape | 17:bbaf3e8440ad | 34 | Ticker SendSensorDataTicker; |
WD40andTape | 17:bbaf3e8440ad | 35 | |
WD40andTape | 17:bbaf3e8440ad | 36 | |
WD40andTape | 17:bbaf3e8440ad | 37 | void sendSensorData() { |
WD40andTape | 17:bbaf3e8440ad | 38 | while( true ) { |
WD40andTape | 17:bbaf3e8440ad | 39 | semSensorData.wait(); |
WD40andTape | 22:82871f00f89d | 40 | int error_code = hlcomms.send_sensor_message(llcomms.positionSensor_m,llcomms.pressureSensor_bar); |
WD40andTape | 17:bbaf3e8440ad | 41 | /*if( error_code < 0 ) { |
WD40andTape | 17:bbaf3e8440ad | 42 | if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. " |
WD40andTape | 17:bbaf3e8440ad | 43 | "Perhaps the server socket is not bound or not set to listen for connections? " |
WD40andTape | 17:bbaf3e8440ad | 44 | "Or the socket is set to non-blocking or timed out?\n\r", error_code); |
WD40andTape | 17:bbaf3e8440ad | 45 | hlcomms.close_server(); |
WD40andTape | 17:bbaf3e8440ad | 46 | return; |
WD40andTape | 17:bbaf3e8440ad | 47 | }*/ |
WD40andTape | 17:bbaf3e8440ad | 48 | } |
WD40andTape | 17:bbaf3e8440ad | 49 | } |
WD40andTape | 17:bbaf3e8440ad | 50 | |
WD40andTape | 17:bbaf3e8440ad | 51 | void signalSendSensorData() { |
WD40andTape | 17:bbaf3e8440ad | 52 | semSensorData.release(); |
WD40andTape | 17:bbaf3e8440ad | 53 | } |
dofydoink | 0:607bc887b6e0 | 54 | |
WD40andTape | 7:5b6a2cefbf3b | 55 | // This function will be called when a new transmission is received from high level |
WD40andTape | 7:5b6a2cefbf3b | 56 | void ReceiveAndReplan() { |
WD40andTape | 6:f0a18e28a322 | 57 | |
WD40andTape | 6:f0a18e28a322 | 58 | int error_code; |
WD40andTape | 7:5b6a2cefbf3b | 59 | error_code = hlcomms.setup_server(); |
WD40andTape | 6:f0a18e28a322 | 60 | if( error_code == -1 ) return; |
WD40andTape | 7:5b6a2cefbf3b | 61 | error_code = hlcomms.accept_connection(); |
WD40andTape | 7:5b6a2cefbf3b | 62 | if( error_code == -1 ) { |
WD40andTape | 7:5b6a2cefbf3b | 63 | hlcomms.close_server(); |
WD40andTape | 7:5b6a2cefbf3b | 64 | return; |
WD40andTape | 7:5b6a2cefbf3b | 65 | } |
WD40andTape | 6:f0a18e28a322 | 66 | |
WD40andTape | 22:82871f00f89d | 67 | SendSensorDataTicker.attach(&signalSendSensorData, 1/(float)SENSOR_FEEDBACK_HZ); // Set up planning thread to recur at fixed intervals |
WD40andTape | 17:bbaf3e8440ad | 68 | |
WD40andTape | 7:5b6a2cefbf3b | 69 | struct msg_format input; //hlcomms.msg_format |
WD40andTape | 4:303584310071 | 70 | |
WD40andTape | 6:f0a18e28a322 | 71 | while( true ) { |
WD40andTape | 6:f0a18e28a322 | 72 | // RECEIVE MESSAGE |
WD40andTape | 9:cd3607ba5643 | 73 | error_code = hlcomms.receive_message(); |
WD40andTape | 6:f0a18e28a322 | 74 | if( error_code == NSAPI_ERROR_NO_CONNECTION ) { // -3004 |
dofydoink | 12:595ed862e52f | 75 | if(IS_PRINT_OUTPUT) printf("Client disconnected.\n\r"); |
WD40andTape | 7:5b6a2cefbf3b | 76 | hlcomms.close_server(); |
WD40andTape | 6:f0a18e28a322 | 77 | return; |
WD40andTape | 6:f0a18e28a322 | 78 | } else if( error_code < 0 ) { |
dofydoink | 12:595ed862e52f | 79 | if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. " |
WD40andTape | 6:f0a18e28a322 | 80 | "Perhaps the server socket is not connected to a remote host? " |
WD40andTape | 6:f0a18e28a322 | 81 | "Or the socket is set to non-blocking or timed out?\n\r", error_code); |
WD40andTape | 7:5b6a2cefbf3b | 82 | hlcomms.close_server(); |
WD40andTape | 6:f0a18e28a322 | 83 | return; |
WD40andTape | 6:f0a18e28a322 | 84 | } |
WD40andTape | 17:bbaf3e8440ad | 85 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RECV MUTEX |
WD40andTape | 9:cd3607ba5643 | 86 | input = hlcomms.process_message(); |
WD40andTape | 1:2a43cf183a62 | 87 | |
WD40andTape | 7:5b6a2cefbf3b | 88 | // PROCESS INPUT |
WD40andTape | 19:e5acb2183d4e | 89 | if(IS_PRINT_OUTPUT) printf("REPLAN, %f\r\n",input.speed); |
WD40andTape | 14:54c3759e76ed | 90 | |
WD40andTape | 19:e5acb2183d4e | 91 | double dblTarget_mm[N_CHANNELS]; // The currenly assigned final target position (actuator will reach this at end of path) |
WD40andTape | 19:e5acb2183d4e | 92 | // Update rear segment |
WD40andTape | 19:e5acb2183d4e | 93 | dblTarget_mm[3] = input.psi[0][0]*1000; |
WD40andTape | 19:e5acb2183d4e | 94 | dblTarget_mm[5] = 0.0; //input.psi[0][1]*1000; |
WD40andTape | 19:e5acb2183d4e | 95 | dblTarget_mm[6] = 0.0; //input.psi[0][2]*1000; |
WD40andTape | 19:e5acb2183d4e | 96 | // Update mid segment |
WD40andTape | 19:e5acb2183d4e | 97 | dblTarget_mm[4] = input.psi[1][0]*1000; |
WD40andTape | 19:e5acb2183d4e | 98 | dblTarget_mm[7] = dblTarget_mm[4]; // Same because two pumps are used |
WD40andTape | 14:54c3759e76ed | 99 | // Update front segment |
WD40andTape | 19:e5acb2183d4e | 100 | dblTarget_mm[0] = input.psi[2][0]*1000; |
WD40andTape | 19:e5acb2183d4e | 101 | dblTarget_mm[1] = input.psi[2][1]*1000; |
WD40andTape | 19:e5acb2183d4e | 102 | dblTarget_mm[2] = input.psi[2][2]*1000; |
dofydoink | 11:7029367a1840 | 103 | |
WD40andTape | 21:0b10d8e615d1 | 104 | // Lock mutex, preventing setDemandsForLL from running |
WD40andTape | 19:e5acb2183d4e | 105 | mutPathIn.lock(); |
WD40andTape | 19:e5acb2183d4e | 106 | // Limit requested speed |
WD40andTape | 19:e5acb2183d4e | 107 | double limitedSpeed_mmps = min( max( 0.0 , input.speed ) , (double)MAX_SPEED_MMPS ); |
WD40andTape | 19:e5acb2183d4e | 108 | // For each actuator, limit the input position, calculate the position change, and select the absolute max |
WD40andTape | 19:e5acb2183d4e | 109 | double dblDisplacementToTarget_mm[N_CHANNELS]; |
WD40andTape | 19:e5acb2183d4e | 110 | double maxDistanceToTarget_mm = 0.0; |
WD40andTape | 19:e5acb2183d4e | 111 | for(int i=0; i<N_CHANNELS; i++) { |
WD40andTape | 19:e5acb2183d4e | 112 | // If requested position is negative |
WD40andTape | 19:e5acb2183d4e | 113 | if(dblTarget_mm[i]<0) { |
WD40andTape | 19:e5acb2183d4e | 114 | // Set actuator position change to 0 |
WD40andTape | 19:e5acb2183d4e | 115 | dblDisplacementToTarget_mm[i] = 0.0; |
WD40andTape | 19:e5acb2183d4e | 116 | } else { // Requested position is positive |
WD40andTape | 19:e5acb2183d4e | 117 | // ? Limit requested chamber lengths |
WD40andTape | 19:e5acb2183d4e | 118 | // ? Convert from chamber length to actuator space |
WD40andTape | 19:e5acb2183d4e | 119 | // Limit actuator position |
WD40andTape | 21:0b10d8e615d1 | 120 | dblTarget_mm[i] = min( max( 0.0 , dblTarget_mm[i] ) , (double)MAX_ACTUATOR_LIMIT ); // !!! USE A CONSTANT FOR THIS |
WD40andTape | 19:e5acb2183d4e | 121 | // Calculate actuator position change |
WD40andTape | 22:82871f00f89d | 122 | double dblCurrentPosition = llcomms.positionSensor_m[i]; |
WD40andTape | 19:e5acb2183d4e | 123 | dblDisplacementToTarget_mm[i] = dblTarget_mm[i] - dblCurrentPosition; |
WD40andTape | 19:e5acb2183d4e | 124 | // Select the max absolute actuator position change |
WD40andTape | 19:e5acb2183d4e | 125 | if(fabs(dblDisplacementToTarget_mm[i])>maxDistanceToTarget_mm) { |
WD40andTape | 19:e5acb2183d4e | 126 | maxDistanceToTarget_mm = fabs(dblDisplacementToTarget_mm[i]); |
WD40andTape | 19:e5acb2183d4e | 127 | } |
WD40andTape | 19:e5acb2183d4e | 128 | } |
dofydoink | 11:7029367a1840 | 129 | } |
WD40andTape | 19:e5acb2183d4e | 130 | // For max actuator position change, calculate the time to destination at the limited speed |
WD40andTape | 19:e5acb2183d4e | 131 | double maxTimeToTarget_s = fabs(maxDistanceToTarget_mm) / limitedSpeed_mmps; |
WD40andTape | 19:e5acb2183d4e | 132 | // For each actuator, replan target position and velocity as required |
WD40andTape | 19:e5acb2183d4e | 133 | for(int i=0; i<N_CHANNELS; i++) { |
WD40andTape | 19:e5acb2183d4e | 134 | // If requested actuator position change is already within tolerance, do NOT replan that actuator |
WD40andTape | 19:e5acb2183d4e | 135 | if( fabs(dblDisplacementToTarget_mm[i]) < FLT_PATH_TOLERANCE ) continue; |
WD40andTape | 19:e5acb2183d4e | 136 | // Calculate velocity for each motor to synchronise movements to complete in max time |
WD40andTape | 21:0b10d8e615d1 | 137 | // Set dblDemandPosition_mtrs and dblDemandVelocity_mmps |
WD40andTape | 21:0b10d8e615d1 | 138 | dblDemandPosition_mtrs[i] = dblTarget_mm[i]; |
WD40andTape | 21:0b10d8e615d1 | 139 | dblDemandVelocity_mmps[i] = 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 | 21:0b10d8e615d1 | 161 | void setDemandsForLL() { |
WD40andTape | 21:0b10d8e615d1 | 162 | |
WD40andTape | 4:303584310071 | 163 | while(1) { |
WD40andTape | 21:0b10d8e615d1 | 164 | semLLcomms.wait(); |
WD40andTape | 21:0b10d8e615d1 | 165 | |
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 | 24:bc852aa89e7a | 169 | llcomms.demandPosition[i] = dblDemandPosition_mtrs[i]; |
WD40andTape | 24:bc852aa89e7a | 170 | llcomms.demandSpeed[i] = dblDemandVelocity_mmps[i]; |
WD40andTape | 21:0b10d8e615d1 | 171 | llcomms.mutChannel[i].unlock(); // MUTEX UNLOCK |
WD40andTape | 22:82871f00f89d | 172 | llcomms.isDataReady[i] = 1; // Signal that data ready |
WD40andTape | 21:0b10d8e615d1 | 173 | } // end for |
WD40andTape | 21:0b10d8e615d1 | 174 | mutPathIn.unlock(); // Unlock relevant mutex |
WD40andTape | 24:bc852aa89e7a | 175 | } // end while(1) |
WD40andTape | 7:5b6a2cefbf3b | 176 | |
WD40andTape | 3:c83291bf9fd2 | 177 | } |
dofydoink | 0:607bc887b6e0 | 178 | |
WD40andTape | 13:a373dfc57b89 | 179 | int main() { |
WD40andTape | 7:5b6a2cefbf3b | 180 | pc.baud(BAUD_RATE); |
WD40andTape | 17:bbaf3e8440ad | 181 | printf("ML engage. Compiled at %s\r\n.",__TIME__); |
dofydoink | 5:712e7634c779 | 182 | wait(3); |
WD40andTape | 9:cd3607ba5643 | 183 | |
WD40andTape | 10:1b6daba32452 | 184 | threadLowLevelSPI.start(callback(&llcomms.queue, &EventQueue::dispatch_forever)); // Start the event queue |
WD40andTape | 7:5b6a2cefbf3b | 185 | threadReceiveAndReplan.start(ReceiveAndReplan);// Start replanning thread |
WD40andTape | 21:0b10d8e615d1 | 186 | threadSetDemands.start(setDemandsForLL); // Start planning thread |
WD40andTape | 17:bbaf3e8440ad | 187 | threadSensorFeedback.start(sendSensorData); // Start sensor feedback thread |
WD40andTape | 7:5b6a2cefbf3b | 188 | |
WD40andTape | 24:bc852aa89e7a | 189 | setDemandsTicker.attach(&startLLcomms, 1/(float)LL_DEMANDS_FREQ_HZ); // Set up LL comms thread to recur at fixed intervals |
dofydoink | 0:607bc887b6e0 | 190 | |
WD40andTape | 7:5b6a2cefbf3b | 191 | Thread::wait(1); |
WD40andTape | 1:2a43cf183a62 | 192 | while(1) { |
WD40andTape | 1:2a43cf183a62 | 193 | Thread::wait(osWaitForever); |
dofydoink | 0:607bc887b6e0 | 194 | } |
WD40andTape | 7:5b6a2cefbf3b | 195 | } |