
Mid level control code
Dependencies: ros_lib_kinetic
main.cpp@15:59471daef4cb, 2018-08-31 (annotated)
- Committer:
- WD40andTape
- Date:
- Fri Aug 31 13:54:50 2018 +0000
- Revision:
- 15:59471daef4cb
- Parent:
- 14:54c3759e76ed
- Child:
- 16:1e2804a4e5bd
Increased actuator limits to 0.04 m.
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 | 13:a373dfc57b89 | 11 | //DigitalOut pinTesty(PB_8); |
WD40andTape | 13:a373dfc57b89 | 12 | |
dofydoink | 12:595ed862e52f | 13 | // Maximum achievable mm path tolerance plus additional % tolerance |
dofydoink | 12:595ed862e52f | 14 | const float FLT_PATH_TOLERANCE = MAX_SPEED_MMPS * PATH_SAMPLE_TIME_S * (1.0f+FLT_PERCENT_PATH_TOLERANCE); |
dofydoink | 0:607bc887b6e0 | 15 | |
WD40andTape | 7:5b6a2cefbf3b | 16 | // PATH VARIABLES |
WD40andTape | 7:5b6a2cefbf3b | 17 | double dblVelocity_mmps[N_CHANNELS] = { 0.0 }; // The linear path velocity (not sent to actuator) |
WD40andTape | 7:5b6a2cefbf3b | 18 | double dblLinearPathCurrentPos_mm[N_CHANNELS]={ 0.0 }; // The current position of the linear path (not sent to actuator) |
WD40andTape | 7:5b6a2cefbf3b | 19 | double dblTargetActPos_mm[N_CHANNELS] = { 0.0 }; // The final target position for the actuator |
dofydoink | 0:607bc887b6e0 | 20 | |
dofydoink | 12:595ed862e52f | 21 | // These have to be declared in the global scope or we get a stack overflow. No idea why. |
dofydoink | 11:7029367a1840 | 22 | double _dblVelocity_mmps[N_CHANNELS]; |
dofydoink | 12:595ed862e52f | 23 | //double _dblLinearPathCurrentPos_mm[N_CHANNELS]; |
dofydoink | 12:595ed862e52f | 24 | double dblADCCurrentPosition[N_CHANNELS]; |
dofydoink | 11:7029367a1840 | 25 | |
WD40andTape | 7:5b6a2cefbf3b | 26 | Serial pc(USBTX, USBRX); // tx, rx for usb debugging |
dofydoink | 12:595ed862e52f | 27 | LLComms llcomms; |
WD40andTape | 3:c83291bf9fd2 | 28 | |
WD40andTape | 10:1b6daba32452 | 29 | Thread threadLowLevelSPI(osPriorityRealtime); |
WD40andTape | 13:a373dfc57b89 | 30 | Thread threadSmoothPathPlan(osPriorityNormal); |
WD40andTape | 4:303584310071 | 31 | Thread threadReceiveAndReplan(osPriorityBelowNormal); |
dofydoink | 0:607bc887b6e0 | 32 | |
WD40andTape | 4:303584310071 | 33 | Mutex mutPathIn; |
WD40andTape | 7:5b6a2cefbf3b | 34 | Semaphore semPathPlan(1); |
dofydoink | 0:607bc887b6e0 | 35 | |
WD40andTape | 7:5b6a2cefbf3b | 36 | Timer timer; |
WD40andTape | 7:5b6a2cefbf3b | 37 | Ticker PathCalculationTicker; |
dofydoink | 0:607bc887b6e0 | 38 | |
WD40andTape | 7:5b6a2cefbf3b | 39 | void startPathPlan() { // Plan a new linear path after receiving new target data |
WD40andTape | 4:303584310071 | 40 | semPathPlan.release(); // Uses threadReceiveAndReplan which is below normal priority to ensure consistent transmission to LL |
dofydoink | 0:607bc887b6e0 | 41 | } |
dofydoink | 0:607bc887b6e0 | 42 | |
WD40andTape | 7:5b6a2cefbf3b | 43 | // This function will be called when a new transmission is received from high level |
WD40andTape | 7:5b6a2cefbf3b | 44 | void ReceiveAndReplan() { |
WD40andTape | 7:5b6a2cefbf3b | 45 | HLComms hlcomms(SERVER_PORT); |
WD40andTape | 6:f0a18e28a322 | 46 | |
WD40andTape | 6:f0a18e28a322 | 47 | int error_code; |
WD40andTape | 7:5b6a2cefbf3b | 48 | error_code = hlcomms.setup_server(); |
WD40andTape | 6:f0a18e28a322 | 49 | if( error_code == -1 ) return; |
WD40andTape | 7:5b6a2cefbf3b | 50 | error_code = hlcomms.accept_connection(); |
WD40andTape | 7:5b6a2cefbf3b | 51 | if( error_code == -1 ) { |
WD40andTape | 7:5b6a2cefbf3b | 52 | hlcomms.close_server(); |
WD40andTape | 7:5b6a2cefbf3b | 53 | return; |
WD40andTape | 7:5b6a2cefbf3b | 54 | } |
WD40andTape | 6:f0a18e28a322 | 55 | |
WD40andTape | 7:5b6a2cefbf3b | 56 | int ii; |
WD40andTape | 7:5b6a2cefbf3b | 57 | struct msg_format input; //hlcomms.msg_format |
WD40andTape | 4:303584310071 | 58 | |
WD40andTape | 6:f0a18e28a322 | 59 | while( true ) { |
WD40andTape | 6:f0a18e28a322 | 60 | // RECEIVE MESSAGE |
WD40andTape | 9:cd3607ba5643 | 61 | error_code = hlcomms.receive_message(); |
WD40andTape | 6:f0a18e28a322 | 62 | if( error_code == NSAPI_ERROR_NO_CONNECTION ) { // -3004 |
dofydoink | 12:595ed862e52f | 63 | if(IS_PRINT_OUTPUT) printf("Client disconnected.\n\r"); |
WD40andTape | 7:5b6a2cefbf3b | 64 | hlcomms.close_server(); |
WD40andTape | 6:f0a18e28a322 | 65 | return; |
WD40andTape | 6:f0a18e28a322 | 66 | } else if( error_code < 0 ) { |
dofydoink | 12:595ed862e52f | 67 | if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. " |
WD40andTape | 6:f0a18e28a322 | 68 | "Perhaps the server socket is not connected to a remote host? " |
WD40andTape | 6:f0a18e28a322 | 69 | "Or the socket is set to non-blocking or timed out?\n\r", error_code); |
WD40andTape | 7:5b6a2cefbf3b | 70 | hlcomms.close_server(); |
WD40andTape | 6:f0a18e28a322 | 71 | return; |
WD40andTape | 6:f0a18e28a322 | 72 | } |
WD40andTape | 9:cd3607ba5643 | 73 | input = hlcomms.process_message(); |
WD40andTape | 1:2a43cf183a62 | 74 | |
WD40andTape | 7:5b6a2cefbf3b | 75 | // PROCESS INPUT |
WD40andTape | 7:5b6a2cefbf3b | 76 | double dblTargetChambLen_mm[N_CHANNELS]; // The currenly assigned final target position (actuator will reach this at end of path) |
dofydoink | 12:595ed862e52f | 77 | if(IS_PRINT_OUTPUT) printf("REPLAN, %f\r\n",input.duration); |
WD40andTape | 7:5b6a2cefbf3b | 78 | // Update front segment |
dofydoink | 11:7029367a1840 | 79 | /*dblTargetChambLen_mm[0] = input.psi[0][0]*1000; |
WD40andTape | 6:f0a18e28a322 | 80 | dblTargetChambLen_mm[1] = input.psi[0][1]*1000; |
WD40andTape | 6:f0a18e28a322 | 81 | dblTargetChambLen_mm[2] = input.psi[0][2]*1000; |
WD40andTape | 7:5b6a2cefbf3b | 82 | // Update mid segment |
WD40andTape | 6:f0a18e28a322 | 83 | dblTargetChambLen_mm[6] = input.psi[1][0]*1000; |
WD40andTape | 7:5b6a2cefbf3b | 84 | dblTargetChambLen_mm[7] = dblTargetChambLen_mm[6]; // Same because two pumps are used |
WD40andTape | 7:5b6a2cefbf3b | 85 | // Update rear segment |
WD40andTape | 6:f0a18e28a322 | 86 | dblTargetChambLen_mm[3] = input.psi[2][0]*1000; |
WD40andTape | 6:f0a18e28a322 | 87 | dblTargetChambLen_mm[4] = input.psi[2][1]*1000; |
dofydoink | 11:7029367a1840 | 88 | dblTargetChambLen_mm[5] = input.psi[2][2]*1000;*/ |
WD40andTape | 14:54c3759e76ed | 89 | |
WD40andTape | 14:54c3759e76ed | 90 | //update rear Segment |
dofydoink | 11:7029367a1840 | 91 | dblTargetChambLen_mm[3] = input.psi[0][0]*1000; |
WD40andTape | 14:54c3759e76ed | 92 | dblTargetChambLen_mm[5] = 0.0;//input.psi[0][1]*1000;//not used |
WD40andTape | 14:54c3759e76ed | 93 | dblTargetChambLen_mm[6] = 0.0;//input.psi[0][2]*1000;//not used |
WD40andTape | 14:54c3759e76ed | 94 | |
WD40andTape | 14:54c3759e76ed | 95 | //update mid segment |
WD40andTape | 14:54c3759e76ed | 96 | dblTargetChambLen_mm[4] = input.psi[1][0]*1000; |
WD40andTape | 14:54c3759e76ed | 97 | dblTargetChambLen_mm[7] = dblTargetChambLen_mm[4]; // Same because two pumps are used |
WD40andTape | 14:54c3759e76ed | 98 | |
WD40andTape | 14:54c3759e76ed | 99 | // Update front segment |
dofydoink | 11:7029367a1840 | 100 | dblTargetChambLen_mm[0] = input.psi[2][0]*1000; |
dofydoink | 11:7029367a1840 | 101 | dblTargetChambLen_mm[1] = input.psi[2][1]*1000; |
dofydoink | 11:7029367a1840 | 102 | dblTargetChambLen_mm[2] = input.psi[2][2]*1000; |
dofydoink | 11:7029367a1840 | 103 | |
dofydoink | 11:7029367a1840 | 104 | mutPathIn.lock(); // Lock variables to avoid race condition |
dofydoink | 12:595ed862e52f | 105 | /*for(int j = 0; j<N_CHANNELS; j++) { |
dofydoink | 11:7029367a1840 | 106 | //_dblVelocity_mmps[j] = dblVelocity_mmps[j]; |
dofydoink | 11:7029367a1840 | 107 | _dblLinearPathCurrentPos_mm[j] = dblLinearPathCurrentPos_mm[j]; |
dofydoink | 12:595ed862e52f | 108 | }*/ |
dofydoink | 12:595ed862e52f | 109 | for(int j = 0; j<N_CHANNELS; j++) { |
dofydoink | 12:595ed862e52f | 110 | dblADCCurrentPosition[j] = dblLinearPathCurrentPos_mm[j]; |
dofydoink | 12:595ed862e52f | 111 | //dblADCCurrentPosition[j] = llcomms.ReadADCPosition_mtrs(j); // Read position from channel |
dofydoink | 11:7029367a1840 | 112 | } |
dofydoink | 12:595ed862e52f | 113 | |
dofydoink | 11:7029367a1840 | 114 | mutPathIn.unlock(); // Unlock mutex |
dofydoink | 0:607bc887b6e0 | 115 | |
WD40andTape | 13:a373dfc57b89 | 116 | //bool isTimeChanged = 0; |
WD40andTape | 6:f0a18e28a322 | 117 | double dblMaxRecalculatedTime = input.duration; |
WD40andTape | 13:a373dfc57b89 | 118 | // Convert from requested chamber to actuator space and limit actuator positions |
WD40andTape | 4:303584310071 | 119 | for (ii = 0; ii< N_CHANNELS; ii++) { |
WD40andTape | 13:a373dfc57b89 | 120 | // If sent a negative requested position, do NOT replan that actuator |
WD40andTape | 14:54c3759e76ed | 121 | if( dblTargetChambLen_mm[ii] < 0.0 ) continue; |
dofydoink | 0:607bc887b6e0 | 122 | //check to see if positions are achievable |
dofydoink | 12:595ed862e52f | 123 | dblTargetActPos_mm[ii] = dblTargetChambLen_mm[ii]*FLT_ACTUATOR_CONVERSION[ii]; |
WD40andTape | 15:59471daef4cb | 124 | dblTargetActPos_mm[ii] = min( max( 0.0 , dblTargetActPos_mm[ii] ) , 40.0 ); |
WD40andTape | 6:f0a18e28a322 | 125 | //!! LIMIT CHAMBER LENGTHS TOO |
WD40andTape | 6:f0a18e28a322 | 126 | } |
WD40andTape | 13:a373dfc57b89 | 127 | // Calculate achievable velocities, and hence time, for the requested move to complete within tolerance |
WD40andTape | 6:f0a18e28a322 | 128 | double dblActPosChange; |
WD40andTape | 6:f0a18e28a322 | 129 | short sgn; |
WD40andTape | 7:5b6a2cefbf3b | 130 | for (ii = 0; ii< N_CHANNELS; ii++) { // Work out new velocities |
WD40andTape | 13:a373dfc57b89 | 131 | // If sent a negative requested position, do NOT replan that actuator |
WD40andTape | 14:54c3759e76ed | 132 | if( dblTargetChambLen_mm[ii] < 0.0 ) continue; |
WD40andTape | 13:a373dfc57b89 | 133 | |
dofydoink | 11:7029367a1840 | 134 | /*dblActPosChange = 1.0; // or = 0.0; |
dofydoink | 11:7029367a1840 | 135 | _dblVelocity_mmps[ii] = 0.0;*/ // DOESN'T CRASH |
dofydoink | 11:7029367a1840 | 136 | /*dblActPosChange = dblTargetActPos_mm[ii]; |
dofydoink | 11:7029367a1840 | 137 | _dblVelocity_mmps[ii] = 0.0;*/ // DOESN'T CRASH |
dofydoink | 11:7029367a1840 | 138 | /*dblActPosChange = _dblLinearPathCurrentPos_mm[ii];*/ // DOESN'T CRASH |
dofydoink | 11:7029367a1840 | 139 | /*_dblVelocity_mmps[ii] = _dblLinearPathCurrentPos_mm[ii];*/ // DOESN'T CRASH |
dofydoink | 11:7029367a1840 | 140 | /*dblActPosChange = 0.0; |
dofydoink | 11:7029367a1840 | 141 | _dblVelocity_mmps[ii] = _dblLinearPathCurrentPos_mm[ii];*/ // DOESN'T CRASH |
dofydoink | 11:7029367a1840 | 142 | |
dofydoink | 11:7029367a1840 | 143 | // DOES CRASH but not with a return at the end of while OR if _ variables are declared globally |
dofydoink | 11:7029367a1840 | 144 | /*dblActPosChange = _dblLinearPathCurrentPos_mm[ii]; |
dofydoink | 11:7029367a1840 | 145 | _dblVelocity_mmps[ii] = 0.0;*/ |
dofydoink | 11:7029367a1840 | 146 | |
dofydoink | 12:595ed862e52f | 147 | dblActPosChange = dblTargetActPos_mm[ii] - dblADCCurrentPosition[ii]; |
WD40andTape | 13:a373dfc57b89 | 148 | if( fabs(dblActPosChange) < FLT_PATH_TOLERANCE ) { // If actuator ii is already within tolerance |
WD40andTape | 6:f0a18e28a322 | 149 | dblActPosChange = 0.0; |
WD40andTape | 13:a373dfc57b89 | 150 | //isTimeChanged = 1; |
dofydoink | 0:607bc887b6e0 | 151 | } |
dofydoink | 11:7029367a1840 | 152 | //IS BELOW |
WD40andTape | 13:a373dfc57b89 | 153 | if( input.duration < 0.000000001 ) { // If max (safe) velocity was requested |
WD40andTape | 6:f0a18e28a322 | 154 | sgn = (dblActPosChange > 0) ? 1 : ((dblActPosChange < 0) ? -1 : 0); |
dofydoink | 11:7029367a1840 | 155 | _dblVelocity_mmps[ii] = sgn * MAX_SPEED_MMPS; |
WD40andTape | 13:a373dfc57b89 | 156 | //isTimeChanged = 1; |
WD40andTape | 6:f0a18e28a322 | 157 | } else { |
dofydoink | 11:7029367a1840 | 158 | _dblVelocity_mmps[ii] = dblActPosChange / input.duration; |
WD40andTape | 6:f0a18e28a322 | 159 | } |
dofydoink | 11:7029367a1840 | 160 | //IS ABOVE |
WD40andTape | 13:a373dfc57b89 | 161 | // Check to see if velocities are achievable -- this can move into the else section of the above if statement |
dofydoink | 11:7029367a1840 | 162 | if(abs(_dblVelocity_mmps[ii]) > MAX_SPEED_MMPS) { |
dofydoink | 11:7029367a1840 | 163 | if(_dblVelocity_mmps[ii]>0) { |
dofydoink | 11:7029367a1840 | 164 | _dblVelocity_mmps[ii] = MAX_SPEED_MMPS; |
WD40andTape | 6:f0a18e28a322 | 165 | } else { |
dofydoink | 11:7029367a1840 | 166 | _dblVelocity_mmps[ii] = -1*MAX_SPEED_MMPS; |
WD40andTape | 6:f0a18e28a322 | 167 | } |
WD40andTape | 13:a373dfc57b89 | 168 | //isTimeChanged = 1; |
WD40andTape | 6:f0a18e28a322 | 169 | } |
WD40andTape | 13:a373dfc57b89 | 170 | // Recalculate the move's time after altering the position and/or velocity |
WD40andTape | 6:f0a18e28a322 | 171 | double dblRecalculatedTime; |
dofydoink | 11:7029367a1840 | 172 | if( fabs(_dblVelocity_mmps[ii]) < 0.000000001 ) { |
WD40andTape | 6:f0a18e28a322 | 173 | dblRecalculatedTime = 0; |
WD40andTape | 6:f0a18e28a322 | 174 | } else { |
dofydoink | 11:7029367a1840 | 175 | dblRecalculatedTime = dblActPosChange / _dblVelocity_mmps[ii]; |
WD40andTape | 6:f0a18e28a322 | 176 | } |
WD40andTape | 13:a373dfc57b89 | 177 | // Find the maximum time for any actuator's move for synchronization |
WD40andTape | 6:f0a18e28a322 | 178 | if( dblRecalculatedTime > dblMaxRecalculatedTime ) { |
WD40andTape | 6:f0a18e28a322 | 179 | dblMaxRecalculatedTime = dblRecalculatedTime; |
WD40andTape | 6:f0a18e28a322 | 180 | } |
WD40andTape | 6:f0a18e28a322 | 181 | } |
WD40andTape | 13:a373dfc57b89 | 182 | // Finally recalculate all of the velocities based upon this maximum time for synchronization |
WD40andTape | 13:a373dfc57b89 | 183 | // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!MUTEX FOR dblVelocity_mmps and dblTargetActPos_mm?? |
WD40andTape | 13:a373dfc57b89 | 184 | //if( dblMaxRecalculatedTime >= 0.000000001 ) { // isTimeChanged && |
WD40andTape | 13:a373dfc57b89 | 185 | for (ii = 0; ii< N_CHANNELS; ii++) { // Work out new velocities |
WD40andTape | 13:a373dfc57b89 | 186 | // If sent a negative requested position, do NOT replan that actuator |
WD40andTape | 14:54c3759e76ed | 187 | if( dblTargetChambLen_mm[ii] < 0.0 ) continue; |
WD40andTape | 13:a373dfc57b89 | 188 | |
WD40andTape | 13:a373dfc57b89 | 189 | _dblVelocity_mmps[ii] = (dblTargetActPos_mm[ii] - dblADCCurrentPosition[ii]) / dblMaxRecalculatedTime; |
WD40andTape | 13:a373dfc57b89 | 190 | dblVelocity_mmps[ii] = _dblVelocity_mmps[ii]; |
WD40andTape | 6:f0a18e28a322 | 191 | } |
WD40andTape | 13:a373dfc57b89 | 192 | // !!!!!!!!! IF TIME ISN'T CHANGED THEN VELOCITY ISN'T UPDATED..................................................................................... |
WD40andTape | 6:f0a18e28a322 | 193 | // SEND MESSAGE |
dofydoink | 11:7029367a1840 | 194 | //dblMaxRecalculatedTime = 1.0; |
WD40andTape | 9:cd3607ba5643 | 195 | hlcomms.make_message(&dblMaxRecalculatedTime); |
WD40andTape | 9:cd3607ba5643 | 196 | error_code = hlcomms.send_message(); |
WD40andTape | 6:f0a18e28a322 | 197 | if( error_code < 0 ) { |
dofydoink | 12:595ed862e52f | 198 | if(IS_PRINT_OUTPUT) printf("Error %i. Could not send data over the TCP socket. " |
WD40andTape | 6:f0a18e28a322 | 199 | "Perhaps the server socket is not bound or not set to listen for connections? " |
WD40andTape | 6:f0a18e28a322 | 200 | "Or the socket is set to non-blocking or timed out?\n\r", error_code); |
WD40andTape | 7:5b6a2cefbf3b | 201 | hlcomms.close_server(); |
WD40andTape | 6:f0a18e28a322 | 202 | return; |
WD40andTape | 6:f0a18e28a322 | 203 | } |
WD40andTape | 6:f0a18e28a322 | 204 | } |
WD40andTape | 6:f0a18e28a322 | 205 | |
dofydoink | 0:607bc887b6e0 | 206 | } |
dofydoink | 0:607bc887b6e0 | 207 | |
WD40andTape | 4:303584310071 | 208 | void CalculateSmoothPath() { |
dofydoink | 5:712e7634c779 | 209 | int jj; |
WD40andTape | 1:2a43cf183a62 | 210 | double dblMeasuredSampleTime; |
WD40andTape | 7:5b6a2cefbf3b | 211 | double dblSmoothPathCurrentPos_mm[N_CHANNELS] = { 0.0 }; // The current position of the smooth path (not sent to actuator) |
WD40andTape | 7:5b6a2cefbf3b | 212 | //double dblPosition_mtrs[N_CHANNELS]; // The actual chamber lengths in meters given as the change in length relative to neutral (should always be >=0) |
WD40andTape | 7:5b6a2cefbf3b | 213 | //double dblPressure_bar[N_CHANNELS]; // The pressure in a given chamber in bar (1 bar = 100,000 Pa) |
WD40andTape | 4:303584310071 | 214 | while(1) { |
dofydoink | 0:607bc887b6e0 | 215 | semPathPlan.wait(); |
WD40andTape | 13:a373dfc57b89 | 216 | //pinTesty = 1; |
WD40andTape | 1:2a43cf183a62 | 217 | // If run time is more than 50 us from expected, calculate from measured time step |
WD40andTape | 4:303584310071 | 218 | if (fabs(PATH_SAMPLE_TIME_S*1000000 - timer.read_us()) > 50) { |
dofydoink | 5:712e7634c779 | 219 | dblMeasuredSampleTime = timer.read(); |
WD40andTape | 4:303584310071 | 220 | } else { |
WD40andTape | 1:2a43cf183a62 | 221 | dblMeasuredSampleTime = PATH_SAMPLE_TIME_S; |
WD40andTape | 1:2a43cf183a62 | 222 | } |
WD40andTape | 4:303584310071 | 223 | timer.reset(); |
WD40andTape | 1:2a43cf183a62 | 224 | |
dofydoink | 5:712e7634c779 | 225 | for(jj = 0; jj < N_CHANNELS; jj++) { |
WD40andTape | 7:5b6a2cefbf3b | 226 | //dblPressure_bar[jj] = ReadADCPressure_bar(jj); // Read pressure from channel |
WD40andTape | 7:5b6a2cefbf3b | 227 | //dblPosition_mtrs[jj] = ReadADCPosition_mtrs(jj); // Read position from channel |
WD40andTape | 1:2a43cf183a62 | 228 | |
WD40andTape | 7:5b6a2cefbf3b | 229 | // Calculate next step in linear path |
WD40andTape | 7:5b6a2cefbf3b | 230 | mutPathIn.lock(); // Lock relevant mutex |
WD40andTape | 7:5b6a2cefbf3b | 231 | // Check tolerance |
dofydoink | 12:595ed862e52f | 232 | if (fabs(dblLinearPathCurrentPos_mm[jj] - dblTargetActPos_mm[jj]) <= FLT_PATH_TOLERANCE) { |
WD40andTape | 7:5b6a2cefbf3b | 233 | dblVelocity_mmps[jj] = 0.0; // Stop linear path generation when linear path is within tolerance of target position |
WD40andTape | 7:5b6a2cefbf3b | 234 | } |
dofydoink | 11:7029367a1840 | 235 | dblLinearPathCurrentPos_mm[jj] = dblLinearPathCurrentPos_mm[jj] + dblVelocity_mmps[jj]*dblMeasuredSampleTime; |
WD40andTape | 15:59471daef4cb | 236 | dblLinearPathCurrentPos_mm[jj] = min( max( 0.0 , dblLinearPathCurrentPos_mm[jj] ) , 40.0 ); |
WD40andTape | 7:5b6a2cefbf3b | 237 | mutPathIn.unlock(); // Unlock relevant mutex |
dofydoink | 0:607bc887b6e0 | 238 | |
WD40andTape | 7:5b6a2cefbf3b | 239 | // Calculate next step in smooth path |
dofydoink | 12:595ed862e52f | 240 | dblSmoothPathCurrentPos_mm[jj] = FLT_SMOOTHING_FACTOR*dblLinearPathCurrentPos_mm[jj] + (1.0f-FLT_SMOOTHING_FACTOR)*dblSmoothPathCurrentPos_mm[jj]; |
dofydoink | 11:7029367a1840 | 241 | dblSmoothPathCurrentPos_mm[jj] = max( 0.0 , dblSmoothPathCurrentPos_mm[jj] ); |
dofydoink | 11:7029367a1840 | 242 | llcomms.mutChannel[jj].lock(); // MUTEX LOCK |
WD40andTape | 10:1b6daba32452 | 243 | llcomms.demandPosition[jj] = (int) ((dblSmoothPathCurrentPos_mm[jj]/MAX_ACTUATOR_LENGTH)*8191);// Convert to a 13-bit number |
WD40andTape | 10:1b6daba32452 | 244 | llcomms.demandPosition[jj] = llcomms.demandPosition[jj] & 0x1FFF; // Ensure number is 13-bit |
WD40andTape | 8:d6657767a182 | 245 | llcomms.mutChannel[jj].unlock(); // MUTEX UNLOCK |
dofydoink | 0:607bc887b6e0 | 246 | |
WD40andTape | 10:1b6daba32452 | 247 | llcomms.isDataReady[jj] = 1; // Signal that data ready |
WD40andTape | 4:303584310071 | 248 | } // end for |
WD40andTape | 7:5b6a2cefbf3b | 249 | |
dofydoink | 12:595ed862e52f | 250 | //if(IS_PRINT_OUTPUT) printf("%f, %d\r\n",dblSmoothPathCurrentPos_mm[0], intDemandPos_Tx[0]); |
dofydoink | 12:595ed862e52f | 251 | //if(IS_PRINT_OUTPUT) printf("%f, %f, %f, %f, %f, %f, %f, %f\r\n",dblLinearPathCurrentPos_mm[0],dblLinearPathCurrentPos_mm[1],dblLinearPathCurrentPos_mm[2], |
dofydoink | 11:7029367a1840 | 252 | // dblLinearPathCurrentPos_mm[3],dblLinearPathCurrentPos_mm[4],dblLinearPathCurrentPos_mm[5],dblLinearPathCurrentPos_mm[6],dblLinearPathCurrentPos_mm[7]); |
dofydoink | 12:595ed862e52f | 253 | //if(IS_PRINT_OUTPUT) printf("%f\r\n",dblLinearPathCurrentPos_mm[0]); |
WD40andTape | 13:a373dfc57b89 | 254 | //pinTesty = 0; |
WD40andTape | 4:303584310071 | 255 | } // end while |
WD40andTape | 3:c83291bf9fd2 | 256 | } |
dofydoink | 0:607bc887b6e0 | 257 | |
WD40andTape | 13:a373dfc57b89 | 258 | int main() { |
WD40andTape | 7:5b6a2cefbf3b | 259 | pc.baud(BAUD_RATE); |
WD40andTape | 13:a373dfc57b89 | 260 | printf("Sup bruvva! I'll be your mid-level controller for today.\r\n"); |
WD40andTape | 13:a373dfc57b89 | 261 | printf("Compiled at %s\r\n.",__TIME__); |
dofydoink | 5:712e7634c779 | 262 | wait(3); |
WD40andTape | 9:cd3607ba5643 | 263 | |
dofydoink | 0:607bc887b6e0 | 264 | timer.start(); |
WD40andTape | 7:5b6a2cefbf3b | 265 | |
WD40andTape | 10:1b6daba32452 | 266 | threadLowLevelSPI.start(callback(&llcomms.queue, &EventQueue::dispatch_forever)); // Start the event queue |
WD40andTape | 7:5b6a2cefbf3b | 267 | threadReceiveAndReplan.start(ReceiveAndReplan);// Start replanning thread |
WD40andTape | 7:5b6a2cefbf3b | 268 | threadSmoothPathPlan.start(CalculateSmoothPath); // Start planning thread |
WD40andTape | 7:5b6a2cefbf3b | 269 | |
WD40andTape | 7:5b6a2cefbf3b | 270 | PathCalculationTicker.attach(&startPathPlan, PATH_SAMPLE_TIME_S); // Set up planning thread to recur at fixed intervals |
dofydoink | 0:607bc887b6e0 | 271 | |
WD40andTape | 7:5b6a2cefbf3b | 272 | Thread::wait(1); |
WD40andTape | 1:2a43cf183a62 | 273 | while(1) { |
WD40andTape | 1:2a43cf183a62 | 274 | Thread::wait(osWaitForever); |
dofydoink | 0:607bc887b6e0 | 275 | } |
WD40andTape | 7:5b6a2cefbf3b | 276 | } |