Mid level control code

Dependencies:   ros_lib_kinetic

Committer:
WD40andTape
Date:
Mon Dec 17 15:09:44 2018 +0000
Revision:
25:88e6cccde856
Parent:
24:bc852aa89e7a
Child:
26:7c59002c9cd7
Fixed splitting across 2 SPI buses in SendReceiveData. Still need to fix SPI writing and processing received data.

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 21:0b10d8e615d1 12 const float FLT_PATH_TOLERANCE = 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 21:0b10d8e615d1 15 double dblDemandVelocity_mmps[N_CHANNELS] = { 0.0 }; // The linear path velocity (not sent to actuator)
WD40andTape 21:0b10d8e615d1 16 double dblDemandPosition_mtrs[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 22:82871f00f89d 38 int error_code = hlcomms.send_sensor_message(llcomms.positionSensor_m,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 19:e5acb2183d4e 110 // If requested position is negative
WD40andTape 19:e5acb2183d4e 111 if(dblTarget_mm[i]<0) {
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 21:0b10d8e615d1 118 dblTarget_mm[i] = min( max( 0.0 , dblTarget_mm[i] ) , (double)MAX_ACTUATOR_LIMIT ); // !!! USE A CONSTANT FOR THIS
WD40andTape 19:e5acb2183d4e 119 // Calculate actuator position change
WD40andTape 22:82871f00f89d 120 double dblCurrentPosition = llcomms.positionSensor_m[i];
WD40andTape 19:e5acb2183d4e 121 dblDisplacementToTarget_mm[i] = dblTarget_mm[i] - dblCurrentPosition;
WD40andTape 19:e5acb2183d4e 122 // Select the max absolute actuator position change
WD40andTape 19:e5acb2183d4e 123 if(fabs(dblDisplacementToTarget_mm[i])>maxDistanceToTarget_mm) {
WD40andTape 19:e5acb2183d4e 124 maxDistanceToTarget_mm = fabs(dblDisplacementToTarget_mm[i]);
WD40andTape 19:e5acb2183d4e 125 }
WD40andTape 19:e5acb2183d4e 126 }
dofydoink 11:7029367a1840 127 }
WD40andTape 19:e5acb2183d4e 128 // For max actuator position change, calculate the time to destination at the limited speed
WD40andTape 19:e5acb2183d4e 129 double maxTimeToTarget_s = fabs(maxDistanceToTarget_mm) / limitedSpeed_mmps;
WD40andTape 19:e5acb2183d4e 130 // For each actuator, replan target position and velocity as required
WD40andTape 19:e5acb2183d4e 131 for(int i=0; i<N_CHANNELS; i++) {
WD40andTape 19:e5acb2183d4e 132 // If requested actuator position change is already within tolerance, do NOT replan that actuator
WD40andTape 19:e5acb2183d4e 133 if( fabs(dblDisplacementToTarget_mm[i]) < FLT_PATH_TOLERANCE ) continue;
WD40andTape 19:e5acb2183d4e 134 // Calculate velocity for each motor to synchronise movements to complete in max time
WD40andTape 21:0b10d8e615d1 135 // Set dblDemandPosition_mtrs and dblDemandVelocity_mmps
WD40andTape 21:0b10d8e615d1 136 dblDemandPosition_mtrs[i] = dblTarget_mm[i];
WD40andTape 21:0b10d8e615d1 137 dblDemandVelocity_mmps[i] = dblDisplacementToTarget_mm[i] / maxTimeToTarget_s;
WD40andTape 19:e5acb2183d4e 138 }
WD40andTape 21:0b10d8e615d1 139 // Unlock mutex, allowing setDemandsForLL to run again
WD40andTape 19:e5acb2183d4e 140 mutPathIn.unlock();
dofydoink 12:595ed862e52f 141
WD40andTape 6:f0a18e28a322 142 // SEND MESSAGE
WD40andTape 19:e5acb2183d4e 143 error_code = hlcomms.send_duration_message(&maxTimeToTarget_s);
WD40andTape 6:f0a18e28a322 144 if( error_code < 0 ) {
dofydoink 12:595ed862e52f 145 if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. "
WD40andTape 6:f0a18e28a322 146 "Perhaps the server socket is not bound or not set to listen for connections? "
WD40andTape 6:f0a18e28a322 147 "Or the socket is set to non-blocking or timed out?\n\r", error_code);
WD40andTape 7:5b6a2cefbf3b 148 hlcomms.close_server();
WD40andTape 6:f0a18e28a322 149 return;
WD40andTape 6:f0a18e28a322 150 }
WD40andTape 6:f0a18e28a322 151 }
WD40andTape 6:f0a18e28a322 152
dofydoink 0:607bc887b6e0 153 }
dofydoink 0:607bc887b6e0 154
WD40andTape 24:bc852aa89e7a 155 void startLLcomms() { // Send new demands to LL after receiving new target data
WD40andTape 24:bc852aa89e7a 156 semLLcomms.release(); // Uses threadSetDemands which is normal priority
WD40andTape 24:bc852aa89e7a 157 }
WD40andTape 24:bc852aa89e7a 158
WD40andTape 21:0b10d8e615d1 159 void setDemandsForLL() {
WD40andTape 21:0b10d8e615d1 160
WD40andTape 4:303584310071 161 while(1) {
WD40andTape 21:0b10d8e615d1 162 semLLcomms.wait();
WD40andTape 21:0b10d8e615d1 163
WD40andTape 21:0b10d8e615d1 164 mutPathIn.lock(); // Lock relevant mutex
WD40andTape 21:0b10d8e615d1 165 for(short int i=0; i<N_CHANNELS; i++) { // For each LL
WD40andTape 21:0b10d8e615d1 166 llcomms.mutChannel[i].lock(); // MUTEX LOCK
WD40andTape 24:bc852aa89e7a 167 llcomms.demandPosition[i] = dblDemandPosition_mtrs[i];
WD40andTape 24:bc852aa89e7a 168 llcomms.demandSpeed[i] = dblDemandVelocity_mmps[i];
WD40andTape 21:0b10d8e615d1 169 llcomms.mutChannel[i].unlock(); // MUTEX UNLOCK
WD40andTape 22:82871f00f89d 170 llcomms.isDataReady[i] = 1; // Signal that data ready
WD40andTape 21:0b10d8e615d1 171 } // end for
WD40andTape 21:0b10d8e615d1 172 mutPathIn.unlock(); // Unlock relevant mutex
WD40andTape 24:bc852aa89e7a 173 } // end while(1)
WD40andTape 7:5b6a2cefbf3b 174
WD40andTape 3:c83291bf9fd2 175 }
dofydoink 0:607bc887b6e0 176
WD40andTape 13:a373dfc57b89 177 int main() {
WD40andTape 7:5b6a2cefbf3b 178 pc.baud(BAUD_RATE);
WD40andTape 17:bbaf3e8440ad 179 printf("ML engage. Compiled at %s\r\n.",__TIME__);
dofydoink 5:712e7634c779 180 wait(3);
WD40andTape 9:cd3607ba5643 181
WD40andTape 10:1b6daba32452 182 threadLowLevelSPI.start(callback(&llcomms.queue, &EventQueue::dispatch_forever)); // Start the event queue
WD40andTape 7:5b6a2cefbf3b 183 threadReceiveAndReplan.start(ReceiveAndReplan);// Start replanning thread
WD40andTape 21:0b10d8e615d1 184 threadSetDemands.start(setDemandsForLL); // Start planning thread
WD40andTape 17:bbaf3e8440ad 185 threadSensorFeedback.start(sendSensorData); // Start sensor feedback thread
WD40andTape 7:5b6a2cefbf3b 186
WD40andTape 24:bc852aa89e7a 187 setDemandsTicker.attach(&startLLcomms, 1/(float)LL_DEMANDS_FREQ_HZ); // Set up LL comms thread to recur at fixed intervals
dofydoink 0:607bc887b6e0 188
WD40andTape 7:5b6a2cefbf3b 189 Thread::wait(1);
WD40andTape 1:2a43cf183a62 190 while(1) {
WD40andTape 1:2a43cf183a62 191 Thread::wait(osWaitForever);
dofydoink 0:607bc887b6e0 192 }
WD40andTape 7:5b6a2cefbf3b 193 }