Mid level control code

Dependencies:   ros_lib_kinetic

Committer:
dofydoink
Date:
Fri Dec 14 09:58:24 2018 +0000
Revision:
23:61526647cc8a
Parent:
22:82871f00f89d
Child:
24:bc852aa89e7a
Updated communications. Still need to sort out how feedback is handled by ML.

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
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 22:82871f00f89d 55 void startLLcomms() { // Send new demands to LL after receiving new target data
WD40andTape 22:82871f00f89d 56 semLLcomms.release(); // Uses threadSetDemands which is normal priority
dofydoink 0:607bc887b6e0 57 }
dofydoink 0:607bc887b6e0 58
WD40andTape 7:5b6a2cefbf3b 59 // This function will be called when a new transmission is received from high level
WD40andTape 7:5b6a2cefbf3b 60 void ReceiveAndReplan() {
WD40andTape 6:f0a18e28a322 61
WD40andTape 6:f0a18e28a322 62 int error_code;
WD40andTape 7:5b6a2cefbf3b 63 error_code = hlcomms.setup_server();
WD40andTape 6:f0a18e28a322 64 if( error_code == -1 ) return;
WD40andTape 7:5b6a2cefbf3b 65 error_code = hlcomms.accept_connection();
WD40andTape 7:5b6a2cefbf3b 66 if( error_code == -1 ) {
WD40andTape 7:5b6a2cefbf3b 67 hlcomms.close_server();
WD40andTape 7:5b6a2cefbf3b 68 return;
WD40andTape 7:5b6a2cefbf3b 69 }
WD40andTape 6:f0a18e28a322 70
WD40andTape 22:82871f00f89d 71 SendSensorDataTicker.attach(&signalSendSensorData, 1/(float)SENSOR_FEEDBACK_HZ); // Set up planning thread to recur at fixed intervals
WD40andTape 17:bbaf3e8440ad 72
WD40andTape 7:5b6a2cefbf3b 73 struct msg_format input; //hlcomms.msg_format
WD40andTape 4:303584310071 74
WD40andTape 6:f0a18e28a322 75 while( true ) {
WD40andTape 6:f0a18e28a322 76 // RECEIVE MESSAGE
WD40andTape 9:cd3607ba5643 77 error_code = hlcomms.receive_message();
WD40andTape 6:f0a18e28a322 78 if( error_code == NSAPI_ERROR_NO_CONNECTION ) { // -3004
dofydoink 12:595ed862e52f 79 if(IS_PRINT_OUTPUT) printf("Client disconnected.\n\r");
WD40andTape 7:5b6a2cefbf3b 80 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 81 return;
WD40andTape 6:f0a18e28a322 82 } else if( error_code < 0 ) {
dofydoink 12:595ed862e52f 83 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 6:f0a18e28a322 84 "Perhaps the server socket is not connected to a remote host? "
WD40andTape 6:f0a18e28a322 85 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 7:5b6a2cefbf3b 86 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 87 return;
WD40andTape 6:f0a18e28a322 88 }
WD40andTape 17:bbaf3e8440ad 89 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RECV MUTEX
WD40andTape 9:cd3607ba5643 90 input = hlcomms.process_message();
WD40andTape 1:2a43cf183a62 91
WD40andTape 7:5b6a2cefbf3b 92 // PROCESS INPUT
WD40andTape 19:e5acb2183d4e 93 if(IS_PRINT_OUTPUT) printf("REPLAN, %f\r\n",input.speed);
WD40andTape 14:54c3759e76ed 94
WD40andTape 19:e5acb2183d4e 95 double dblTarget_mm[N_CHANNELS]; // The currenly assigned final target position (actuator will reach this at end of path)
WD40andTape 19:e5acb2183d4e 96 // Update rear segment
WD40andTape 19:e5acb2183d4e 97 dblTarget_mm[3] = input.psi[0][0]*1000;
WD40andTape 19:e5acb2183d4e 98 dblTarget_mm[5] = 0.0; //input.psi[0][1]*1000;
WD40andTape 19:e5acb2183d4e 99 dblTarget_mm[6] = 0.0; //input.psi[0][2]*1000;
WD40andTape 19:e5acb2183d4e 100 // Update mid segment
WD40andTape 19:e5acb2183d4e 101 dblTarget_mm[4] = input.psi[1][0]*1000;
WD40andTape 19:e5acb2183d4e 102 dblTarget_mm[7] = dblTarget_mm[4]; // Same because two pumps are used
WD40andTape 14:54c3759e76ed 103 // Update front segment
WD40andTape 19:e5acb2183d4e 104 dblTarget_mm[0] = input.psi[2][0]*1000;
WD40andTape 19:e5acb2183d4e 105 dblTarget_mm[1] = input.psi[2][1]*1000;
WD40andTape 19:e5acb2183d4e 106 dblTarget_mm[2] = input.psi[2][2]*1000;
dofydoink 11:7029367a1840 107
WD40andTape 21:0b10d8e615d1 108 // Lock mutex, preventing setDemandsForLL from running
WD40andTape 19:e5acb2183d4e 109 mutPathIn.lock();
WD40andTape 19:e5acb2183d4e 110 // Limit requested speed
WD40andTape 19:e5acb2183d4e 111 double limitedSpeed_mmps = min( max( 0.0 , input.speed ) , (double)MAX_SPEED_MMPS );
WD40andTape 19:e5acb2183d4e 112 // For each actuator, limit the input position, calculate the position change, and select the absolute max
WD40andTape 19:e5acb2183d4e 113 double dblDisplacementToTarget_mm[N_CHANNELS];
WD40andTape 19:e5acb2183d4e 114 double maxDistanceToTarget_mm = 0.0;
WD40andTape 19:e5acb2183d4e 115 for(int i=0; i<N_CHANNELS; i++) {
WD40andTape 19:e5acb2183d4e 116 // If requested position is negative
WD40andTape 19:e5acb2183d4e 117 if(dblTarget_mm[i]<0) {
WD40andTape 19:e5acb2183d4e 118 // Set actuator position change to 0
WD40andTape 19:e5acb2183d4e 119 dblDisplacementToTarget_mm[i] = 0.0;
WD40andTape 19:e5acb2183d4e 120 } else { // Requested position is positive
WD40andTape 19:e5acb2183d4e 121 // ? Limit requested chamber lengths
WD40andTape 19:e5acb2183d4e 122 // ? Convert from chamber length to actuator space
WD40andTape 19:e5acb2183d4e 123 // Limit actuator position
WD40andTape 21:0b10d8e615d1 124 dblTarget_mm[i] = min( max( 0.0 , dblTarget_mm[i] ) , (double)MAX_ACTUATOR_LIMIT ); // !!! USE A CONSTANT FOR THIS
WD40andTape 19:e5acb2183d4e 125 // Calculate actuator position change
WD40andTape 22:82871f00f89d 126 double dblCurrentPosition = llcomms.positionSensor_m[i];
WD40andTape 19:e5acb2183d4e 127 dblDisplacementToTarget_mm[i] = dblTarget_mm[i] - dblCurrentPosition;
WD40andTape 19:e5acb2183d4e 128 // Select the max absolute actuator position change
WD40andTape 19:e5acb2183d4e 129 if(fabs(dblDisplacementToTarget_mm[i])>maxDistanceToTarget_mm) {
WD40andTape 19:e5acb2183d4e 130 maxDistanceToTarget_mm = fabs(dblDisplacementToTarget_mm[i]);
WD40andTape 19:e5acb2183d4e 131 }
WD40andTape 19:e5acb2183d4e 132 }
dofydoink 11:7029367a1840 133 }
WD40andTape 19:e5acb2183d4e 134 // For max actuator position change, calculate the time to destination at the limited speed
WD40andTape 19:e5acb2183d4e 135 double maxTimeToTarget_s = fabs(maxDistanceToTarget_mm) / limitedSpeed_mmps;
WD40andTape 19:e5acb2183d4e 136 // For each actuator, replan target position and velocity as required
WD40andTape 19:e5acb2183d4e 137 for(int i=0; i<N_CHANNELS; i++) {
WD40andTape 19:e5acb2183d4e 138 // If requested actuator position change is already within tolerance, do NOT replan that actuator
WD40andTape 19:e5acb2183d4e 139 if( fabs(dblDisplacementToTarget_mm[i]) < FLT_PATH_TOLERANCE ) continue;
WD40andTape 19:e5acb2183d4e 140 // Calculate velocity for each motor to synchronise movements to complete in max time
WD40andTape 21:0b10d8e615d1 141 // Set dblDemandPosition_mtrs and dblDemandVelocity_mmps
WD40andTape 21:0b10d8e615d1 142 dblDemandPosition_mtrs[i] = dblTarget_mm[i];
WD40andTape 21:0b10d8e615d1 143 dblDemandVelocity_mmps[i] = dblDisplacementToTarget_mm[i] / maxTimeToTarget_s;
WD40andTape 19:e5acb2183d4e 144 }
WD40andTape 21:0b10d8e615d1 145 // Unlock mutex, allowing setDemandsForLL to run again
WD40andTape 19:e5acb2183d4e 146 mutPathIn.unlock();
dofydoink 12:595ed862e52f 147
WD40andTape 6:f0a18e28a322 148 // SEND MESSAGE
WD40andTape 19:e5acb2183d4e 149 error_code = hlcomms.send_duration_message(&maxTimeToTarget_s);
WD40andTape 6:f0a18e28a322 150 if( error_code < 0 ) {
dofydoink 12:595ed862e52f 151 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 6:f0a18e28a322 152 "Perhaps the server socket is not bound or not set to listen for connections? "
WD40andTape 6:f0a18e28a322 153 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 7:5b6a2cefbf3b 154 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 155 return;
WD40andTape 6:f0a18e28a322 156 }
WD40andTape 6:f0a18e28a322 157 }
WD40andTape 6:f0a18e28a322 158
dofydoink 0:607bc887b6e0 159 }
dofydoink 0:607bc887b6e0 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
dofydoink 23:61526647cc8a 169 llcomms.demandPosition[i] = (int) ((dblDemandPosition_mtrs[i]/MAX_ACTUATOR_LENGTH)*511); // Convert to a 9-bit number
dofydoink 23:61526647cc8a 170 llcomms.demandPosition[i] = llcomms.demandPosition[i] & 0x1FF; // Ensure number is 9-bit
dofydoink 23:61526647cc8a 171 llcomms.demandSpeed[i] = (int) ((dblDemandVelocity_mmps[i]/MAX_SPEED_MMPS)*511);// Convert to a 9-bit number
dofydoink 23:61526647cc8a 172 llcomms.demandSpeed[i] = llcomms.demandSpeed[i] & 0x1FF; // Ensure number is 9-bit
WD40andTape 21:0b10d8e615d1 173 llcomms.mutChannel[i].unlock(); // MUTEX UNLOCK
WD40andTape 22:82871f00f89d 174 llcomms.isDataReady[i] = 1; // Signal that data ready
WD40andTape 21:0b10d8e615d1 175 } // end for
WD40andTape 21:0b10d8e615d1 176 mutPathIn.unlock(); // Unlock relevant mutex
WD40andTape 22:82871f00f89d 177
WD40andTape 21:0b10d8e615d1 178 } // end while
WD40andTape 7:5b6a2cefbf3b 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 10:1b6daba32452 187 threadLowLevelSPI.start(callback(&llcomms.queue, &EventQueue::dispatch_forever)); // Start the event queue
WD40andTape 7:5b6a2cefbf3b 188 threadReceiveAndReplan.start(ReceiveAndReplan);// Start replanning thread
WD40andTape 21:0b10d8e615d1 189 threadSetDemands.start(setDemandsForLL); // Start planning thread
WD40andTape 17:bbaf3e8440ad 190 threadSensorFeedback.start(sendSensorData); // Start sensor feedback thread
WD40andTape 7:5b6a2cefbf3b 191
WD40andTape 21:0b10d8e615d1 192 setDemandsTicker.attach(&startLLcomms, 1/LL_DEMANDS_FREQ_HZ); // Set up LL comms thread to recur at fixed intervals
dofydoink 0:607bc887b6e0 193
WD40andTape 7:5b6a2cefbf3b 194 Thread::wait(1);
WD40andTape 1:2a43cf183a62 195 while(1) {
WD40andTape 1:2a43cf183a62 196 Thread::wait(osWaitForever);
dofydoink 0:607bc887b6e0 197 }
WD40andTape 7:5b6a2cefbf3b 198 }