![](/media/cache/group/11700697_10200701668420946_8214886085951071495_o.jpg.50x50_q85.jpg)
โปรแกรมของบอร์ด Motion
Dependencies: BEAR_Reciever Motor eeprom iSerial mbed
Fork of DogPID by
main.cpp@30:3f8e86fa1413, 2016-01-27 (annotated)
- Committer:
- b0ssiz
- Date:
- Wed Jan 27 21:43:48 2016 +0000
- Revision:
- 30:3f8e86fa1413
- Parent:
- 29:5db0a9261161
- Child:
- 31:d6fa5e8e26b3
Fixed Bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ParinyaT | 0:451c27e4d55e | 1 | //*****************************************************/ |
b0ssiz | 6:98871feebea0 | 2 | // Include // |
ParinyaT | 0:451c27e4d55e | 3 | #include "mbed.h" |
ParinyaT | 0:451c27e4d55e | 4 | #include "pinconfig.h" |
ParinyaT | 0:451c27e4d55e | 5 | #include "PID.h" |
ParinyaT | 0:451c27e4d55e | 6 | #include "Motor.h" |
ParinyaT | 0:451c27e4d55e | 7 | #include "eeprom.h" |
b0ssiz | 6:98871feebea0 | 8 | #include "Receiver.h" |
b0ssiz | 22:449f31da2d3d | 9 | #include "Motion_EEPROM_Address.h" |
ParinyaT | 0:451c27e4d55e | 10 | |
ParinyaT | 0:451c27e4d55e | 11 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 12 | //--PID parameter-- |
ParinyaT | 1:84167ca00307 | 13 | //-Upper-// |
b0ssiz | 10:3b3d6bc88677 | 14 | float U_Kc; |
b0ssiz | 30:3f8e86fa1413 | 15 | float U_Ki_gain; |
b0ssiz | 30:3f8e86fa1413 | 16 | float U_Kd_gain; |
b0ssiz | 10:3b3d6bc88677 | 17 | float U_Ti; |
b0ssiz | 10:3b3d6bc88677 | 18 | float U_Td; |
ParinyaT | 16:c0a1daeb9fa5 | 19 | float U_Ki=U_Kc*U_Ti; |
ParinyaT | 16:c0a1daeb9fa5 | 20 | float U_Kd=U_Kc*U_Td; |
ParinyaT | 1:84167ca00307 | 21 | //-lower-// |
b0ssiz | 10:3b3d6bc88677 | 22 | float L_Kc; |
b0ssiz | 30:3f8e86fa1413 | 23 | float L_Ki_gain; |
b0ssiz | 30:3f8e86fa1413 | 24 | float L_Kd_gain; |
b0ssiz | 10:3b3d6bc88677 | 25 | float L_Ti; |
b0ssiz | 10:3b3d6bc88677 | 26 | float L_Td; |
ParinyaT | 16:c0a1daeb9fa5 | 27 | float L_Ki=L_Kc*L_Ti; |
ParinyaT | 16:c0a1daeb9fa5 | 28 | float L_Kd=L_Kc*L_Td; |
ParinyaT | 0:451c27e4d55e | 29 | //*****************************************************/ |
ParinyaT | 0:451c27e4d55e | 30 | // Global // |
ParinyaT | 11:3dd92d1d542c | 31 | Ticker Recieve; |
ParinyaT | 1:84167ca00307 | 32 | //-- Communication -- |
ParinyaT | 0:451c27e4d55e | 33 | Serial PC(D1,D0); |
b0ssiz | 17:4c96838e579f | 34 | Bear_Receiver com(Tx,Rx,1000000); |
b0ssiz | 30:3f8e86fa1413 | 35 | int16_t MY_ID = 0x02; |
ParinyaT | 11:3dd92d1d542c | 36 | //-- Memorry -- |
ParinyaT | 11:3dd92d1d542c | 37 | EEPROM memory(PB_4,PA_8,0); |
b0ssiz | 14:28e24fcc5a01 | 38 | uint8_t UpMargin[4]; |
b0ssiz | 14:28e24fcc5a01 | 39 | uint8_t LowMargin[4]; |
ParinyaT | 13:49cb002ad8fd | 40 | uint8_t Height[4]; |
ParinyaT | 13:49cb002ad8fd | 41 | uint8_t Wheelpos[4]; |
ParinyaT | 13:49cb002ad8fd | 42 | uint8_t Mag[24]; |
soulx | 26:fbccc263a588 | 43 | uint8_t Offset[8];//={1,2,3,4,5,6,7,8}; |
b0ssiz | 14:28e24fcc5a01 | 44 | uint8_t Body_width[4]; |
ParinyaT | 16:c0a1daeb9fa5 | 45 | uint8_t Angle_Range_Up[8]; |
ParinyaT | 16:c0a1daeb9fa5 | 46 | uint8_t Angle_Range_Low[8]; |
b0ssiz | 22:449f31da2d3d | 47 | uint8_t UpLinkLength[4]; |
b0ssiz | 22:449f31da2d3d | 48 | uint8_t LowLinkLength[4]; |
b0ssiz | 6:98871feebea0 | 49 | //-- encoder -- |
ParinyaT | 13:49cb002ad8fd | 50 | float up_angle,low_angle; |
ParinyaT | 1:84167ca00307 | 51 | int Upper_Position; |
ParinyaT | 1:84167ca00307 | 52 | int Lower_Position; |
ParinyaT | 0:451c27e4d55e | 53 | int data; |
ParinyaT | 1:84167ca00307 | 54 | SPI ENC(Emosi, Emiso, Esck); |
ParinyaT | 1:84167ca00307 | 55 | DigitalOut EncA(EncoderA); |
ParinyaT | 1:84167ca00307 | 56 | DigitalOut EncB(EncoderB); |
ParinyaT | 0:451c27e4d55e | 57 | //-- Motor -- |
ParinyaT | 1:84167ca00307 | 58 | int dir; |
ParinyaT | 1:84167ca00307 | 59 | Motor Upper(PWM_LU,A_LU,B_LU); |
ParinyaT | 1:84167ca00307 | 60 | Motor Lower(PWM_LL,A_LL,B_LL); |
ParinyaT | 0:451c27e4d55e | 61 | //-- PID -- |
ParinyaT | 1:84167ca00307 | 62 | int Upper_SetPoint; |
ParinyaT | 1:84167ca00307 | 63 | int Lower_SetPoint; |
ParinyaT | 1:84167ca00307 | 64 | PID Up_PID(U_Kc, U_Ti, U_Td, 0.001);//Kp,Ki,Kd,Rate |
ParinyaT | 1:84167ca00307 | 65 | PID Low_PID(L_Kc, L_Ti, L_Td, 0.001); |
ParinyaT | 0:451c27e4d55e | 66 | //*****************************************************/ |
soulx | 20:7e6d56655336 | 67 | |
soulx | 20:7e6d56655336 | 68 | DigitalOut myled(LED1); |
soulx | 20:7e6d56655336 | 69 | |
soulx | 20:7e6d56655336 | 70 | |
ParinyaT | 1:84167ca00307 | 71 | void Read_Encoder(PinName Encoder) |
ParinyaT | 0:451c27e4d55e | 72 | { |
soulx | 7:bf239d051e8c | 73 | ENC.format(8,0); |
soulx | 7:bf239d051e8c | 74 | ENC.frequency(200000);//due to rising time,have to decrease clock from 1M - 240k |
b0ssiz | 6:98871feebea0 | 75 | |
soulx | 7:bf239d051e8c | 76 | if(Encoder == EncoderA) { |
soulx | 7:bf239d051e8c | 77 | EncA = 0; |
soulx | 7:bf239d051e8c | 78 | } else { |
soulx | 7:bf239d051e8c | 79 | EncB = 0; |
soulx | 7:bf239d051e8c | 80 | } |
soulx | 7:bf239d051e8c | 81 | ENC.write(0x41); |
soulx | 7:bf239d051e8c | 82 | ENC.write(0x09); |
soulx | 7:bf239d051e8c | 83 | data = ENC.write(0x00); |
soulx | 7:bf239d051e8c | 84 | if(Encoder == EncoderA) { |
soulx | 7:bf239d051e8c | 85 | EncA = 1; |
soulx | 7:bf239d051e8c | 86 | } else { |
soulx | 7:bf239d051e8c | 87 | EncB = 1; |
soulx | 7:bf239d051e8c | 88 | } |
b0ssiz | 6:98871feebea0 | 89 | |
ParinyaT | 0:451c27e4d55e | 90 | } |
ParinyaT | 0:451c27e4d55e | 91 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 92 | int Get_EnValue(int Val) |
ParinyaT | 0:451c27e4d55e | 93 | { |
ParinyaT | 0:451c27e4d55e | 94 | int i = 0; |
ParinyaT | 0:451c27e4d55e | 95 | static unsigned char codes[] = { |
b0ssiz | 6:98871feebea0 | 96 | 127, 63, 62, 58, 56, 184, 152, 24, 8, 72, 73, 77, 79, 15, 47, 175, |
b0ssiz | 6:98871feebea0 | 97 | 191, 159, 31, 29, 28, 92, 76, 12, 4, 36, 164, 166, 167, 135, 151, 215, |
b0ssiz | 6:98871feebea0 | 98 | 223, 207, 143, 142, 14, 46, 38, 6, 2, 18, 82, 83, 211, 195, 203, 235, |
b0ssiz | 6:98871feebea0 | 99 | 239, 231, 199, 71, 7, 23, 19, 3, 1, 9, 41, 169, 233, 225, 229, 245, |
b0ssiz | 6:98871feebea0 | 100 | 247, 243, 227, 163, 131, 139, 137, 129, 128, 132, 148, 212, 244, 240, 242, 250, |
b0ssiz | 6:98871feebea0 | 101 | 251, 249, 241, 209, 193, 197, 196, 192, 64, 66, 74, 106, 122, 120, 121, 125, |
b0ssiz | 6:98871feebea0 | 102 | 253, 252, 248, 232, 224, 226, 98, 96, 32, 33, 37, 53, 61, 60, 188, 190, |
b0ssiz | 6:98871feebea0 | 103 | 254, 126, 124, 116, 112, 113, 49, 48, 16, 144, 146, 154, 158, 30, 94, 95 |
b0ssiz | 6:98871feebea0 | 104 | }; |
b0ssiz | 6:98871feebea0 | 105 | |
b0ssiz | 6:98871feebea0 | 106 | while(Val != codes[i]) { |
ParinyaT | 1:84167ca00307 | 107 | i++; |
ParinyaT | 0:451c27e4d55e | 108 | } |
b0ssiz | 6:98871feebea0 | 109 | |
ParinyaT | 1:84167ca00307 | 110 | return i; |
b0ssiz | 6:98871feebea0 | 111 | |
ParinyaT | 0:451c27e4d55e | 112 | } |
ParinyaT | 0:451c27e4d55e | 113 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 114 | void SET_UpperPID() |
ParinyaT | 1:84167ca00307 | 115 | { |
ParinyaT | 1:84167ca00307 | 116 | Upper.period(0.001); |
ParinyaT | 1:84167ca00307 | 117 | Up_PID.setMode(0); |
ParinyaT | 1:84167ca00307 | 118 | Up_PID.setInputLimits(0,127); |
ParinyaT | 1:84167ca00307 | 119 | Up_PID.setOutputLimits(0,1); |
ParinyaT | 1:84167ca00307 | 120 | } |
ParinyaT | 1:84167ca00307 | 121 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 122 | void SET_LowerPID() |
ParinyaT | 1:84167ca00307 | 123 | { |
ParinyaT | 1:84167ca00307 | 124 | Lower.period(0.001); |
ParinyaT | 1:84167ca00307 | 125 | Low_PID.setMode(0); |
b0ssiz | 10:3b3d6bc88677 | 126 | Low_PID.setInputLimits(0,127); // set range |
ParinyaT | 1:84167ca00307 | 127 | Low_PID.setOutputLimits(0,1); |
ParinyaT | 1:84167ca00307 | 128 | } |
b0ssiz | 6:98871feebea0 | 129 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 130 | void Move_Upper() |
ParinyaT | 1:84167ca00307 | 131 | { |
ParinyaT | 1:84167ca00307 | 132 | Read_Encoder(EncoderA); |
ParinyaT | 1:84167ca00307 | 133 | Upper_Position = Get_EnValue(data); |
ParinyaT | 1:84167ca00307 | 134 | |
ParinyaT | 1:84167ca00307 | 135 | Up_PID.setProcessValue(Upper_Position); |
b0ssiz | 6:98871feebea0 | 136 | |
soulx | 7:bf239d051e8c | 137 | if(Upper_Position - Upper_SetPoint > 0 ) { |
ParinyaT | 1:84167ca00307 | 138 | dir = 1; |
b0ssiz | 6:98871feebea0 | 139 | } |
soulx | 7:bf239d051e8c | 140 | if(Upper_Position - Upper_SetPoint < 0 ) { |
ParinyaT | 1:84167ca00307 | 141 | dir = -1; |
b0ssiz | 6:98871feebea0 | 142 | } |
soulx | 7:bf239d051e8c | 143 | Upper.speed(Up_PID.compute() * dir); |
ParinyaT | 1:84167ca00307 | 144 | } |
ParinyaT | 1:84167ca00307 | 145 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 146 | void Move_Lower() |
ParinyaT | 1:84167ca00307 | 147 | { |
ParinyaT | 1:84167ca00307 | 148 | Read_Encoder(EncoderB); |
ParinyaT | 1:84167ca00307 | 149 | Lower_Position = Get_EnValue(data); |
ParinyaT | 1:84167ca00307 | 150 | |
ParinyaT | 1:84167ca00307 | 151 | Low_PID.setProcessValue(Lower_Position); |
b0ssiz | 6:98871feebea0 | 152 | |
b0ssiz | 6:98871feebea0 | 153 | if(Lower_Position - Lower_SetPoint > 0 ) { |
ParinyaT | 1:84167ca00307 | 154 | dir = 1; |
b0ssiz | 6:98871feebea0 | 155 | } |
b0ssiz | 6:98871feebea0 | 156 | if(Lower_Position - Lower_SetPoint < 0 ) { |
ParinyaT | 1:84167ca00307 | 157 | dir = -1; |
b0ssiz | 6:98871feebea0 | 158 | } |
soulx | 7:bf239d051e8c | 159 | Lower.speed(Low_PID.compute() * dir); |
b0ssiz | 6:98871feebea0 | 160 | } |
ParinyaT | 1:84167ca00307 | 161 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 162 | |
b0ssiz | 30:3f8e86fa1413 | 163 | void CmdCheck(int16_t id,uint8_t *command,uint8_t ins) |
b0ssiz | 10:3b3d6bc88677 | 164 | { |
b0ssiz | 14:28e24fcc5a01 | 165 | if(id==MY_ID) { |
b0ssiz | 14:28e24fcc5a01 | 166 | switch (ins) { |
b0ssiz | 14:28e24fcc5a01 | 167 | case PING: { |
soulx | 18:face01c94152 | 168 | break; |
b0ssiz | 14:28e24fcc5a01 | 169 | } |
b0ssiz | 14:28e24fcc5a01 | 170 | case WRITE_DATA: { |
b0ssiz | 30:3f8e86fa1413 | 171 | switch (command[0]) { |
b0ssiz | 14:28e24fcc5a01 | 172 | case ID: { |
soulx | 18:face01c94152 | 173 | /// |
b0ssiz | 30:3f8e86fa1413 | 174 | MY_ID = (int16_t)command[1]; |
soulx | 18:face01c94152 | 175 | break; |
b0ssiz | 14:28e24fcc5a01 | 176 | } |
b0ssiz | 14:28e24fcc5a01 | 177 | case MOTOR_UPPER_ANG: { |
b0ssiz | 14:28e24fcc5a01 | 178 | uint8_t IntUpAngle[2],FloatUpAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 179 | uint8_t IntLowAngle[2],FloatLowAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 180 | float int_buffer,float_buffer; |
b0ssiz | 14:28e24fcc5a01 | 181 | |
b0ssiz | 30:3f8e86fa1413 | 182 | IntUpAngle[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 183 | IntUpAngle[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 184 | FloatUpAngle[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 185 | FloatUpAngle[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 186 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntUpAngle); |
b0ssiz | 14:28e24fcc5a01 | 187 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatUpAngle)/FLOAT_CONVERTER; |
b0ssiz | 14:28e24fcc5a01 | 188 | up_angle=int_buffer+float_buffer; |
b0ssiz | 30:3f8e86fa1413 | 189 | //printf("Up Angle = %f\n",up_angle); |
b0ssiz | 30:3f8e86fa1413 | 190 | |
b0ssiz | 30:3f8e86fa1413 | 191 | IntLowAngle[0]=command[5]; |
b0ssiz | 30:3f8e86fa1413 | 192 | IntLowAngle[1]=command[6]; |
b0ssiz | 30:3f8e86fa1413 | 193 | FloatLowAngle[0]=command[7]; |
b0ssiz | 30:3f8e86fa1413 | 194 | FloatLowAngle[1]=command[8]; |
b0ssiz | 14:28e24fcc5a01 | 195 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntLowAngle); |
b0ssiz | 14:28e24fcc5a01 | 196 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatLowAngle)/FLOAT_CONVERTER; |
soulx | 18:face01c94152 | 197 | low_angle=int_buffer+float_buffer; |
b0ssiz | 30:3f8e86fa1413 | 198 | //printf("Low Angle = %f\n",low_angle); |
soulx | 18:face01c94152 | 199 | break; |
b0ssiz | 14:28e24fcc5a01 | 200 | } |
b0ssiz | 14:28e24fcc5a01 | 201 | case UP_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 202 | int i; |
b0ssiz | 14:28e24fcc5a01 | 203 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 204 | UpMargin[i]=command[1+i]; |
b0ssiz | 28:b3509fd32b00 | 205 | //printf("UPMARGIN[%d]=0x%02x\n\r",i,UpMargin[i]); |
b0ssiz | 14:28e24fcc5a01 | 206 | } |
soulx | 18:face01c94152 | 207 | break; |
b0ssiz | 14:28e24fcc5a01 | 208 | } |
b0ssiz | 14:28e24fcc5a01 | 209 | case LOW_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 210 | int i; |
b0ssiz | 14:28e24fcc5a01 | 211 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 212 | LowMargin[i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 213 | } |
soulx | 18:face01c94152 | 214 | break; |
b0ssiz | 14:28e24fcc5a01 | 215 | } |
b0ssiz | 14:28e24fcc5a01 | 216 | case KP_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 217 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 218 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 219 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 220 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 221 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 222 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 223 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 224 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 225 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 226 | U_Kc=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 227 | //printf("Kp Upper : %f\r\n",U_Kc); |
soulx | 18:face01c94152 | 228 | break; |
b0ssiz | 14:28e24fcc5a01 | 229 | } |
b0ssiz | 14:28e24fcc5a01 | 230 | case KI_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 231 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 232 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 233 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 234 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 235 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 236 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 237 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 238 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 239 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 240 | U_Ki_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 241 | U_Ti=U_Ki_gain/U_Kc; |
b0ssiz | 30:3f8e86fa1413 | 242 | //printf("Ki Upper : %f\r\n",U_Ki_gain); |
soulx | 18:face01c94152 | 243 | break; |
b0ssiz | 14:28e24fcc5a01 | 244 | } |
b0ssiz | 14:28e24fcc5a01 | 245 | case KD_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 246 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 247 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 248 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 249 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 250 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 251 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 252 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 253 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 254 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 255 | U_Kd_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 256 | U_Td=U_Kd_gain/U_Kc; |
b0ssiz | 30:3f8e86fa1413 | 257 | //printf("Kd Upper : %f\r\n",U_Kd_gain); |
soulx | 18:face01c94152 | 258 | break; |
b0ssiz | 14:28e24fcc5a01 | 259 | } |
b0ssiz | 14:28e24fcc5a01 | 260 | case KP_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 261 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 262 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 263 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 264 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 265 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 266 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 267 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 268 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 269 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 270 | L_Kc=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 271 | //printf("Kp Lower : %f\r\n",L_Kc); |
soulx | 18:face01c94152 | 272 | break; |
b0ssiz | 14:28e24fcc5a01 | 273 | } |
b0ssiz | 14:28e24fcc5a01 | 274 | case KI_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 275 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 276 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 277 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 278 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 279 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 280 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 281 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 282 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 283 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 284 | L_Ki_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 285 | L_Ti=L_Ki_gain/L_Kc; |
b0ssiz | 30:3f8e86fa1413 | 286 | //printf("Ki Lower : %f\r\n",L_Ki_gain); |
soulx | 18:face01c94152 | 287 | break; |
b0ssiz | 14:28e24fcc5a01 | 288 | } |
b0ssiz | 14:28e24fcc5a01 | 289 | case KD_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 290 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 291 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 292 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 293 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 294 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 295 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 296 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 297 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 298 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 299 | L_Kd_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 300 | L_Td=L_Kd_gain/L_Kc; |
b0ssiz | 30:3f8e86fa1413 | 301 | //printf("Kd Lower : %f\r\n",L_Kd_gain); |
soulx | 18:face01c94152 | 302 | break; |
b0ssiz | 14:28e24fcc5a01 | 303 | } |
b0ssiz | 14:28e24fcc5a01 | 304 | case HEIGHT: { |
b0ssiz | 14:28e24fcc5a01 | 305 | int i; |
b0ssiz | 14:28e24fcc5a01 | 306 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 307 | Height[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 308 | } |
soulx | 18:face01c94152 | 309 | break; |
b0ssiz | 14:28e24fcc5a01 | 310 | } |
b0ssiz | 14:28e24fcc5a01 | 311 | case WHEELPOS: { |
b0ssiz | 14:28e24fcc5a01 | 312 | int i; |
b0ssiz | 14:28e24fcc5a01 | 313 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 314 | Wheelpos[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 315 | } |
soulx | 18:face01c94152 | 316 | break; |
b0ssiz | 14:28e24fcc5a01 | 317 | } |
b0ssiz | 14:28e24fcc5a01 | 318 | case MAG_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 319 | int i; |
b0ssiz | 14:28e24fcc5a01 | 320 | for(i=0; i<24; i++) { |
b0ssiz | 30:3f8e86fa1413 | 321 | Mag[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 322 | } |
soulx | 18:face01c94152 | 323 | break; |
b0ssiz | 14:28e24fcc5a01 | 324 | } |
b0ssiz | 14:28e24fcc5a01 | 325 | case OFFSET: { |
b0ssiz | 14:28e24fcc5a01 | 326 | int i; |
b0ssiz | 14:28e24fcc5a01 | 327 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 328 | Offset[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 329 | } |
soulx | 18:face01c94152 | 330 | break; |
b0ssiz | 14:28e24fcc5a01 | 331 | } |
b0ssiz | 14:28e24fcc5a01 | 332 | case BODY_WIDTH: { |
b0ssiz | 14:28e24fcc5a01 | 333 | int i; |
b0ssiz | 14:28e24fcc5a01 | 334 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 335 | Body_width[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 336 | } |
soulx | 18:face01c94152 | 337 | break; |
b0ssiz | 14:28e24fcc5a01 | 338 | } |
b0ssiz | 14:28e24fcc5a01 | 339 | case ANGLE_RANGE_UP: { |
b0ssiz | 14:28e24fcc5a01 | 340 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 341 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 342 | Angle_Range_Up[i]=command[1+i]; |
b0ssiz | 30:3f8e86fa1413 | 343 | //printf("%d Angle = 0x%02x\r\n",i,Angle_Range_Up[i]); |
b0ssiz | 14:28e24fcc5a01 | 344 | } |
soulx | 18:face01c94152 | 345 | break; |
b0ssiz | 14:28e24fcc5a01 | 346 | } |
b0ssiz | 14:28e24fcc5a01 | 347 | case ANGLE_RANGE_LOW: { |
b0ssiz | 14:28e24fcc5a01 | 348 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 349 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 350 | Angle_Range_Low[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 351 | } |
soulx | 18:face01c94152 | 352 | break; |
ParinyaT | 13:49cb002ad8fd | 353 | } |
b0ssiz | 22:449f31da2d3d | 354 | |
b0ssiz | 22:449f31da2d3d | 355 | case UP_LINK_LENGTH: { |
b0ssiz | 22:449f31da2d3d | 356 | int i; |
b0ssiz | 22:449f31da2d3d | 357 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 358 | UpLinkLength[i]=command[1+i]; |
b0ssiz | 22:449f31da2d3d | 359 | } |
b0ssiz | 22:449f31da2d3d | 360 | break; |
b0ssiz | 22:449f31da2d3d | 361 | } |
b0ssiz | 22:449f31da2d3d | 362 | case LOW_LINK_LENGTH: { |
b0ssiz | 22:449f31da2d3d | 363 | int i; |
b0ssiz | 22:449f31da2d3d | 364 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 365 | LowLinkLength[i]=command[1+i]; |
b0ssiz | 22:449f31da2d3d | 366 | } |
b0ssiz | 22:449f31da2d3d | 367 | break; |
b0ssiz | 22:449f31da2d3d | 368 | } |
ParinyaT | 16:c0a1daeb9fa5 | 369 | // unfinish yet!!!!!!!!!!!!!!!!! |
ParinyaT | 16:c0a1daeb9fa5 | 370 | case SAVE_EEPROM_DATA: { |
b0ssiz | 22:449f31da2d3d | 371 | if(id==0x01) { |
b0ssiz | 22:449f31da2d3d | 372 | |
b0ssiz | 30:3f8e86fa1413 | 373 | if (command[1]==HEIGHT) { |
b0ssiz | 22:449f31da2d3d | 374 | int32_t data_buff; |
b0ssiz | 22:449f31da2d3d | 375 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Height); |
b0ssiz | 22:449f31da2d3d | 376 | memory.write(ADDRESS_HEIGHT,data_buff); |
b0ssiz | 28:b3509fd32b00 | 377 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 378 | |
b0ssiz | 30:3f8e86fa1413 | 379 | } else if(command[1]==BODY_WIDTH) { |
b0ssiz | 22:449f31da2d3d | 380 | int32_t data_buff; |
b0ssiz | 22:449f31da2d3d | 381 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Body_width); |
b0ssiz | 22:449f31da2d3d | 382 | memory.write(ADDRESS_BODY_WIDTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 383 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 384 | |
b0ssiz | 30:3f8e86fa1413 | 385 | } else if(command[1]==OFFSET) { |
b0ssiz | 22:449f31da2d3d | 386 | uint8_t y_offset_array[4]; |
b0ssiz | 22:449f31da2d3d | 387 | uint8_t z_offset_array[4]; |
b0ssiz | 22:449f31da2d3d | 388 | int32_t y_data_buffer,z_data_buffer; |
b0ssiz | 22:449f31da2d3d | 389 | for(int i=0; i<4; i++) { |
b0ssiz | 22:449f31da2d3d | 390 | y_offset_array[i]=Offset[i]; |
b0ssiz | 22:449f31da2d3d | 391 | z_offset_array[i]=Offset[i+4]; |
b0ssiz | 22:449f31da2d3d | 392 | } |
b0ssiz | 22:449f31da2d3d | 393 | y_data_buffer = Utilities::ConvertUInt8ArrayToInt32(y_offset_array); |
b0ssiz | 22:449f31da2d3d | 394 | z_data_buffer = Utilities::ConvertUInt8ArrayToInt32(z_offset_array); |
b0ssiz | 22:449f31da2d3d | 395 | memory.write(ADDRESS_OFFSET,y_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 396 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 397 | memory.write(ADDRESS_OFFSET+4,z_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 398 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 399 | |
b0ssiz | 30:3f8e86fa1413 | 400 | } else if(command[1]==MAG_DATA) { |
b0ssiz | 22:449f31da2d3d | 401 | uint8_t x_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 402 | uint8_t x_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 403 | uint8_t y_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 404 | uint8_t y_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 405 | uint8_t z_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 406 | uint8_t z_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 407 | int32_t x_max_buffer,x_min_buffer,y_max_buffer,y_min_buffer,z_max_buffer,z_min_buffer; |
b0ssiz | 22:449f31da2d3d | 408 | for(int i=0; i<4; i++) { |
b0ssiz | 22:449f31da2d3d | 409 | x_max_array[i]=Mag[i]; |
b0ssiz | 22:449f31da2d3d | 410 | x_min_array[i]=Mag[i+4]; |
b0ssiz | 22:449f31da2d3d | 411 | y_max_array[i]=Mag[i+8]; |
b0ssiz | 22:449f31da2d3d | 412 | y_min_array[i]=Mag[i+12]; |
b0ssiz | 22:449f31da2d3d | 413 | z_max_array[i]=Mag[i+16]; |
b0ssiz | 22:449f31da2d3d | 414 | z_min_array[i]=Mag[i+20]; |
b0ssiz | 22:449f31da2d3d | 415 | } |
b0ssiz | 22:449f31da2d3d | 416 | x_max_buffer = Utilities::ConvertUInt8ArrayToInt32(x_max_array); |
b0ssiz | 22:449f31da2d3d | 417 | x_min_buffer = Utilities::ConvertUInt8ArrayToInt32(x_min_array); |
b0ssiz | 22:449f31da2d3d | 418 | y_max_buffer = Utilities::ConvertUInt8ArrayToInt32(y_max_array); |
b0ssiz | 22:449f31da2d3d | 419 | y_min_buffer = Utilities::ConvertUInt8ArrayToInt32(y_min_array); |
b0ssiz | 22:449f31da2d3d | 420 | z_max_buffer = Utilities::ConvertUInt8ArrayToInt32(z_max_array); |
b0ssiz | 22:449f31da2d3d | 421 | z_min_buffer = Utilities::ConvertUInt8ArrayToInt32(z_min_array); |
b0ssiz | 22:449f31da2d3d | 422 | memory.write(ADDRESS_MAG_DATA,x_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 423 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 424 | memory.write(ADDRESS_MAG_DATA+4,x_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 425 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 426 | memory.write(ADDRESS_MAG_DATA+8,y_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 427 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 428 | memory.write(ADDRESS_MAG_DATA+12,y_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 429 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 430 | memory.write(ADDRESS_MAG_DATA+16,z_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 431 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 432 | memory.write(ADDRESS_MAG_DATA+20,z_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 433 | wait_ms(1); |
b0ssiz | 28:b3509fd32b00 | 434 | |
b0ssiz | 22:449f31da2d3d | 435 | } |
b0ssiz | 22:449f31da2d3d | 436 | |
soulx | 27:718fc94e40ad | 437 | } |
soulx | 27:718fc94e40ad | 438 | // else { |
b0ssiz | 30:3f8e86fa1413 | 439 | if (command[1]==ID) { |
soulx | 27:718fc94e40ad | 440 | memory.write(ADDRESS_ID,id); |
b0ssiz | 28:b3509fd32b00 | 441 | wait_ms(1); |
soulx | 18:face01c94152 | 442 | |
b0ssiz | 30:3f8e86fa1413 | 443 | } else if(command[1]==UP_MARGIN) { |
soulx | 27:718fc94e40ad | 444 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 445 | data_buff = Utilities::ConvertUInt8ArrayToInt32(UpMargin); |
soulx | 27:718fc94e40ad | 446 | memory.write(ADDRESS_UP_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 447 | wait_ms(1); |
b0ssiz | 28:b3509fd32b00 | 448 | //printf("save OK!!\n\r"); |
b0ssiz | 22:449f31da2d3d | 449 | |
b0ssiz | 30:3f8e86fa1413 | 450 | } else if (command[1]==LOW_MARGIN) { |
soulx | 27:718fc94e40ad | 451 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 452 | data_buff = Utilities::ConvertUInt8ArrayToInt32(LowMargin); |
soulx | 27:718fc94e40ad | 453 | memory.write(ADDRESS_LOW_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 454 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 455 | |
b0ssiz | 30:3f8e86fa1413 | 456 | } else if (command[1]==PID_UPPER_MOTOR) { |
soulx | 27:718fc94e40ad | 457 | memory.write(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 458 | //printf("U_Write : %f\r\n",U_Kc); |
b0ssiz | 28:b3509fd32b00 | 459 | wait_ms(1); |
b0ssiz | 30:3f8e86fa1413 | 460 | memory.write(ADDRESS_UPPER_KI,U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 461 | //printf("U_Write : %f\r\n",U_Ki_gain); |
b0ssiz | 28:b3509fd32b00 | 462 | wait_ms(1); |
b0ssiz | 30:3f8e86fa1413 | 463 | memory.write(ADDRESS_UPPER_KD,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 464 | //printf("U_Write : %f\r\n",U_Kd_gain); |
b0ssiz | 28:b3509fd32b00 | 465 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 466 | |
b0ssiz | 30:3f8e86fa1413 | 467 | } else if (command[1]==PID_LOWER_MOTOR) { |
soulx | 27:718fc94e40ad | 468 | memory.write(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 469 | //printf("L_Write : %f\r\n",L_Kc); |
b0ssiz | 28:b3509fd32b00 | 470 | wait_ms(1); |
b0ssiz | 30:3f8e86fa1413 | 471 | memory.write(ADDRESS_LOWER_KI,L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 472 | //printf("L_Write : %f\r\n",L_Ki_gain); |
b0ssiz | 28:b3509fd32b00 | 473 | wait_ms(1); |
b0ssiz | 30:3f8e86fa1413 | 474 | memory.write(ADDRESS_LOWER_KD,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 475 | //printf("L_Write : %f\r\n",L_Kd_gain); |
b0ssiz | 28:b3509fd32b00 | 476 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 477 | |
b0ssiz | 30:3f8e86fa1413 | 478 | } else if (command[1]==ANGLE_RANGE_UP) { |
soulx | 27:718fc94e40ad | 479 | uint8_t max_array[4]; |
soulx | 27:718fc94e40ad | 480 | uint8_t min_array[4]; |
soulx | 27:718fc94e40ad | 481 | int32_t max_data_buffer,min_data_buffer; |
soulx | 27:718fc94e40ad | 482 | for(int i=0; i<4; i++) { |
soulx | 27:718fc94e40ad | 483 | max_array[i]=Angle_Range_Up[i]; |
soulx | 27:718fc94e40ad | 484 | min_array[i]=Angle_Range_Up[i+4]; |
soulx | 27:718fc94e40ad | 485 | } |
soulx | 27:718fc94e40ad | 486 | max_data_buffer = Utilities::ConvertUInt8ArrayToInt32(max_array); |
soulx | 27:718fc94e40ad | 487 | min_data_buffer = Utilities::ConvertUInt8ArrayToInt32(min_array); |
soulx | 27:718fc94e40ad | 488 | memory.write(ADDRESS_ANGLE_RANGE_UP,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 489 | wait_ms(1); |
soulx | 27:718fc94e40ad | 490 | memory.write(ADDRESS_ANGLE_RANGE_UP+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 491 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 492 | |
b0ssiz | 30:3f8e86fa1413 | 493 | } else if (command[1]==ANGLE_RANGE_LOW) { |
soulx | 27:718fc94e40ad | 494 | uint8_t max_array[4]; |
soulx | 27:718fc94e40ad | 495 | uint8_t min_array[4]; |
soulx | 27:718fc94e40ad | 496 | int32_t max_data_buffer,min_data_buffer; |
soulx | 27:718fc94e40ad | 497 | for(int i=0; i<4; i++) { |
soulx | 27:718fc94e40ad | 498 | max_array[i]=Angle_Range_Low[i]; |
soulx | 27:718fc94e40ad | 499 | min_array[i]=Angle_Range_Low[i+4]; |
soulx | 27:718fc94e40ad | 500 | } |
soulx | 27:718fc94e40ad | 501 | max_data_buffer = Utilities::ConvertUInt8ArrayToInt32(max_array); |
soulx | 27:718fc94e40ad | 502 | min_data_buffer = Utilities::ConvertUInt8ArrayToInt32(min_array); |
soulx | 27:718fc94e40ad | 503 | memory.write(ADDRESS_ANGLE_RANGE_LOW,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 504 | wait_ms(1); |
soulx | 27:718fc94e40ad | 505 | memory.write(ADDRESS_ANGLE_RANGE_LOW+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 506 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 507 | |
b0ssiz | 30:3f8e86fa1413 | 508 | } else if (command[1]==UP_LINK_LENGTH) { |
soulx | 27:718fc94e40ad | 509 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 510 | data_buff = Utilities::ConvertUInt8ArrayToInt32(UpLinkLength); |
soulx | 27:718fc94e40ad | 511 | memory.write(ADDRESS_UP_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 512 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 513 | |
b0ssiz | 30:3f8e86fa1413 | 514 | } else if (command[1]==LOW_LINK_LENGTH) { |
soulx | 27:718fc94e40ad | 515 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 516 | data_buff = Utilities::ConvertUInt8ArrayToInt32(LowLinkLength); |
soulx | 27:718fc94e40ad | 517 | memory.write(ADDRESS_LOW_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 518 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 519 | |
b0ssiz | 30:3f8e86fa1413 | 520 | } else if (command[1]==WHEELPOS) { |
soulx | 27:718fc94e40ad | 521 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 522 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Wheelpos); |
soulx | 27:718fc94e40ad | 523 | memory.write(ADDRESS_WHEELPOS,data_buff); |
b0ssiz | 28:b3509fd32b00 | 524 | wait_ms(1); |
b0ssiz | 22:449f31da2d3d | 525 | } |
soulx | 27:718fc94e40ad | 526 | break; |
ParinyaT | 13:49cb002ad8fd | 527 | } |
b0ssiz | 28:b3509fd32b00 | 528 | break; |
b0ssiz | 28:b3509fd32b00 | 529 | } |
b0ssiz | 28:b3509fd32b00 | 530 | break; |
b0ssiz | 28:b3509fd32b00 | 531 | } |
b0ssiz | 28:b3509fd32b00 | 532 | case READ_DATA: { |
b0ssiz | 30:3f8e86fa1413 | 533 | switch (command[0]) { |
b0ssiz | 28:b3509fd32b00 | 534 | case MOTOR_UPPER_ANG: { |
b0ssiz | 30:3f8e86fa1413 | 535 | com.sendMotorPos(MY_ID,up_angle,low_angle); |
b0ssiz | 28:b3509fd32b00 | 536 | break; |
b0ssiz | 28:b3509fd32b00 | 537 | } |
b0ssiz | 28:b3509fd32b00 | 538 | case UP_MARGIN: { |
b0ssiz | 28:b3509fd32b00 | 539 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 540 | memory.read(ADDRESS_UP_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 541 | Utilities::ConvertInt32ToUInt8Array(data_buff,UpMargin); |
b0ssiz | 28:b3509fd32b00 | 542 | com.sendUpMargin(MY_ID,UpMargin); |
b0ssiz | 28:b3509fd32b00 | 543 | break; |
b0ssiz | 28:b3509fd32b00 | 544 | } |
b0ssiz | 28:b3509fd32b00 | 545 | case LOW_MARGIN: { |
b0ssiz | 28:b3509fd32b00 | 546 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 547 | memory.read(ADDRESS_LOW_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 548 | Utilities::ConvertInt32ToUInt8Array(data_buff,LowMargin); |
b0ssiz | 28:b3509fd32b00 | 549 | com.sendLowMargin(MY_ID,LowMargin); |
b0ssiz | 28:b3509fd32b00 | 550 | break; |
b0ssiz | 28:b3509fd32b00 | 551 | } |
b0ssiz | 28:b3509fd32b00 | 552 | case PID_UPPER_MOTOR: { |
b0ssiz | 28:b3509fd32b00 | 553 | memory.read(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 554 | memory.read(ADDRESS_UPPER_KI,U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 555 | memory.read(ADDRESS_UPPER_KD,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 556 | com.sendUpMotorKpKiKd(MY_ID,U_Kc,U_Ki_gain,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 557 | /* |
b0ssiz | 30:3f8e86fa1413 | 558 | printf("After read Kp : %f\r\n",U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 559 | printf("After read Ki : %f\r\n",U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 560 | printf("After read Kd : %f\r\n",U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 561 | */ |
b0ssiz | 28:b3509fd32b00 | 562 | break; |
b0ssiz | 28:b3509fd32b00 | 563 | } |
b0ssiz | 28:b3509fd32b00 | 564 | case PID_LOWER_MOTOR: { |
b0ssiz | 28:b3509fd32b00 | 565 | memory.read(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 566 | memory.read(ADDRESS_LOWER_KI,L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 567 | memory.read(ADDRESS_LOWER_KD,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 568 | com.sendLowMotorKpKiKd(MY_ID,L_Kc,L_Ki_gain,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 569 | /* |
b0ssiz | 30:3f8e86fa1413 | 570 | printf("After read L_Kp : %f\r\n",L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 571 | printf("After read L_Ki : %f\r\n",L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 572 | printf("After read L_Kd : %f\r\n",L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 573 | */ |
b0ssiz | 28:b3509fd32b00 | 574 | break; |
b0ssiz | 28:b3509fd32b00 | 575 | } |
b0ssiz | 28:b3509fd32b00 | 576 | case HEIGHT: { |
b0ssiz | 28:b3509fd32b00 | 577 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 578 | memory.read(ADDRESS_HEIGHT,data_buff); |
b0ssiz | 28:b3509fd32b00 | 579 | Utilities::ConvertInt32ToUInt8Array(data_buff,Height); |
b0ssiz | 28:b3509fd32b00 | 580 | com.sendHeight(MY_ID,Height); |
b0ssiz | 28:b3509fd32b00 | 581 | break; |
b0ssiz | 28:b3509fd32b00 | 582 | } |
b0ssiz | 28:b3509fd32b00 | 583 | case WHEELPOS: { |
b0ssiz | 28:b3509fd32b00 | 584 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 585 | memory.read(ADDRESS_WHEELPOS,data_buff); |
b0ssiz | 28:b3509fd32b00 | 586 | Utilities::ConvertInt32ToUInt8Array(data_buff,Wheelpos); |
b0ssiz | 28:b3509fd32b00 | 587 | com.sendWheelPos(MY_ID,Wheelpos); |
b0ssiz | 28:b3509fd32b00 | 588 | break; |
b0ssiz | 28:b3509fd32b00 | 589 | } |
b0ssiz | 28:b3509fd32b00 | 590 | case MAG_DATA: { |
b0ssiz | 28:b3509fd32b00 | 591 | uint8_t x_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 592 | uint8_t x_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 593 | uint8_t y_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 594 | uint8_t y_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 595 | uint8_t z_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 596 | uint8_t z_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 597 | int32_t x_max_buffer,x_min_buffer,y_max_buffer,y_min_buffer,z_max_buffer,z_min_buffer; |
b0ssiz | 28:b3509fd32b00 | 598 | memory.read(ADDRESS_MAG_DATA,x_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 599 | memory.read(ADDRESS_MAG_DATA+4,x_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 600 | memory.read(ADDRESS_MAG_DATA+8,y_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 601 | memory.read(ADDRESS_MAG_DATA+12,y_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 602 | memory.read(ADDRESS_MAG_DATA+16,z_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 603 | memory.read(ADDRESS_MAG_DATA+20,z_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 604 | Utilities::ConvertInt32ToUInt8Array(x_max_buffer,x_max_array); |
b0ssiz | 28:b3509fd32b00 | 605 | Utilities::ConvertInt32ToUInt8Array(x_min_buffer,x_min_array); |
b0ssiz | 28:b3509fd32b00 | 606 | Utilities::ConvertInt32ToUInt8Array(y_max_buffer,y_max_array); |
b0ssiz | 28:b3509fd32b00 | 607 | Utilities::ConvertInt32ToUInt8Array(y_min_buffer,y_min_array); |
b0ssiz | 28:b3509fd32b00 | 608 | Utilities::ConvertInt32ToUInt8Array(z_max_buffer,z_max_array); |
b0ssiz | 28:b3509fd32b00 | 609 | Utilities::ConvertInt32ToUInt8Array(z_min_buffer,z_min_array); |
b0ssiz | 28:b3509fd32b00 | 610 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 611 | Mag[i]=x_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 612 | Mag[i+4]=x_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 613 | Mag[i+8]=y_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 614 | Mag[i+12]=y_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 615 | Mag[i+16]=z_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 616 | Mag[i+20]=z_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 617 | } |
b0ssiz | 28:b3509fd32b00 | 618 | com.sendMagData(MY_ID,Mag); |
b0ssiz | 28:b3509fd32b00 | 619 | break; |
b0ssiz | 28:b3509fd32b00 | 620 | } |
b0ssiz | 28:b3509fd32b00 | 621 | case OFFSET: { |
b0ssiz | 28:b3509fd32b00 | 622 | uint8_t y_offset_array[4]; |
b0ssiz | 28:b3509fd32b00 | 623 | uint8_t z_offset_array[4]; |
b0ssiz | 28:b3509fd32b00 | 624 | int32_t y_data_buffer,z_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 625 | memory.read(ADDRESS_OFFSET,y_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 626 | memory.read(ADDRESS_OFFSET+4,z_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 627 | Utilities::ConvertInt32ToUInt8Array(y_data_buffer,y_offset_array); |
b0ssiz | 28:b3509fd32b00 | 628 | Utilities::ConvertInt32ToUInt8Array(z_data_buffer,z_offset_array); |
b0ssiz | 28:b3509fd32b00 | 629 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 630 | Offset[i]=y_offset_array[i]; |
b0ssiz | 28:b3509fd32b00 | 631 | Offset[i+4]=z_offset_array[i]; |
b0ssiz | 28:b3509fd32b00 | 632 | } |
b0ssiz | 28:b3509fd32b00 | 633 | com.sendOffset(MY_ID,Offset); |
b0ssiz | 28:b3509fd32b00 | 634 | break; |
b0ssiz | 28:b3509fd32b00 | 635 | } |
b0ssiz | 28:b3509fd32b00 | 636 | case BODY_WIDTH: { |
b0ssiz | 28:b3509fd32b00 | 637 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 638 | memory.read(ADDRESS_BODY_WIDTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 639 | Utilities::ConvertInt32ToUInt8Array(data_buff,Body_width); |
b0ssiz | 28:b3509fd32b00 | 640 | com.sendBodyWidth(MY_ID,Body_width); |
b0ssiz | 28:b3509fd32b00 | 641 | break; |
b0ssiz | 28:b3509fd32b00 | 642 | } |
b0ssiz | 28:b3509fd32b00 | 643 | case ANGLE_RANGE_UP: { |
b0ssiz | 28:b3509fd32b00 | 644 | uint8_t max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 645 | uint8_t min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 646 | int32_t max_data_buffer,min_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 647 | memory.read(ADDRESS_ANGLE_RANGE_UP,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 648 | memory.read(ADDRESS_ANGLE_RANGE_UP+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 649 | Utilities::ConvertInt32ToUInt8Array(max_data_buffer,max_array); |
b0ssiz | 28:b3509fd32b00 | 650 | Utilities::ConvertInt32ToUInt8Array(min_data_buffer,min_array); |
b0ssiz | 28:b3509fd32b00 | 651 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 652 | Angle_Range_Up[i]=max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 653 | Angle_Range_Up[i+4]=min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 654 | } |
b0ssiz | 28:b3509fd32b00 | 655 | com.sendUpAngleRange(MY_ID,Angle_Range_Up); |
b0ssiz | 28:b3509fd32b00 | 656 | break; |
b0ssiz | 28:b3509fd32b00 | 657 | } |
b0ssiz | 28:b3509fd32b00 | 658 | case ANGLE_RANGE_LOW: { |
b0ssiz | 28:b3509fd32b00 | 659 | uint8_t max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 660 | uint8_t min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 661 | int32_t max_data_buffer,min_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 662 | memory.read(ADDRESS_ANGLE_RANGE_LOW,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 663 | memory.read(ADDRESS_ANGLE_RANGE_LOW+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 664 | Utilities::ConvertInt32ToUInt8Array(max_data_buffer,max_array); |
b0ssiz | 28:b3509fd32b00 | 665 | Utilities::ConvertInt32ToUInt8Array(min_data_buffer,min_array); |
b0ssiz | 28:b3509fd32b00 | 666 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 667 | Angle_Range_Low[i]=max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 668 | Angle_Range_Low[i+4]=min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 669 | } |
b0ssiz | 28:b3509fd32b00 | 670 | com.sendLowAngleRange(MY_ID,Angle_Range_Low); |
b0ssiz | 28:b3509fd32b00 | 671 | break; |
b0ssiz | 28:b3509fd32b00 | 672 | } |
b0ssiz | 28:b3509fd32b00 | 673 | case UP_LINK_LENGTH: { |
b0ssiz | 28:b3509fd32b00 | 674 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 675 | memory.read(ADDRESS_UP_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 676 | Utilities::ConvertInt32ToUInt8Array(data_buff,UpLinkLength); |
b0ssiz | 28:b3509fd32b00 | 677 | com.sendUpLinkLength(MY_ID,UpLinkLength); |
b0ssiz | 28:b3509fd32b00 | 678 | break; |
b0ssiz | 28:b3509fd32b00 | 679 | } |
b0ssiz | 28:b3509fd32b00 | 680 | case LOW_LINK_LENGTH: { |
b0ssiz | 28:b3509fd32b00 | 681 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 682 | memory.read(ADDRESS_LOW_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 683 | Utilities::ConvertInt32ToUInt8Array(data_buff,LowLinkLength); |
b0ssiz | 28:b3509fd32b00 | 684 | com.sendLowLinkLength(MY_ID,LowLinkLength); |
b0ssiz | 28:b3509fd32b00 | 685 | break; |
b0ssiz | 28:b3509fd32b00 | 686 | } |
b0ssiz | 28:b3509fd32b00 | 687 | break; |
ParinyaT | 13:49cb002ad8fd | 688 | } |
ParinyaT | 13:49cb002ad8fd | 689 | } |
b0ssiz | 10:3b3d6bc88677 | 690 | } |
b0ssiz | 14:28e24fcc5a01 | 691 | } |
b0ssiz | 10:3b3d6bc88677 | 692 | } |
b0ssiz | 17:4c96838e579f | 693 | |
b0ssiz | 28:b3509fd32b00 | 694 | |
ParinyaT | 11:3dd92d1d542c | 695 | /******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 696 | void Start_Up() |
ParinyaT | 11:3dd92d1d542c | 697 | { |
ParinyaT | 11:3dd92d1d542c | 698 | // wait for reciever |
b0ssiz | 22:449f31da2d3d | 699 | memory.read(ADDRESS_ID,MY_ID); |
b0ssiz | 22:449f31da2d3d | 700 | memory.read(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 22:449f31da2d3d | 701 | memory.read(ADDRESS_UPPER_KI,U_Ti); |
b0ssiz | 22:449f31da2d3d | 702 | memory.read(ADDRESS_UPPER_KD,U_Td); |
b0ssiz | 22:449f31da2d3d | 703 | memory.read(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 22:449f31da2d3d | 704 | memory.read(ADDRESS_LOWER_KI,L_Ti); |
b0ssiz | 22:449f31da2d3d | 705 | memory.read(ADDRESS_LOWER_KD,L_Td); |
b0ssiz | 14:28e24fcc5a01 | 706 | |
ParinyaT | 11:3dd92d1d542c | 707 | } |
b0ssiz | 14:28e24fcc5a01 | 708 | /*******************************************************/ |
soulx | 27:718fc94e40ad | 709 | void Rc() |
ParinyaT | 11:3dd92d1d542c | 710 | { |
soulx | 21:1c04c4afe3b7 | 711 | myled =1; |
b0ssiz | 17:4c96838e579f | 712 | uint8_t data_array[30]; |
soulx | 27:718fc94e40ad | 713 | uint8_t id=0; |
soulx | 27:718fc94e40ad | 714 | uint8_t ins=0; |
soulx | 27:718fc94e40ad | 715 | uint8_t status=0xFF; |
b0ssiz | 14:28e24fcc5a01 | 716 | |
soulx | 27:718fc94e40ad | 717 | |
soulx | 20:7e6d56655336 | 718 | |
soulx | 18:face01c94152 | 719 | status = com.ReceiveCommand(&id,data_array,&ins); |
soulx | 29:5db0a9261161 | 720 | //PC.printf("status = 0x%02x\n\r",status); |
soulx | 18:face01c94152 | 721 | if(status == ANDANTE_ERRBIT_NONE) { |
soulx | 18:face01c94152 | 722 | CmdCheck((int16_t)id,data_array,ins); |
soulx | 18:face01c94152 | 723 | } |
soulx | 27:718fc94e40ad | 724 | |
ParinyaT | 11:3dd92d1d542c | 725 | } |
ParinyaT | 11:3dd92d1d542c | 726 | /*******************************************************/ |
ParinyaT | 0:451c27e4d55e | 727 | int main() |
ParinyaT | 0:451c27e4d55e | 728 | { |
soulx | 29:5db0a9261161 | 729 | PC.baud(115200); |
soulx | 27:718fc94e40ad | 730 | |
soulx | 21:1c04c4afe3b7 | 731 | //Recieve.attach(&Rc,0.025); |
soulx | 20:7e6d56655336 | 732 | //Start_Up(); |
b0ssiz | 23:999dd41eef14 | 733 | |
ParinyaT | 1:84167ca00307 | 734 | SET_UpperPID(); |
ParinyaT | 1:84167ca00307 | 735 | SET_LowerPID(); |
ParinyaT | 11:3dd92d1d542c | 736 | |
soulx | 29:5db0a9261161 | 737 | printf("BEAR MOTION\n\r"); |
b0ssiz | 14:28e24fcc5a01 | 738 | while(1) { |
soulx | 27:718fc94e40ad | 739 | myled =0; |
soulx | 27:718fc94e40ad | 740 | //wait_ms(10); |
soulx | 27:718fc94e40ad | 741 | |
soulx | 20:7e6d56655336 | 742 | ///////////////////////////////////////////////////// start |
b0ssiz | 14:28e24fcc5a01 | 743 | //Set Set_point |
b0ssiz | 14:28e24fcc5a01 | 744 | Up_PID.setSetPoint(Upper_SetPoint); |
b0ssiz | 14:28e24fcc5a01 | 745 | Low_PID.setSetPoint(Lower_SetPoint); |
ParinyaT | 1:84167ca00307 | 746 | |
b0ssiz | 14:28e24fcc5a01 | 747 | //Control Motor |
b0ssiz | 14:28e24fcc5a01 | 748 | Move_Upper(); |
b0ssiz | 14:28e24fcc5a01 | 749 | Move_Lower(); |
soulx | 20:7e6d56655336 | 750 | ///////////////////////////////////////////////////// stop =306us |
soulx | 21:1c04c4afe3b7 | 751 | //uint8_t aaa[1]={10}; |
soulx | 21:1c04c4afe3b7 | 752 | //com.sendBodyWidth(0x01,aaa); |
soulx | 21:1c04c4afe3b7 | 753 | Rc(); |
soulx | 21:1c04c4afe3b7 | 754 | //wait_ms(1); |
soulx | 20:7e6d56655336 | 755 | } |
ParinyaT | 0:451c27e4d55e | 756 | } |
b0ssiz | 6:98871feebea0 | 757 | |
ParinyaT | 0:451c27e4d55e | 758 | |
ParinyaT | 0:451c27e4d55e | 759 | |
b0ssiz | 6:98871feebea0 | 760 | |
b0ssiz | 6:98871feebea0 | 761 | |
b0ssiz | 6:98871feebea0 | 762 |