Basic Mid-Level control for the rebuilt MorphGI control unit, using PWM to communicate with the low level controllers.

Dependencies:   ros_lib_kinetic

Committer:
WD40andTape
Date:
Tue Dec 11 15:45:01 2018 +0000
Revision:
21:0b10d8e615d1
Parent:
14:54c3759e76ed
Child:
22:82871f00f89d
Updates to main to move path following to LL

Who changed what in which revision?

UserRevisionLine numberNew 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 8:d6657767a182 7 pinGate6(PE_11),
WD40andTape 14:54c3759e76ed 8 spi_0(PC_12, PC_11, PC_10),
WD40andTape 14:54c3759e76ed 9 spi_1(PF_9, PF_8, PF_7),
WD40andTape 8:d6657767a182 10 pinCheck(PE_5),
WD40andTape 8:d6657767a182 11 // These interrupt pins have to be declared AFTER SPI declaration. No Clue Why.
WD40andTape 8:d6657767a182 12 pinGate0(PF_11),
WD40andTape 8:d6657767a182 13 pinGate1(PG_14),
WD40andTape 8:d6657767a182 14 pinGate2(PF_15),
WD40andTape 8:d6657767a182 15 pinGate3(PF_12),
WD40andTape 8:d6657767a182 16 pinGate4(PF_3),
WD40andTape 8:d6657767a182 17 pinGate5(PF_13),
WD40andTape 8:d6657767a182 18 //pinGate6(PE_11), // See above nonsense
WD40andTape 8:d6657767a182 19 pinGate7(PE_13),
dofydoink 12:595ed862e52f 20 pinReset(PD_2)
WD40andTape 8:d6657767a182 21 { // Constructor
WD40andTape 8:d6657767a182 22
dofydoink 12:595ed862e52f 23 PinName LLPins[8] = {PD_15, PE_10, PD_11, PD_14, PE_7, PD_12, PF_10, PD_13};
dofydoink 12:595ed862e52f 24 //PinName LLPins[8] = {PD_15, PE_10, PD_14, PD_11, PE_7, PD_12, PF_10, PD_13};
WD40andTape 8:d6657767a182 25 PinName ADCPins[8] = {PG_12, PG_9, PE_1, PG_0, PD_0, PD_1, PF_0, PF_1};
WD40andTape 8:d6657767a182 26 for (short int i = 0; i < 8; i++) {
WD40andTape 10:1b6daba32452 27 isDataReady[i] = 0;
WD40andTape 8:d6657767a182 28 cs_LL[i] = new DigitalOut(LLPins[i]);
WD40andTape 8:d6657767a182 29 cs_ADC[i] = new DigitalOut(ADCPins[i]);
WD40andTape 8:d6657767a182 30 }
WD40andTape 10:1b6daba32452 31
WD40andTape 9:cd3607ba5643 32 // Initialise relevant variables
dofydoink 11:7029367a1840 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 9:cd3607ba5643 36 *cs_ADC[i] = 1;
WD40andTape 9:cd3607ba5643 37 }
WD40andTape 9:cd3607ba5643 38 pinReset = 1; // Initialise reset pin to not reset the controllers.
WD40andTape 9:cd3607ba5643 39 wait(0.25);
WD40andTape 9:cd3607ba5643 40 pinReset=0; // Reset controllers to be safe
WD40andTape 9:cd3607ba5643 41 wait(0.25);
WD40andTape 9:cd3607ba5643 42 pinReset = 1; // Ready to go
WD40andTape 10:1b6daba32452 43
WD40andTape 10:1b6daba32452 44 // Set up rise interrupts MIGHT NOT NEED TO BE POINTERS
dofydoink 12:595ed862e52f 45 pinGate0.rise(callback(this,&LLComms::rise0));
dofydoink 12:595ed862e52f 46 pinGate1.rise(callback(this,&LLComms::rise1));
dofydoink 12:595ed862e52f 47 pinGate2.rise(callback(this,&LLComms::rise2));
dofydoink 12:595ed862e52f 48 pinGate3.rise(callback(this,&LLComms::rise3));
dofydoink 12:595ed862e52f 49 pinGate4.rise(callback(this,&LLComms::rise4));
dofydoink 12:595ed862e52f 50 pinGate5.rise(callback(this,&LLComms::rise5));
dofydoink 12:595ed862e52f 51 pinGate6.rise(callback(this,&LLComms::rise6));
dofydoink 12:595ed862e52f 52 pinGate7.rise(callback(this,&LLComms::rise7));
WD40andTape 10:1b6daba32452 53 // Set up fall interrupts MIGHT NOT NEED TO BE POINTERS
dofydoink 12:595ed862e52f 54 pinGate0.fall(callback(this,&LLComms::fall0));
dofydoink 12:595ed862e52f 55 pinGate1.fall(callback(this,&LLComms::fall1));
dofydoink 12:595ed862e52f 56 pinGate2.fall(callback(this,&LLComms::fall2));
dofydoink 12:595ed862e52f 57 pinGate3.fall(callback(this,&LLComms::fall3));
dofydoink 12:595ed862e52f 58 pinGate4.fall(callback(this,&LLComms::fall4));
dofydoink 12:595ed862e52f 59 pinGate5.fall(callback(this,&LLComms::fall5));
dofydoink 12:595ed862e52f 60 pinGate6.fall(callback(this,&LLComms::fall6));
dofydoink 12:595ed862e52f 61 pinGate7.fall(callback(this,&LLComms::fall7));
WD40andTape 9:cd3607ba5643 62 }
WD40andTape 9:cd3607ba5643 63
dofydoink 11:7029367a1840 64 //LLComms::~LLComms(void) { } // Destructor
dofydoink 11:7029367a1840 65
WD40andTape 10:1b6daba32452 66 void LLComms::SendReceiveData(int channel) {
WD40andTape 13:a373dfc57b89 67 //pinTesty = 1;
dofydoink 11:7029367a1840 68 int intPosSPI_Rx[N_CHANNELS]; // 13 bit value received over SPI from the actuator
WD40andTape 10:1b6daba32452 69
WD40andTape 10:1b6daba32452 70 // Get data from controller
WD40andTape 14:54c3759e76ed 71 if( channel < 4 ) {
WD40andTape 14:54c3759e76ed 72 spi_0.format(16,2); // !! Can probably move to constructor
WD40andTape 14:54c3759e76ed 73 spi_0.frequency(LOW_LEVEL_SPI_FREQUENCY);
WD40andTape 14:54c3759e76ed 74 mutChannel[channel].lock(); // Lock mutex for specific Channel
WD40andTape 14:54c3759e76ed 75 *cs_LL[channel] = 0; // Select relevant chip
WD40andTape 14:54c3759e76ed 76 intPosSPI_Rx[channel] = spi_0.write(demandPosition[channel]); // Transmit & receive
WD40andTape 14:54c3759e76ed 77 *cs_LL[channel] = 1; // Deselect chip
WD40andTape 14:54c3759e76ed 78 } else {
WD40andTape 14:54c3759e76ed 79 spi_1.format(16,2); // !! Can probably move to constructor
WD40andTape 14:54c3759e76ed 80 spi_1.frequency(LOW_LEVEL_SPI_FREQUENCY);
WD40andTape 14:54c3759e76ed 81 mutChannel[channel].lock(); // Lock mutex for specific Channel
WD40andTape 14:54c3759e76ed 82 *cs_LL[channel] = 0; // Select relevant chip
WD40andTape 14:54c3759e76ed 83 intPosSPI_Rx[channel] = spi_1.write(demandPosition[channel]); // Transmit & receive
WD40andTape 14:54c3759e76ed 84 *cs_LL[channel] = 1; // Deselect chip
WD40andTape 14:54c3759e76ed 85 }
WD40andTape 10:1b6daba32452 86 isDataReady[channel] = 0; // Data no longer ready, i.e. we now require new data
WD40andTape 10:1b6daba32452 87 /*if(channel == 0) {
WD40andTape 10:1b6daba32452 88 intGlobalTest = intPosSPI_Rx[channel];
WD40andTape 10:1b6daba32452 89 dblGlobalTest = ((double) (intPosSPI_Rx[channel])/8191.0*52.2);
WD40andTape 10:1b6daba32452 90 }*/
WD40andTape 10:1b6daba32452 91
WD40andTape 10:1b6daba32452 92 // Sort out received data
WD40andTape 10:1b6daba32452 93 chrErrorFlag[channel] = intPosSPI_Rx[channel]>>13;
WD40andTape 10:1b6daba32452 94
WD40andTape 10:1b6daba32452 95 intPosSPI_Rx[channel] = intPosSPI_Rx[channel] & 0x1FFF;
WD40andTape 10:1b6daba32452 96 //dblPosition_mtrs[channel] = (double)intPosSPI_Rx[channel]/8191*(MAX_ACTUATOR_LENGTH/DBL_ACTUATOR_CONVERSION[channel])/1000;
WD40andTape 10:1b6daba32452 97 mutChannel[channel].unlock();//unlock mutex for specific channel
dofydoink 12:595ed862e52f 98 //if(IS_PRINT_OUTPUT) printf("%d, %d\r\n",intPosSPI_Rx[0],_intDemandPos_Tx[0]);
WD40andTape 13:a373dfc57b89 99 //pinTesty = 0;
WD40andTape 10:1b6daba32452 100 }
WD40andTape 10:1b6daba32452 101
WD40andTape 10:1b6daba32452 102 // Common rise handler function
WD40andTape 10:1b6daba32452 103 void LLComms::common_rise_handler(int channel) {
WD40andTape 10:1b6daba32452 104 pinCheck = 1;
WD40andTape 10:1b6daba32452 105 if (isDataReady[channel]) { // Check if data is ready for tranmission
WD40andTape 10:1b6daba32452 106 ThreadID[channel] = queue.call(this,&LLComms::SendReceiveData,channel); // Schedule transmission
WD40andTape 10:1b6daba32452 107 }
WD40andTape 10:1b6daba32452 108 }
WD40andTape 10:1b6daba32452 109
WD40andTape 10:1b6daba32452 110 // Common fall handler functions
WD40andTape 10:1b6daba32452 111 void LLComms::common_fall_handler(int channel) {
WD40andTape 10:1b6daba32452 112 pinCheck = 0;
WD40andTape 10:1b6daba32452 113 queue.cancel(ThreadID[channel]); // Cancel relevant queued event
WD40andTape 10:1b6daba32452 114 }
WD40andTape 10:1b6daba32452 115
WD40andTape 10:1b6daba32452 116 // Stub rise functions
WD40andTape 10:1b6daba32452 117 void LLComms::rise0(void) { common_rise_handler(0); }
WD40andTape 10:1b6daba32452 118 void LLComms::rise1(void) { common_rise_handler(1); }
WD40andTape 10:1b6daba32452 119 void LLComms::rise2(void) { common_rise_handler(2); }
WD40andTape 10:1b6daba32452 120 void LLComms::rise3(void) { common_rise_handler(3); }
WD40andTape 10:1b6daba32452 121 void LLComms::rise4(void) { common_rise_handler(4); }
WD40andTape 10:1b6daba32452 122 void LLComms::rise5(void) { common_rise_handler(5); }
WD40andTape 10:1b6daba32452 123 void LLComms::rise6(void) { common_rise_handler(6); }
WD40andTape 10:1b6daba32452 124 void LLComms::rise7(void) { common_rise_handler(7); }
WD40andTape 10:1b6daba32452 125 // Stub fall functions
WD40andTape 10:1b6daba32452 126 void LLComms::fall0(void) { common_fall_handler(0); }
WD40andTape 10:1b6daba32452 127 void LLComms::fall1(void) { common_fall_handler(1); }
WD40andTape 10:1b6daba32452 128 void LLComms::fall2(void) { common_fall_handler(2); }
WD40andTape 10:1b6daba32452 129 void LLComms::fall3(void) { common_fall_handler(3); }
WD40andTape 10:1b6daba32452 130 void LLComms::fall4(void) { common_fall_handler(4); }
WD40andTape 10:1b6daba32452 131 void LLComms::fall5(void) { common_fall_handler(5); }
WD40andTape 10:1b6daba32452 132 void LLComms::fall6(void) { common_fall_handler(6); }
WD40andTape 10:1b6daba32452 133 void LLComms::fall7(void) { common_fall_handler(7); }
WD40andTape 10:1b6daba32452 134
dofydoink 12:595ed862e52f 135 // NEEDS CALIBRATING
WD40andTape 8:d6657767a182 136 double LLComms::ReadADCPosition_mtrs(int channel) {
WD40andTape 8:d6657767a182 137 unsigned int outputA;
WD40andTape 8:d6657767a182 138 unsigned int outputB;
WD40andTape 8:d6657767a182 139 int output;
WD40andTape 8:d6657767a182 140 double dblOutput;
WD40andTape 8:d6657767a182 141
WD40andTape 14:54c3759e76ed 142 if( channel < 4 ) {
WD40andTape 14:54c3759e76ed 143 spi_0.format(8,0);
WD40andTape 14:54c3759e76ed 144 spi_0.frequency(1000000);
WD40andTape 14:54c3759e76ed 145
WD40andTape 14:54c3759e76ed 146 *cs_ADC[channel] = 0;
WD40andTape 14:54c3759e76ed 147 spi_0.write(PREAMBLE);
WD40andTape 14:54c3759e76ed 148 outputA = spi_0.write(CHAN_3);
WD40andTape 14:54c3759e76ed 149 outputB = spi_0.write(0xFF);
WD40andTape 14:54c3759e76ed 150 *cs_ADC[channel] = 1;
WD40andTape 14:54c3759e76ed 151 } else {
WD40andTape 14:54c3759e76ed 152 spi_1.format(8,0);
WD40andTape 14:54c3759e76ed 153 spi_1.frequency(1000000);
WD40andTape 14:54c3759e76ed 154
WD40andTape 14:54c3759e76ed 155 *cs_ADC[channel] = 0;
WD40andTape 14:54c3759e76ed 156 spi_1.write(PREAMBLE);
WD40andTape 14:54c3759e76ed 157 outputA = spi_1.write(CHAN_3);
WD40andTape 14:54c3759e76ed 158 outputB = spi_1.write(0xFF);
WD40andTape 14:54c3759e76ed 159 *cs_ADC[channel] = 1;
WD40andTape 14:54c3759e76ed 160 }
WD40andTape 8:d6657767a182 161
WD40andTape 8:d6657767a182 162 outputA = outputA & DATA_MASK;
WD40andTape 8:d6657767a182 163 outputA = outputA<<8;
WD40andTape 8:d6657767a182 164 output = (outputA | outputB);
WD40andTape 8:d6657767a182 165 output = 4095- output;
WD40andTape 8:d6657767a182 166 dblOutput = (double) (output);
WD40andTape 8:d6657767a182 167 dblOutput = dblOutput*0.0229 - 21.582;
WD40andTape 8:d6657767a182 168 return dblOutput;
WD40andTape 8:d6657767a182 169 }
WD40andTape 8:d6657767a182 170
WD40andTape 8:d6657767a182 171 double LLComms::ReadADCPressure_bar(int channel) {
WD40andTape 8:d6657767a182 172 unsigned int outputA;
WD40andTape 8:d6657767a182 173 unsigned int outputB;
WD40andTape 8:d6657767a182 174 int output;
WD40andTape 8:d6657767a182 175 double dblOutput;
WD40andTape 8:d6657767a182 176
WD40andTape 14:54c3759e76ed 177 if( channel < 4 ) {
WD40andTape 14:54c3759e76ed 178 spi_0.format(8,0);
WD40andTape 14:54c3759e76ed 179 spi_0.frequency(1000000);
WD40andTape 14:54c3759e76ed 180
WD40andTape 14:54c3759e76ed 181 *cs_ADC[channel] = 0;
WD40andTape 14:54c3759e76ed 182 spi_0.write(PREAMBLE);
WD40andTape 14:54c3759e76ed 183 outputA = spi_0.write(CHAN_1);
WD40andTape 14:54c3759e76ed 184 outputB = spi_0.write(0xFF);
WD40andTape 14:54c3759e76ed 185 *cs_ADC[channel] = 1;
WD40andTape 14:54c3759e76ed 186 } else {
WD40andTape 14:54c3759e76ed 187 spi_1.format(8,0);
WD40andTape 14:54c3759e76ed 188 spi_1.frequency(1000000);
WD40andTape 14:54c3759e76ed 189
WD40andTape 14:54c3759e76ed 190 *cs_ADC[channel] = 0;
WD40andTape 14:54c3759e76ed 191 spi_1.write(PREAMBLE);
WD40andTape 14:54c3759e76ed 192 outputA = spi_1.write(CHAN_1);
WD40andTape 14:54c3759e76ed 193 outputB = spi_1.write(0xFF);
WD40andTape 14:54c3759e76ed 194 *cs_ADC[channel] = 1;
WD40andTape 14:54c3759e76ed 195 }
WD40andTape 8:d6657767a182 196
WD40andTape 8:d6657767a182 197 outputA = outputA & DATA_MASK;
WD40andTape 8:d6657767a182 198 outputA = outputA<<8;
WD40andTape 8:d6657767a182 199 output = (outputA | outputB);
WD40andTape 8:d6657767a182 200
WD40andTape 8:d6657767a182 201 dblOutput = (double)(output);
WD40andTape 8:d6657767a182 202 dblOutput = dblOutput-502.0;
WD40andTape 8:d6657767a182 203 dblOutput = dblOutput/4095.0*8.0;
WD40andTape 8:d6657767a182 204 return dblOutput;
WD40andTape 10:1b6daba32452 205 }