
Basic Mid-Level control for the rebuilt MorphGI control unit, using PWM to communicate with the low level controllers.
Dependencies: ros_lib_kinetic
LLComms.cpp@42:5a5ad23a4bb1, 2021-06-24 (annotated)
- Committer:
- dofydoink
- Date:
- Thu Jun 24 20:34:47 2021 +0000
- Revision:
- 42:5a5ad23a4bb1
- Parent:
- 39:e69a6fa12977
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WD40andTape | 8:d6657767a182 | 1 | // LLComms.cpp |
WD40andTape | 8:d6657767a182 | 2 | |
WD40andTape | 8:d6657767a182 | 3 | #include "LLComms.h" |
WD40andTape | 8:d6657767a182 | 4 | |
dofydoink | 12:595ed862e52f | 5 | LLComms::LLComms() : |
WD40andTape | 13:a373dfc57b89 | 6 | queue(32 * EVENTS_EVENT_SIZE), //32 //8 * EVENTS_EVENT_SIZE |
WD40andTape | 14:54c3759e76ed | 7 | spi_0(PC_12, PC_11, PC_10), |
dofydoink | 42:5a5ad23a4bb1 | 8 | // spi_1(PF_9, PF_8, PF_7), |
dofydoink | 42:5a5ad23a4bb1 | 9 | // spi_2(PB_15, PC_2, PB_13), |
WD40andTape | 8:d6657767a182 | 10 | // These interrupt pins have to be declared AFTER SPI declaration. No Clue Why. |
WD40andTape | 8:d6657767a182 | 11 | pinGate0(PF_11), |
WD40andTape | 8:d6657767a182 | 12 | pinGate1(PG_14), |
WD40andTape | 8:d6657767a182 | 13 | pinGate2(PF_15), |
dofydoink | 42:5a5ad23a4bb1 | 14 | //pinGate3(PF_12), |
dofydoink | 12:595ed862e52f | 15 | pinReset(PD_2) |
WD40andTape | 8:d6657767a182 | 16 | { // Constructor |
WD40andTape | 8:d6657767a182 | 17 | |
WD40andTape | 24:bc852aa89e7a | 18 | spi_0.format(16,2); |
WD40andTape | 24:bc852aa89e7a | 19 | spi_0.frequency(LOW_LEVEL_SPI_FREQUENCY); |
dofydoink | 42:5a5ad23a4bb1 | 20 | // spi_1.format(16,2); |
dofydoink | 42:5a5ad23a4bb1 | 21 | // spi_1.frequency(LOW_LEVEL_SPI_FREQUENCY); |
WD40andTape | 24:bc852aa89e7a | 22 | |
dofydoink | 42:5a5ad23a4bb1 | 23 | |
dofydoink | 42:5a5ad23a4bb1 | 24 | PinName LLPins[N_CHANNELS] = {PD_15, PE_10, PD_14};//, PD_11};//, PE_7, PB_1, PB_12, PD_13, PB_5}; |
dofydoink | 42:5a5ad23a4bb1 | 25 | |
WD40andTape | 36:4459be8296e9 | 26 | for (short int i=0; i<N_CHANNELS; i++) { |
WD40andTape | 10:1b6daba32452 | 27 | isDataReady[i] = 0; |
WD40andTape | 8:d6657767a182 | 28 | cs_LL[i] = new DigitalOut(LLPins[i]); |
WD40andTape | 34:54e9ebe9e87f | 29 | //cs_ADC[i] = new DigitalOut(ADCPins[i]); |
WD40andTape | 8:d6657767a182 | 30 | } |
WD40andTape | 10:1b6daba32452 | 31 | |
WD40andTape | 9:cd3607ba5643 | 32 | // Initialise relevant variables |
WD40andTape | 36:4459be8296e9 | 33 | for(short int i=0; i<N_CHANNELS; i++) { |
WD40andTape | 9:cd3607ba5643 | 34 | // All chip selects in off state |
WD40andTape | 9:cd3607ba5643 | 35 | *cs_LL[i] = 1; |
WD40andTape | 34:54e9ebe9e87f | 36 | //*cs_ADC[i] = 1; |
WD40andTape | 26:7c59002c9cd7 | 37 | // Initialise pressures/positions |
WD40andTape | 29:10a5cf37a875 | 38 | pressureSensor_uint[i] = 0.0; |
WD40andTape | 26:7c59002c9cd7 | 39 | pressureSensor_bar[i] = -1.0; |
WD40andTape | 29:10a5cf37a875 | 40 | positionSensor_uint[i] = 0.0; |
WD40andTape | 26:7c59002c9cd7 | 41 | positionSensor_mm[i] = -1.0; |
WD40andTape | 9:cd3607ba5643 | 42 | } |
WD40andTape | 9:cd3607ba5643 | 43 | pinReset = 1; // Initialise reset pin to not reset the controllers. |
WD40andTape | 9:cd3607ba5643 | 44 | wait(0.25); |
WD40andTape | 9:cd3607ba5643 | 45 | pinReset=0; // Reset controllers to be safe |
WD40andTape | 9:cd3607ba5643 | 46 | wait(0.25); |
WD40andTape | 9:cd3607ba5643 | 47 | pinReset = 1; // Ready to go |
dofydoink | 42:5a5ad23a4bb1 | 48 | |
dofydoink | 42:5a5ad23a4bb1 | 49 | // pinReset.mode(PullUp); // Initialise reset pin to not reset the controllers. |
dofydoink | 42:5a5ad23a4bb1 | 50 | // wait(0.25); |
dofydoink | 42:5a5ad23a4bb1 | 51 | // pinReset.write(0); // Reset controllers to be safe |
dofydoink | 42:5a5ad23a4bb1 | 52 | // wait(0.25); |
dofydoink | 42:5a5ad23a4bb1 | 53 | // pinReset.mode(PullUp); // Ready to go |
dofydoink | 42:5a5ad23a4bb1 | 54 | // wait(0.25); |
WD40andTape | 10:1b6daba32452 | 55 | |
WD40andTape | 10:1b6daba32452 | 56 | // Set up rise interrupts MIGHT NOT NEED TO BE POINTERS |
dofydoink | 12:595ed862e52f | 57 | pinGate0.rise(callback(this,&LLComms::rise0)); |
dofydoink | 12:595ed862e52f | 58 | pinGate1.rise(callback(this,&LLComms::rise1)); |
dofydoink | 12:595ed862e52f | 59 | pinGate2.rise(callback(this,&LLComms::rise2)); |
dofydoink | 42:5a5ad23a4bb1 | 60 | //pinGate3.rise(callback(this,&LLComms::rise3)); |
dofydoink | 42:5a5ad23a4bb1 | 61 | |
WD40andTape | 10:1b6daba32452 | 62 | // Set up fall interrupts MIGHT NOT NEED TO BE POINTERS |
dofydoink | 12:595ed862e52f | 63 | pinGate0.fall(callback(this,&LLComms::fall0)); |
dofydoink | 12:595ed862e52f | 64 | pinGate1.fall(callback(this,&LLComms::fall1)); |
dofydoink | 12:595ed862e52f | 65 | pinGate2.fall(callback(this,&LLComms::fall2)); |
dofydoink | 42:5a5ad23a4bb1 | 66 | //pinGate3.fall(callback(this,&LLComms::fall3)); |
dofydoink | 42:5a5ad23a4bb1 | 67 | |
WD40andTape | 9:cd3607ba5643 | 68 | } |
WD40andTape | 9:cd3607ba5643 | 69 | |
dofydoink | 11:7029367a1840 | 70 | //LLComms::~LLComms(void) { } // Destructor |
WD40andTape | 26:7c59002c9cd7 | 71 | unsigned int LLComms::formatMessage(short int type, double dblValue, double dblMaxValue) { |
WD40andTape | 24:bc852aa89e7a | 72 | // Convert to a 9-bit number |
WD40andTape | 24:bc852aa89e7a | 73 | int intValue = (int) ((dblValue/dblMaxValue)*511); |
WD40andTape | 24:bc852aa89e7a | 74 | intValue = intValue & 0x1FF; // Ensure number is 9-bit |
WD40andTape | 24:bc852aa89e7a | 75 | // Initialize message with value |
WD40andTape | 24:bc852aa89e7a | 76 | unsigned int intMsg = intValue; |
WD40andTape | 24:bc852aa89e7a | 77 | // Calculate checksum (the decimal sum of the position data) |
WD40andTape | 24:bc852aa89e7a | 78 | int intCheckSum = 0, intTempVar = intValue; |
WD40andTape | 26:7c59002c9cd7 | 79 | while( intTempVar>0 ) { |
WD40andTape | 24:bc852aa89e7a | 80 | intCheckSum += intTempVar%10; |
WD40andTape | 25:88e6cccde856 | 81 | intTempVar = floor(intTempVar/10.0); |
WD40andTape | 24:bc852aa89e7a | 82 | } |
WD40andTape | 24:bc852aa89e7a | 83 | // Add checksum to message |
WD40andTape | 24:bc852aa89e7a | 84 | intMsg = intMsg<<5; |
WD40andTape | 24:bc852aa89e7a | 85 | intMsg = intMsg | intCheckSum; |
WD40andTape | 24:bc852aa89e7a | 86 | // Add type bit (0 == position, 1 == velocity) |
WD40andTape | 24:bc852aa89e7a | 87 | intMsg = intMsg<<1; |
WD40andTape | 26:7c59002c9cd7 | 88 | intMsg = intMsg | (int)type; // CAST AS BOOL |
WD40andTape | 24:bc852aa89e7a | 89 | // Calculate decimal parity check for the whole message |
WD40andTape | 24:bc852aa89e7a | 90 | unsigned int count = 0, b = 1; |
WD40andTape | 24:bc852aa89e7a | 91 | for(short int i=0; i<32; i++) { |
WD40andTape | 24:bc852aa89e7a | 92 | if( intMsg & (b << i) ) count++; |
WD40andTape | 24:bc852aa89e7a | 93 | } |
WD40andTape | 24:bc852aa89e7a | 94 | // Add parity bit to message (0 == Odd, 1 == Even) |
WD40andTape | 27:6853ee8ffefd | 95 | // Parity selected in this way to prevent 0x0000 from passing checks |
WD40andTape | 24:bc852aa89e7a | 96 | bool boolParity = !(bool)(count%2); |
WD40andTape | 24:bc852aa89e7a | 97 | intMsg = intMsg<<1; |
WD40andTape | 24:bc852aa89e7a | 98 | intMsg = intMsg | (int)boolParity; |
WD40andTape | 26:7c59002c9cd7 | 99 | return intMsg; |
WD40andTape | 26:7c59002c9cd7 | 100 | } |
WD40andTape | 26:7c59002c9cd7 | 101 | |
WD40andTape | 26:7c59002c9cd7 | 102 | bool LLComms::CheckMessage(int msg) { |
WD40andTape | 26:7c59002c9cd7 | 103 | // Find message parity |
WD40andTape | 26:7c59002c9cd7 | 104 | short int count = 0; |
WD40andTape | 26:7c59002c9cd7 | 105 | for(short int i=0; i<32; i++) { |
WD40andTape | 26:7c59002c9cd7 | 106 | if( msg>>1 & (1<<i) ) count++; |
WD40andTape | 26:7c59002c9cd7 | 107 | } |
WD40andTape | 26:7c59002c9cd7 | 108 | int intParity = !(count%2); |
WD40andTape | 26:7c59002c9cd7 | 109 | // Find message CheckSum |
WD40andTape | 26:7c59002c9cd7 | 110 | int intChkSum = 0; |
WD40andTape | 26:7c59002c9cd7 | 111 | int intTempVar = msg>>7; |
WD40andTape | 26:7c59002c9cd7 | 112 | while(intTempVar > 0) { |
WD40andTape | 26:7c59002c9cd7 | 113 | intChkSum += intTempVar%10; |
WD40andTape | 26:7c59002c9cd7 | 114 | intTempVar = int(intTempVar/10); |
WD40andTape | 26:7c59002c9cd7 | 115 | } |
WD40andTape | 26:7c59002c9cd7 | 116 | // Check if parity and CheckSum match |
WD40andTape | 26:7c59002c9cd7 | 117 | bool isParityCorrect = (intParity == (msg&0x1)); |
WD40andTape | 26:7c59002c9cd7 | 118 | bool isChkSumCorrect = (intChkSum == ((msg>>2)&0x1F)); |
WD40andTape | 26:7c59002c9cd7 | 119 | bool isCheckPassed = (isParityCorrect && isChkSumCorrect); |
WD40andTape | 26:7c59002c9cd7 | 120 | return isCheckPassed; |
WD40andTape | 26:7c59002c9cd7 | 121 | } |
WD40andTape | 26:7c59002c9cd7 | 122 | |
WD40andTape | 26:7c59002c9cd7 | 123 | bool LLComms::PerformMasterSPI(SPI *spi, unsigned int outboundMsgs[], unsigned int inboundMsgsData[]) { |
WD40andTape | 26:7c59002c9cd7 | 124 | unsigned int dummyMsg = 0x5555; |
WD40andTape | 26:7c59002c9cd7 | 125 | bool isSuccess = true; |
WD40andTape | 26:7c59002c9cd7 | 126 | unsigned int inboundMsg, typeBit; |
WD40andTape | 27:6853ee8ffefd | 127 | AnalogIn ain(PA_4); // Random analogue pin |
WD40andTape | 26:7c59002c9cd7 | 128 | for(short int i=0; i<3; i++) { // Loop 3 times for 3 SPI messages |
WD40andTape | 27:6853ee8ffefd | 129 | ain.read_u16(); // Read analogue pin to cause a delay |
WD40andTape | 27:6853ee8ffefd | 130 | ain.read_u16(); |
WD40andTape | 26:7c59002c9cd7 | 131 | if(i==0) { |
WD40andTape | 26:7c59002c9cd7 | 132 | inboundMsg = spi->write(outboundMsgs[0]); |
WD40andTape | 26:7c59002c9cd7 | 133 | } else if(i==1) { |
WD40andTape | 26:7c59002c9cd7 | 134 | inboundMsg = spi->write(outboundMsgs[1]); |
WD40andTape | 26:7c59002c9cd7 | 135 | } else { |
WD40andTape | 26:7c59002c9cd7 | 136 | inboundMsg = spi->write(dummyMsg); |
WD40andTape | 26:7c59002c9cd7 | 137 | } |
dofydoink | 42:5a5ad23a4bb1 | 138 | //rawMsg[i] = inboundMsg; |
WD40andTape | 26:7c59002c9cd7 | 139 | if((unsigned int)inboundMsg != dummyMsg) { // Message is not dummy which is only used for reply |
WD40andTape | 26:7c59002c9cd7 | 140 | typeBit = inboundMsg>>1 & 0x1; |
WD40andTape | 26:7c59002c9cd7 | 141 | inboundMsgsData[typeBit] = inboundMsg>>7 & 0x1FF; |
WD40andTape | 26:7c59002c9cd7 | 142 | if( !CheckMessage(inboundMsg) ) { |
WD40andTape | 26:7c59002c9cd7 | 143 | isSuccess = false; |
WD40andTape | 26:7c59002c9cd7 | 144 | } |
WD40andTape | 26:7c59002c9cd7 | 145 | } |
WD40andTape | 26:7c59002c9cd7 | 146 | } |
WD40andTape | 26:7c59002c9cd7 | 147 | return isSuccess; |
WD40andTape | 24:bc852aa89e7a | 148 | } |
WD40andTape | 24:bc852aa89e7a | 149 | |
WD40andTape | 10:1b6daba32452 | 150 | void LLComms::SendReceiveData(int channel) { |
WD40andTape | 24:bc852aa89e7a | 151 | mutChannel[channel].lock(); // Lock mutex for specific Channel |
WD40andTape | 24:bc852aa89e7a | 152 | |
WD40andTape | 24:bc852aa89e7a | 153 | // Construct messages |
WD40andTape | 26:7c59002c9cd7 | 154 | unsigned int intPositionMsg = formatMessage(0,demandPosition_mm[channel],MAX_ACTUATOR_LIMIT_MM); |
WD40andTape | 26:7c59002c9cd7 | 155 | unsigned int intVelocityMsg = formatMessage(1,demandSpeed_mmps[channel],MAX_SPEED_MMPS); |
dofydoink | 42:5a5ad23a4bb1 | 156 | //unsigned int rawMsg[3]; |
WD40andTape | 26:7c59002c9cd7 | 157 | *cs_LL[channel] = 0; // Select relevant chip |
WD40andTape | 26:7c59002c9cd7 | 158 | unsigned int outboundMsgs[2] = { intPositionMsg , intVelocityMsg }; |
WD40andTape | 27:6853ee8ffefd | 159 | unsigned int inboundMsgsData[2] = { 0 }; |
WD40andTape | 26:7c59002c9cd7 | 160 | bool isSPIsuccess = false; |
dofydoink | 42:5a5ad23a4bb1 | 161 | if( channel < 3 ) { |
WD40andTape | 26:7c59002c9cd7 | 162 | isSPIsuccess = PerformMasterSPI(&spi_0,outboundMsgs,inboundMsgsData); |
dofydoink | 42:5a5ad23a4bb1 | 163 | } //else { |
dofydoink | 42:5a5ad23a4bb1 | 164 | // isSPIsuccess = PerformMasterSPI(&spi_1,outboundMsgs,inboundMsgsData,rawMsg); |
dofydoink | 42:5a5ad23a4bb1 | 165 | // } |
dofydoink | 42:5a5ad23a4bb1 | 166 | // else if( channel < 7 ) { |
dofydoink | 42:5a5ad23a4bb1 | 167 | // isSPIsuccess = PerformMasterSPI(&spi_1,outboundMsgs,inboundMsgsData); |
dofydoink | 42:5a5ad23a4bb1 | 168 | // } else { |
dofydoink | 42:5a5ad23a4bb1 | 169 | // isSPIsuccess = PerformMasterSPI(&spi_2,outboundMsgs,inboundMsgsData); |
dofydoink | 42:5a5ad23a4bb1 | 170 | // } |
WD40andTape | 26:7c59002c9cd7 | 171 | *cs_LL[channel] = 1; // Deselect chip |
WD40andTape | 26:7c59002c9cd7 | 172 | if( isSPIsuccess ) { |
WD40andTape | 26:7c59002c9cd7 | 173 | isDataReady[channel] = 0; // Data no longer ready, i.e. we now require new data |
WD40andTape | 29:10a5cf37a875 | 174 | positionSensor_uint[channel] = inboundMsgsData[0]; |
WD40andTape | 27:6853ee8ffefd | 175 | positionSensor_mm[channel] = ((double)inboundMsgsData[0]/511) * (double)MAX_ACTUATOR_LENGTH_MM; |
WD40andTape | 27:6853ee8ffefd | 176 | positionSensor_mm[channel] = min( max(positionSensor_mm[channel],0.0) , (double)MAX_ACTUATOR_LENGTH_MM ); |
WD40andTape | 29:10a5cf37a875 | 177 | pressureSensor_uint[channel] = inboundMsgsData[1]; |
WD40andTape | 27:6853ee8ffefd | 178 | pressureSensor_bar[channel] = ((double)inboundMsgsData[1]/511) * (double)MAX_PRESSURE_LIMIT; |
WD40andTape | 27:6853ee8ffefd | 179 | pressureSensor_bar[channel] = min( max(pressureSensor_bar[channel],0.0) , (double)MAX_PRESSURE_LIMIT ); |
WD40andTape | 26:7c59002c9cd7 | 180 | } else { // Data is STILL ready and will be resent at the next pin interrupt |
dofydoink | 42:5a5ad23a4bb1 | 181 | printf("SPI failed: %d. Resending.\n\r",channel); |
WD40andTape | 26:7c59002c9cd7 | 182 | } |
WD40andTape | 26:7c59002c9cd7 | 183 | |
WD40andTape | 26:7c59002c9cd7 | 184 | mutChannel[channel].unlock();//unlock mutex for specific channel |
dofydoink | 42:5a5ad23a4bb1 | 185 | |
dofydoink | 42:5a5ad23a4bb1 | 186 | // if(channel ==3){ |
dofydoink | 42:5a5ad23a4bb1 | 187 | // printf("%x\t%x\t%x\r\n",rawMsg[0], rawMsg[1], rawMsg[2] ); |
dofydoink | 42:5a5ad23a4bb1 | 188 | // } |
dofydoink | 42:5a5ad23a4bb1 | 189 | |
dofydoink | 42:5a5ad23a4bb1 | 190 | // printf("%d, ", channel); |
dofydoink | 42:5a5ad23a4bb1 | 191 | // if(channel == 3){ |
dofydoink | 42:5a5ad23a4bb1 | 192 | // printf("\r\n"); |
dofydoink | 42:5a5ad23a4bb1 | 193 | // } |
WD40andTape | 26:7c59002c9cd7 | 194 | } |
WD40andTape | 26:7c59002c9cd7 | 195 | |
WD40andTape | 10:1b6daba32452 | 196 | // Common rise handler function |
WD40andTape | 10:1b6daba32452 | 197 | void LLComms::common_rise_handler(int channel) { |
WD40andTape | 26:7c59002c9cd7 | 198 | //printf("%d %d common_rise_handler\n\r",channel,isDataReady[channel]); |
dofydoink | 42:5a5ad23a4bb1 | 199 | //countervar++; |
WD40andTape | 26:7c59002c9cd7 | 200 | if (isDataReady[channel]) { // Check if data is ready for transmission |
WD40andTape | 10:1b6daba32452 | 201 | ThreadID[channel] = queue.call(this,&LLComms::SendReceiveData,channel); // Schedule transmission |
WD40andTape | 10:1b6daba32452 | 202 | } |
WD40andTape | 10:1b6daba32452 | 203 | } |
WD40andTape | 10:1b6daba32452 | 204 | |
WD40andTape | 10:1b6daba32452 | 205 | // Common fall handler functions |
WD40andTape | 10:1b6daba32452 | 206 | void LLComms::common_fall_handler(int channel) { |
WD40andTape | 10:1b6daba32452 | 207 | queue.cancel(ThreadID[channel]); // Cancel relevant queued event |
WD40andTape | 10:1b6daba32452 | 208 | } |
WD40andTape | 10:1b6daba32452 | 209 | |
WD40andTape | 10:1b6daba32452 | 210 | // Stub rise functions |
WD40andTape | 10:1b6daba32452 | 211 | void LLComms::rise0(void) { common_rise_handler(0); } |
WD40andTape | 10:1b6daba32452 | 212 | void LLComms::rise1(void) { common_rise_handler(1); } |
WD40andTape | 10:1b6daba32452 | 213 | void LLComms::rise2(void) { common_rise_handler(2); } |
dofydoink | 42:5a5ad23a4bb1 | 214 | //void LLComms::rise3(void) { common_rise_handler(3); } |
dofydoink | 42:5a5ad23a4bb1 | 215 | |
WD40andTape | 10:1b6daba32452 | 216 | // Stub fall functions |
WD40andTape | 10:1b6daba32452 | 217 | void LLComms::fall0(void) { common_fall_handler(0); } |
WD40andTape | 10:1b6daba32452 | 218 | void LLComms::fall1(void) { common_fall_handler(1); } |
WD40andTape | 10:1b6daba32452 | 219 | void LLComms::fall2(void) { common_fall_handler(2); } |
dofydoink | 42:5a5ad23a4bb1 | 220 | //void LLComms::fall3(void) { common_fall_handler(3); } |