first
Dependencies: BEAR_Reciever Motor eeprom iSerial mbed
Fork of BEAR_Motion by
main.cpp@32:1f81f3e83889, 2016-02-02 (annotated)
- Committer:
- b0ssiz
- Date:
- Tue Feb 02 01:31:15 2016 +0000
- Revision:
- 32:1f81f3e83889
- Parent:
- 31:d6fa5e8e26b3
- Child:
- 33:65cfa4b72195
- Child:
- 34:0cf04acfe422
Fixed bug and Change position variable
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" |
b0ssiz | 31:d6fa5e8e26b3 | 10 | #define EEPROM_DELAY 2 |
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; |
b0ssiz | 31:d6fa5e8e26b3 | 51 | float Upper_Position; |
b0ssiz | 31:d6fa5e8e26b3 | 52 | float 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); |
b0ssiz | 32:1f81f3e83889 | 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); |
b0ssiz | 32:1f81f3e83889 | 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 | 32:1f81f3e83889 | 178 | uint8_t IntUpAngle[2]; |
b0ssiz | 32:1f81f3e83889 | 179 | uint8_t IntLowAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 180 | |
b0ssiz | 30:3f8e86fa1413 | 181 | IntUpAngle[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 182 | IntUpAngle[1]=command[2]; |
b0ssiz | 31:d6fa5e8e26b3 | 183 | Upper_Position=(float)Utilities::ConvertUInt8ArrayToInt16(IntUpAngle); |
b0ssiz | 30:3f8e86fa1413 | 184 | //printf("Up Angle = %f\n",up_angle); |
b0ssiz | 30:3f8e86fa1413 | 185 | IntLowAngle[0]=command[5]; |
b0ssiz | 30:3f8e86fa1413 | 186 | IntLowAngle[1]=command[6]; |
b0ssiz | 31:d6fa5e8e26b3 | 187 | Lower_Position=(float)Utilities::ConvertUInt8ArrayToInt16(IntLowAngle); |
b0ssiz | 30:3f8e86fa1413 | 188 | //printf("Low Angle = %f\n",low_angle); |
soulx | 18:face01c94152 | 189 | break; |
b0ssiz | 14:28e24fcc5a01 | 190 | } |
b0ssiz | 14:28e24fcc5a01 | 191 | case UP_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 192 | int i; |
b0ssiz | 14:28e24fcc5a01 | 193 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 194 | UpMargin[i]=command[1+i]; |
b0ssiz | 28:b3509fd32b00 | 195 | //printf("UPMARGIN[%d]=0x%02x\n\r",i,UpMargin[i]); |
b0ssiz | 14:28e24fcc5a01 | 196 | } |
soulx | 18:face01c94152 | 197 | break; |
b0ssiz | 14:28e24fcc5a01 | 198 | } |
b0ssiz | 14:28e24fcc5a01 | 199 | case LOW_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 200 | int i; |
b0ssiz | 14:28e24fcc5a01 | 201 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 202 | LowMargin[i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 203 | } |
soulx | 18:face01c94152 | 204 | break; |
b0ssiz | 14:28e24fcc5a01 | 205 | } |
b0ssiz | 14:28e24fcc5a01 | 206 | case KP_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 207 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 208 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 209 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 210 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 211 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 212 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 213 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 214 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 215 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 216 | U_Kc=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 217 | //printf("Kp Upper : %f\r\n",U_Kc); |
soulx | 18:face01c94152 | 218 | break; |
b0ssiz | 14:28e24fcc5a01 | 219 | } |
b0ssiz | 14:28e24fcc5a01 | 220 | case KI_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 221 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 222 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 223 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 224 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 225 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 226 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 227 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 228 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 229 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 230 | U_Ki_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 231 | U_Ti=U_Ki_gain/U_Kc; |
b0ssiz | 30:3f8e86fa1413 | 232 | //printf("Ki Upper : %f\r\n",U_Ki_gain); |
soulx | 18:face01c94152 | 233 | break; |
b0ssiz | 14:28e24fcc5a01 | 234 | } |
b0ssiz | 14:28e24fcc5a01 | 235 | case KD_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 236 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 237 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 238 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 239 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 240 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 241 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 242 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 243 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 244 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 245 | U_Kd_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 246 | U_Td=U_Kd_gain/U_Kc; |
b0ssiz | 30:3f8e86fa1413 | 247 | //printf("Kd Upper : %f\r\n",U_Kd_gain); |
soulx | 18:face01c94152 | 248 | break; |
b0ssiz | 14:28e24fcc5a01 | 249 | } |
b0ssiz | 14:28e24fcc5a01 | 250 | case KP_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 251 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 252 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 253 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 254 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 255 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 256 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 257 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 258 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 259 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 260 | L_Kc=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 261 | //printf("Kp Lower : %f\r\n",L_Kc); |
soulx | 18:face01c94152 | 262 | break; |
b0ssiz | 14:28e24fcc5a01 | 263 | } |
b0ssiz | 14:28e24fcc5a01 | 264 | case KI_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 265 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 266 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 267 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 268 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 269 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 270 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 271 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 272 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 273 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 274 | L_Ki_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 275 | L_Ti=L_Ki_gain/L_Kc; |
b0ssiz | 30:3f8e86fa1413 | 276 | //printf("Ki Lower : %f\r\n",L_Ki_gain); |
soulx | 18:face01c94152 | 277 | break; |
b0ssiz | 14:28e24fcc5a01 | 278 | } |
b0ssiz | 14:28e24fcc5a01 | 279 | case KD_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 280 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 281 | uint8_t float_buffer[2]; |
b0ssiz | 30:3f8e86fa1413 | 282 | float Int,Float; |
b0ssiz | 30:3f8e86fa1413 | 283 | int_buffer[0]=command[1]; |
b0ssiz | 30:3f8e86fa1413 | 284 | int_buffer[1]=command[2]; |
b0ssiz | 30:3f8e86fa1413 | 285 | float_buffer[0]=command[3]; |
b0ssiz | 30:3f8e86fa1413 | 286 | float_buffer[1]=command[4]; |
b0ssiz | 14:28e24fcc5a01 | 287 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 288 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 30:3f8e86fa1413 | 289 | L_Kd_gain=Int+Float; |
b0ssiz | 30:3f8e86fa1413 | 290 | L_Td=L_Kd_gain/L_Kc; |
b0ssiz | 30:3f8e86fa1413 | 291 | //printf("Kd Lower : %f\r\n",L_Kd_gain); |
soulx | 18:face01c94152 | 292 | break; |
b0ssiz | 14:28e24fcc5a01 | 293 | } |
b0ssiz | 14:28e24fcc5a01 | 294 | case HEIGHT: { |
b0ssiz | 14:28e24fcc5a01 | 295 | int i; |
b0ssiz | 14:28e24fcc5a01 | 296 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 297 | Height[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 298 | } |
soulx | 18:face01c94152 | 299 | break; |
b0ssiz | 14:28e24fcc5a01 | 300 | } |
b0ssiz | 14:28e24fcc5a01 | 301 | case WHEELPOS: { |
b0ssiz | 14:28e24fcc5a01 | 302 | int i; |
b0ssiz | 14:28e24fcc5a01 | 303 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 304 | Wheelpos[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 305 | } |
soulx | 18:face01c94152 | 306 | break; |
b0ssiz | 14:28e24fcc5a01 | 307 | } |
b0ssiz | 14:28e24fcc5a01 | 308 | case MAG_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 309 | int i; |
b0ssiz | 14:28e24fcc5a01 | 310 | for(i=0; i<24; i++) { |
b0ssiz | 30:3f8e86fa1413 | 311 | Mag[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 312 | } |
soulx | 18:face01c94152 | 313 | break; |
b0ssiz | 14:28e24fcc5a01 | 314 | } |
b0ssiz | 14:28e24fcc5a01 | 315 | case OFFSET: { |
b0ssiz | 14:28e24fcc5a01 | 316 | int i; |
b0ssiz | 14:28e24fcc5a01 | 317 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 318 | Offset[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 319 | } |
soulx | 18:face01c94152 | 320 | break; |
b0ssiz | 14:28e24fcc5a01 | 321 | } |
b0ssiz | 14:28e24fcc5a01 | 322 | case BODY_WIDTH: { |
b0ssiz | 14:28e24fcc5a01 | 323 | int i; |
b0ssiz | 14:28e24fcc5a01 | 324 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 325 | Body_width[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 326 | } |
soulx | 18:face01c94152 | 327 | break; |
b0ssiz | 14:28e24fcc5a01 | 328 | } |
b0ssiz | 14:28e24fcc5a01 | 329 | case ANGLE_RANGE_UP: { |
b0ssiz | 14:28e24fcc5a01 | 330 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 331 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 332 | Angle_Range_Up[i]=command[1+i]; |
b0ssiz | 32:1f81f3e83889 | 333 | //printf("%d Angle = 0x%02x\r\n",i,Angle_Range_Up[i]); |
b0ssiz | 14:28e24fcc5a01 | 334 | } |
soulx | 18:face01c94152 | 335 | break; |
b0ssiz | 14:28e24fcc5a01 | 336 | } |
b0ssiz | 14:28e24fcc5a01 | 337 | case ANGLE_RANGE_LOW: { |
b0ssiz | 14:28e24fcc5a01 | 338 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 339 | for(i=0; i<8; i++) { |
b0ssiz | 30:3f8e86fa1413 | 340 | Angle_Range_Low[0+i]=command[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 341 | } |
soulx | 18:face01c94152 | 342 | break; |
ParinyaT | 13:49cb002ad8fd | 343 | } |
b0ssiz | 22:449f31da2d3d | 344 | |
b0ssiz | 22:449f31da2d3d | 345 | case UP_LINK_LENGTH: { |
b0ssiz | 22:449f31da2d3d | 346 | int i; |
b0ssiz | 22:449f31da2d3d | 347 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 348 | UpLinkLength[i]=command[1+i]; |
b0ssiz | 22:449f31da2d3d | 349 | } |
b0ssiz | 22:449f31da2d3d | 350 | break; |
b0ssiz | 22:449f31da2d3d | 351 | } |
b0ssiz | 22:449f31da2d3d | 352 | case LOW_LINK_LENGTH: { |
b0ssiz | 22:449f31da2d3d | 353 | int i; |
b0ssiz | 22:449f31da2d3d | 354 | for(i=0; i<4; i++) { |
b0ssiz | 30:3f8e86fa1413 | 355 | LowLinkLength[i]=command[1+i]; |
b0ssiz | 22:449f31da2d3d | 356 | } |
b0ssiz | 22:449f31da2d3d | 357 | break; |
b0ssiz | 22:449f31da2d3d | 358 | } |
ParinyaT | 16:c0a1daeb9fa5 | 359 | // unfinish yet!!!!!!!!!!!!!!!!! |
ParinyaT | 16:c0a1daeb9fa5 | 360 | case SAVE_EEPROM_DATA: { |
b0ssiz | 22:449f31da2d3d | 361 | if(id==0x01) { |
b0ssiz | 22:449f31da2d3d | 362 | |
b0ssiz | 30:3f8e86fa1413 | 363 | if (command[1]==HEIGHT) { |
b0ssiz | 22:449f31da2d3d | 364 | int32_t data_buff; |
b0ssiz | 22:449f31da2d3d | 365 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Height); |
b0ssiz | 22:449f31da2d3d | 366 | memory.write(ADDRESS_HEIGHT,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 367 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 368 | |
b0ssiz | 30:3f8e86fa1413 | 369 | } else if(command[1]==BODY_WIDTH) { |
b0ssiz | 22:449f31da2d3d | 370 | int32_t data_buff; |
b0ssiz | 22:449f31da2d3d | 371 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Body_width); |
b0ssiz | 22:449f31da2d3d | 372 | memory.write(ADDRESS_BODY_WIDTH,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 373 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 374 | |
b0ssiz | 30:3f8e86fa1413 | 375 | } else if(command[1]==OFFSET) { |
b0ssiz | 22:449f31da2d3d | 376 | uint8_t y_offset_array[4]; |
b0ssiz | 22:449f31da2d3d | 377 | uint8_t z_offset_array[4]; |
b0ssiz | 22:449f31da2d3d | 378 | int32_t y_data_buffer,z_data_buffer; |
b0ssiz | 22:449f31da2d3d | 379 | for(int i=0; i<4; i++) { |
b0ssiz | 22:449f31da2d3d | 380 | y_offset_array[i]=Offset[i]; |
b0ssiz | 22:449f31da2d3d | 381 | z_offset_array[i]=Offset[i+4]; |
b0ssiz | 22:449f31da2d3d | 382 | } |
b0ssiz | 22:449f31da2d3d | 383 | y_data_buffer = Utilities::ConvertUInt8ArrayToInt32(y_offset_array); |
b0ssiz | 22:449f31da2d3d | 384 | z_data_buffer = Utilities::ConvertUInt8ArrayToInt32(z_offset_array); |
b0ssiz | 22:449f31da2d3d | 385 | memory.write(ADDRESS_OFFSET,y_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 386 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 387 | memory.write(ADDRESS_OFFSET+4,z_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 388 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 389 | |
b0ssiz | 30:3f8e86fa1413 | 390 | } else if(command[1]==MAG_DATA) { |
b0ssiz | 22:449f31da2d3d | 391 | uint8_t x_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 392 | uint8_t x_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 393 | uint8_t y_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 394 | uint8_t y_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 395 | uint8_t z_max_array[4]; |
b0ssiz | 22:449f31da2d3d | 396 | uint8_t z_min_array[4]; |
b0ssiz | 22:449f31da2d3d | 397 | int32_t x_max_buffer,x_min_buffer,y_max_buffer,y_min_buffer,z_max_buffer,z_min_buffer; |
b0ssiz | 22:449f31da2d3d | 398 | for(int i=0; i<4; i++) { |
b0ssiz | 22:449f31da2d3d | 399 | x_max_array[i]=Mag[i]; |
b0ssiz | 22:449f31da2d3d | 400 | x_min_array[i]=Mag[i+4]; |
b0ssiz | 22:449f31da2d3d | 401 | y_max_array[i]=Mag[i+8]; |
b0ssiz | 22:449f31da2d3d | 402 | y_min_array[i]=Mag[i+12]; |
b0ssiz | 22:449f31da2d3d | 403 | z_max_array[i]=Mag[i+16]; |
b0ssiz | 22:449f31da2d3d | 404 | z_min_array[i]=Mag[i+20]; |
b0ssiz | 22:449f31da2d3d | 405 | } |
b0ssiz | 22:449f31da2d3d | 406 | x_max_buffer = Utilities::ConvertUInt8ArrayToInt32(x_max_array); |
b0ssiz | 22:449f31da2d3d | 407 | x_min_buffer = Utilities::ConvertUInt8ArrayToInt32(x_min_array); |
b0ssiz | 22:449f31da2d3d | 408 | y_max_buffer = Utilities::ConvertUInt8ArrayToInt32(y_max_array); |
b0ssiz | 22:449f31da2d3d | 409 | y_min_buffer = Utilities::ConvertUInt8ArrayToInt32(y_min_array); |
b0ssiz | 22:449f31da2d3d | 410 | z_max_buffer = Utilities::ConvertUInt8ArrayToInt32(z_max_array); |
b0ssiz | 22:449f31da2d3d | 411 | z_min_buffer = Utilities::ConvertUInt8ArrayToInt32(z_min_array); |
b0ssiz | 22:449f31da2d3d | 412 | memory.write(ADDRESS_MAG_DATA,x_max_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 413 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 414 | memory.write(ADDRESS_MAG_DATA+4,x_min_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 415 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 416 | memory.write(ADDRESS_MAG_DATA+8,y_max_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 417 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 418 | memory.write(ADDRESS_MAG_DATA+12,y_min_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 419 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 420 | memory.write(ADDRESS_MAG_DATA+16,z_max_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 421 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 422 | memory.write(ADDRESS_MAG_DATA+20,z_min_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 423 | wait_ms(EEPROM_DELAY); |
b0ssiz | 28:b3509fd32b00 | 424 | |
b0ssiz | 22:449f31da2d3d | 425 | } |
b0ssiz | 22:449f31da2d3d | 426 | |
soulx | 27:718fc94e40ad | 427 | } |
soulx | 27:718fc94e40ad | 428 | // else { |
b0ssiz | 30:3f8e86fa1413 | 429 | if (command[1]==ID) { |
soulx | 27:718fc94e40ad | 430 | memory.write(ADDRESS_ID,id); |
b0ssiz | 31:d6fa5e8e26b3 | 431 | wait_ms(EEPROM_DELAY); |
soulx | 18:face01c94152 | 432 | |
b0ssiz | 30:3f8e86fa1413 | 433 | } else if(command[1]==UP_MARGIN) { |
soulx | 27:718fc94e40ad | 434 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 435 | data_buff = Utilities::ConvertUInt8ArrayToInt32(UpMargin); |
soulx | 27:718fc94e40ad | 436 | memory.write(ADDRESS_UP_MARGIN,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 437 | wait_ms(EEPROM_DELAY); |
b0ssiz | 28:b3509fd32b00 | 438 | //printf("save OK!!\n\r"); |
b0ssiz | 22:449f31da2d3d | 439 | |
b0ssiz | 30:3f8e86fa1413 | 440 | } else if (command[1]==LOW_MARGIN) { |
soulx | 27:718fc94e40ad | 441 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 442 | data_buff = Utilities::ConvertUInt8ArrayToInt32(LowMargin); |
soulx | 27:718fc94e40ad | 443 | memory.write(ADDRESS_LOW_MARGIN,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 444 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 445 | |
b0ssiz | 30:3f8e86fa1413 | 446 | } else if (command[1]==PID_UPPER_MOTOR) { |
soulx | 27:718fc94e40ad | 447 | memory.write(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 448 | //printf("U_Write : %f\r\n",U_Kc); |
b0ssiz | 31:d6fa5e8e26b3 | 449 | wait_ms(EEPROM_DELAY); |
b0ssiz | 30:3f8e86fa1413 | 450 | memory.write(ADDRESS_UPPER_KI,U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 451 | //printf("U_Write : %f\r\n",U_Ki_gain); |
b0ssiz | 31:d6fa5e8e26b3 | 452 | wait_ms(EEPROM_DELAY); |
b0ssiz | 30:3f8e86fa1413 | 453 | memory.write(ADDRESS_UPPER_KD,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 454 | //printf("U_Write : %f\r\n",U_Kd_gain); |
b0ssiz | 31:d6fa5e8e26b3 | 455 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 456 | |
b0ssiz | 30:3f8e86fa1413 | 457 | } else if (command[1]==PID_LOWER_MOTOR) { |
soulx | 27:718fc94e40ad | 458 | memory.write(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 459 | //printf("L_Write : %f\r\n",L_Kc); |
b0ssiz | 31:d6fa5e8e26b3 | 460 | wait_ms(EEPROM_DELAY); |
b0ssiz | 30:3f8e86fa1413 | 461 | memory.write(ADDRESS_LOWER_KI,L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 462 | //printf("L_Write : %f\r\n",L_Ki_gain); |
b0ssiz | 31:d6fa5e8e26b3 | 463 | wait_ms(EEPROM_DELAY); |
b0ssiz | 30:3f8e86fa1413 | 464 | memory.write(ADDRESS_LOWER_KD,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 465 | //printf("L_Write : %f\r\n",L_Kd_gain); |
b0ssiz | 31:d6fa5e8e26b3 | 466 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 467 | |
b0ssiz | 30:3f8e86fa1413 | 468 | } else if (command[1]==ANGLE_RANGE_UP) { |
soulx | 27:718fc94e40ad | 469 | uint8_t max_array[4]; |
soulx | 27:718fc94e40ad | 470 | uint8_t min_array[4]; |
soulx | 27:718fc94e40ad | 471 | int32_t max_data_buffer,min_data_buffer; |
soulx | 27:718fc94e40ad | 472 | for(int i=0; i<4; i++) { |
soulx | 27:718fc94e40ad | 473 | max_array[i]=Angle_Range_Up[i]; |
soulx | 27:718fc94e40ad | 474 | min_array[i]=Angle_Range_Up[i+4]; |
soulx | 27:718fc94e40ad | 475 | } |
soulx | 27:718fc94e40ad | 476 | max_data_buffer = Utilities::ConvertUInt8ArrayToInt32(max_array); |
soulx | 27:718fc94e40ad | 477 | min_data_buffer = Utilities::ConvertUInt8ArrayToInt32(min_array); |
soulx | 27:718fc94e40ad | 478 | memory.write(ADDRESS_ANGLE_RANGE_UP,max_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 479 | wait_ms(EEPROM_DELAY); |
soulx | 27:718fc94e40ad | 480 | memory.write(ADDRESS_ANGLE_RANGE_UP+4,min_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 481 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 482 | |
b0ssiz | 30:3f8e86fa1413 | 483 | } else if (command[1]==ANGLE_RANGE_LOW) { |
soulx | 27:718fc94e40ad | 484 | uint8_t max_array[4]; |
soulx | 27:718fc94e40ad | 485 | uint8_t min_array[4]; |
soulx | 27:718fc94e40ad | 486 | int32_t max_data_buffer,min_data_buffer; |
soulx | 27:718fc94e40ad | 487 | for(int i=0; i<4; i++) { |
soulx | 27:718fc94e40ad | 488 | max_array[i]=Angle_Range_Low[i]; |
soulx | 27:718fc94e40ad | 489 | min_array[i]=Angle_Range_Low[i+4]; |
soulx | 27:718fc94e40ad | 490 | } |
soulx | 27:718fc94e40ad | 491 | max_data_buffer = Utilities::ConvertUInt8ArrayToInt32(max_array); |
soulx | 27:718fc94e40ad | 492 | min_data_buffer = Utilities::ConvertUInt8ArrayToInt32(min_array); |
soulx | 27:718fc94e40ad | 493 | memory.write(ADDRESS_ANGLE_RANGE_LOW,max_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 494 | wait_ms(EEPROM_DELAY); |
soulx | 27:718fc94e40ad | 495 | memory.write(ADDRESS_ANGLE_RANGE_LOW+4,min_data_buffer); |
b0ssiz | 31:d6fa5e8e26b3 | 496 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 497 | |
b0ssiz | 30:3f8e86fa1413 | 498 | } else if (command[1]==UP_LINK_LENGTH) { |
soulx | 27:718fc94e40ad | 499 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 500 | data_buff = Utilities::ConvertUInt8ArrayToInt32(UpLinkLength); |
soulx | 27:718fc94e40ad | 501 | memory.write(ADDRESS_UP_LINK_LENGTH,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 502 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 503 | |
b0ssiz | 30:3f8e86fa1413 | 504 | } else if (command[1]==LOW_LINK_LENGTH) { |
soulx | 27:718fc94e40ad | 505 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 506 | data_buff = Utilities::ConvertUInt8ArrayToInt32(LowLinkLength); |
soulx | 27:718fc94e40ad | 507 | memory.write(ADDRESS_LOW_LINK_LENGTH,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 508 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 509 | |
b0ssiz | 30:3f8e86fa1413 | 510 | } else if (command[1]==WHEELPOS) { |
soulx | 27:718fc94e40ad | 511 | int32_t data_buff; |
soulx | 27:718fc94e40ad | 512 | data_buff = Utilities::ConvertUInt8ArrayToInt32(Wheelpos); |
soulx | 27:718fc94e40ad | 513 | memory.write(ADDRESS_WHEELPOS,data_buff); |
b0ssiz | 31:d6fa5e8e26b3 | 514 | wait_ms(EEPROM_DELAY); |
b0ssiz | 22:449f31da2d3d | 515 | } |
soulx | 27:718fc94e40ad | 516 | break; |
ParinyaT | 13:49cb002ad8fd | 517 | } |
b0ssiz | 28:b3509fd32b00 | 518 | break; |
b0ssiz | 28:b3509fd32b00 | 519 | } |
b0ssiz | 28:b3509fd32b00 | 520 | break; |
b0ssiz | 28:b3509fd32b00 | 521 | } |
b0ssiz | 28:b3509fd32b00 | 522 | case READ_DATA: { |
b0ssiz | 30:3f8e86fa1413 | 523 | switch (command[0]) { |
b0ssiz | 28:b3509fd32b00 | 524 | case MOTOR_UPPER_ANG: { |
b0ssiz | 31:d6fa5e8e26b3 | 525 | com.sendMotorPos(MY_ID,Upper_Position,Lower_Position); |
b0ssiz | 28:b3509fd32b00 | 526 | break; |
b0ssiz | 28:b3509fd32b00 | 527 | } |
b0ssiz | 28:b3509fd32b00 | 528 | case UP_MARGIN: { |
b0ssiz | 28:b3509fd32b00 | 529 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 530 | memory.read(ADDRESS_UP_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 531 | Utilities::ConvertInt32ToUInt8Array(data_buff,UpMargin); |
b0ssiz | 28:b3509fd32b00 | 532 | com.sendUpMargin(MY_ID,UpMargin); |
b0ssiz | 28:b3509fd32b00 | 533 | break; |
b0ssiz | 28:b3509fd32b00 | 534 | } |
b0ssiz | 28:b3509fd32b00 | 535 | case LOW_MARGIN: { |
b0ssiz | 28:b3509fd32b00 | 536 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 537 | memory.read(ADDRESS_LOW_MARGIN,data_buff); |
b0ssiz | 28:b3509fd32b00 | 538 | Utilities::ConvertInt32ToUInt8Array(data_buff,LowMargin); |
b0ssiz | 28:b3509fd32b00 | 539 | com.sendLowMargin(MY_ID,LowMargin); |
b0ssiz | 28:b3509fd32b00 | 540 | break; |
b0ssiz | 28:b3509fd32b00 | 541 | } |
b0ssiz | 28:b3509fd32b00 | 542 | case PID_UPPER_MOTOR: { |
b0ssiz | 28:b3509fd32b00 | 543 | memory.read(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 544 | memory.read(ADDRESS_UPPER_KI,U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 545 | memory.read(ADDRESS_UPPER_KD,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 546 | com.sendUpMotorKpKiKd(MY_ID,U_Kc,U_Ki_gain,U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 547 | /* |
b0ssiz | 30:3f8e86fa1413 | 548 | printf("After read Kp : %f\r\n",U_Kc); |
b0ssiz | 30:3f8e86fa1413 | 549 | printf("After read Ki : %f\r\n",U_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 550 | printf("After read Kd : %f\r\n",U_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 551 | */ |
b0ssiz | 28:b3509fd32b00 | 552 | break; |
b0ssiz | 28:b3509fd32b00 | 553 | } |
b0ssiz | 28:b3509fd32b00 | 554 | case PID_LOWER_MOTOR: { |
b0ssiz | 28:b3509fd32b00 | 555 | memory.read(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 556 | memory.read(ADDRESS_LOWER_KI,L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 557 | memory.read(ADDRESS_LOWER_KD,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 558 | com.sendLowMotorKpKiKd(MY_ID,L_Kc,L_Ki_gain,L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 559 | /* |
b0ssiz | 30:3f8e86fa1413 | 560 | printf("After read L_Kp : %f\r\n",L_Kc); |
b0ssiz | 30:3f8e86fa1413 | 561 | printf("After read L_Ki : %f\r\n",L_Ki_gain); |
b0ssiz | 30:3f8e86fa1413 | 562 | printf("After read L_Kd : %f\r\n",L_Kd_gain); |
b0ssiz | 30:3f8e86fa1413 | 563 | */ |
b0ssiz | 28:b3509fd32b00 | 564 | break; |
b0ssiz | 28:b3509fd32b00 | 565 | } |
b0ssiz | 28:b3509fd32b00 | 566 | case HEIGHT: { |
b0ssiz | 28:b3509fd32b00 | 567 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 568 | memory.read(ADDRESS_HEIGHT,data_buff); |
b0ssiz | 28:b3509fd32b00 | 569 | Utilities::ConvertInt32ToUInt8Array(data_buff,Height); |
b0ssiz | 28:b3509fd32b00 | 570 | com.sendHeight(MY_ID,Height); |
b0ssiz | 28:b3509fd32b00 | 571 | break; |
b0ssiz | 28:b3509fd32b00 | 572 | } |
b0ssiz | 28:b3509fd32b00 | 573 | case WHEELPOS: { |
b0ssiz | 28:b3509fd32b00 | 574 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 575 | memory.read(ADDRESS_WHEELPOS,data_buff); |
b0ssiz | 28:b3509fd32b00 | 576 | Utilities::ConvertInt32ToUInt8Array(data_buff,Wheelpos); |
b0ssiz | 28:b3509fd32b00 | 577 | com.sendWheelPos(MY_ID,Wheelpos); |
b0ssiz | 28:b3509fd32b00 | 578 | break; |
b0ssiz | 28:b3509fd32b00 | 579 | } |
b0ssiz | 28:b3509fd32b00 | 580 | case MAG_DATA: { |
b0ssiz | 28:b3509fd32b00 | 581 | uint8_t x_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 582 | uint8_t x_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 583 | uint8_t y_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 584 | uint8_t y_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 585 | uint8_t z_max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 586 | uint8_t z_min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 587 | int32_t x_max_buffer,x_min_buffer,y_max_buffer,y_min_buffer,z_max_buffer,z_min_buffer; |
b0ssiz | 28:b3509fd32b00 | 588 | memory.read(ADDRESS_MAG_DATA,x_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 589 | memory.read(ADDRESS_MAG_DATA+4,x_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 590 | memory.read(ADDRESS_MAG_DATA+8,y_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 591 | memory.read(ADDRESS_MAG_DATA+12,y_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 592 | memory.read(ADDRESS_MAG_DATA+16,z_max_buffer); |
b0ssiz | 28:b3509fd32b00 | 593 | memory.read(ADDRESS_MAG_DATA+20,z_min_buffer); |
b0ssiz | 28:b3509fd32b00 | 594 | Utilities::ConvertInt32ToUInt8Array(x_max_buffer,x_max_array); |
b0ssiz | 28:b3509fd32b00 | 595 | Utilities::ConvertInt32ToUInt8Array(x_min_buffer,x_min_array); |
b0ssiz | 28:b3509fd32b00 | 596 | Utilities::ConvertInt32ToUInt8Array(y_max_buffer,y_max_array); |
b0ssiz | 28:b3509fd32b00 | 597 | Utilities::ConvertInt32ToUInt8Array(y_min_buffer,y_min_array); |
b0ssiz | 28:b3509fd32b00 | 598 | Utilities::ConvertInt32ToUInt8Array(z_max_buffer,z_max_array); |
b0ssiz | 28:b3509fd32b00 | 599 | Utilities::ConvertInt32ToUInt8Array(z_min_buffer,z_min_array); |
b0ssiz | 28:b3509fd32b00 | 600 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 601 | Mag[i]=x_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 602 | Mag[i+4]=x_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 603 | Mag[i+8]=y_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 604 | Mag[i+12]=y_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 605 | Mag[i+16]=z_max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 606 | Mag[i+20]=z_min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 607 | } |
b0ssiz | 28:b3509fd32b00 | 608 | com.sendMagData(MY_ID,Mag); |
b0ssiz | 28:b3509fd32b00 | 609 | break; |
b0ssiz | 28:b3509fd32b00 | 610 | } |
b0ssiz | 28:b3509fd32b00 | 611 | case OFFSET: { |
b0ssiz | 28:b3509fd32b00 | 612 | uint8_t y_offset_array[4]; |
b0ssiz | 28:b3509fd32b00 | 613 | uint8_t z_offset_array[4]; |
b0ssiz | 28:b3509fd32b00 | 614 | int32_t y_data_buffer,z_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 615 | memory.read(ADDRESS_OFFSET,y_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 616 | memory.read(ADDRESS_OFFSET+4,z_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 617 | Utilities::ConvertInt32ToUInt8Array(y_data_buffer,y_offset_array); |
b0ssiz | 28:b3509fd32b00 | 618 | Utilities::ConvertInt32ToUInt8Array(z_data_buffer,z_offset_array); |
b0ssiz | 28:b3509fd32b00 | 619 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 620 | Offset[i]=y_offset_array[i]; |
b0ssiz | 28:b3509fd32b00 | 621 | Offset[i+4]=z_offset_array[i]; |
b0ssiz | 28:b3509fd32b00 | 622 | } |
b0ssiz | 28:b3509fd32b00 | 623 | com.sendOffset(MY_ID,Offset); |
b0ssiz | 28:b3509fd32b00 | 624 | break; |
b0ssiz | 28:b3509fd32b00 | 625 | } |
b0ssiz | 28:b3509fd32b00 | 626 | case BODY_WIDTH: { |
b0ssiz | 28:b3509fd32b00 | 627 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 628 | memory.read(ADDRESS_BODY_WIDTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 629 | Utilities::ConvertInt32ToUInt8Array(data_buff,Body_width); |
b0ssiz | 28:b3509fd32b00 | 630 | com.sendBodyWidth(MY_ID,Body_width); |
b0ssiz | 28:b3509fd32b00 | 631 | break; |
b0ssiz | 28:b3509fd32b00 | 632 | } |
b0ssiz | 28:b3509fd32b00 | 633 | case ANGLE_RANGE_UP: { |
b0ssiz | 28:b3509fd32b00 | 634 | uint8_t max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 635 | uint8_t min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 636 | int32_t max_data_buffer,min_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 637 | memory.read(ADDRESS_ANGLE_RANGE_UP,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 638 | memory.read(ADDRESS_ANGLE_RANGE_UP+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 639 | Utilities::ConvertInt32ToUInt8Array(max_data_buffer,max_array); |
b0ssiz | 28:b3509fd32b00 | 640 | Utilities::ConvertInt32ToUInt8Array(min_data_buffer,min_array); |
b0ssiz | 28:b3509fd32b00 | 641 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 642 | Angle_Range_Up[i]=max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 643 | Angle_Range_Up[i+4]=min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 644 | } |
b0ssiz | 28:b3509fd32b00 | 645 | com.sendUpAngleRange(MY_ID,Angle_Range_Up); |
b0ssiz | 28:b3509fd32b00 | 646 | break; |
b0ssiz | 28:b3509fd32b00 | 647 | } |
b0ssiz | 28:b3509fd32b00 | 648 | case ANGLE_RANGE_LOW: { |
b0ssiz | 28:b3509fd32b00 | 649 | uint8_t max_array[4]; |
b0ssiz | 28:b3509fd32b00 | 650 | uint8_t min_array[4]; |
b0ssiz | 28:b3509fd32b00 | 651 | int32_t max_data_buffer,min_data_buffer; |
b0ssiz | 28:b3509fd32b00 | 652 | memory.read(ADDRESS_ANGLE_RANGE_LOW,max_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 653 | memory.read(ADDRESS_ANGLE_RANGE_LOW+4,min_data_buffer); |
b0ssiz | 28:b3509fd32b00 | 654 | Utilities::ConvertInt32ToUInt8Array(max_data_buffer,max_array); |
b0ssiz | 28:b3509fd32b00 | 655 | Utilities::ConvertInt32ToUInt8Array(min_data_buffer,min_array); |
b0ssiz | 28:b3509fd32b00 | 656 | for(int i=0; i<4; i++) { |
b0ssiz | 28:b3509fd32b00 | 657 | Angle_Range_Low[i]=max_array[i]; |
b0ssiz | 28:b3509fd32b00 | 658 | Angle_Range_Low[i+4]=min_array[i]; |
b0ssiz | 28:b3509fd32b00 | 659 | } |
b0ssiz | 28:b3509fd32b00 | 660 | com.sendLowAngleRange(MY_ID,Angle_Range_Low); |
b0ssiz | 28:b3509fd32b00 | 661 | break; |
b0ssiz | 28:b3509fd32b00 | 662 | } |
b0ssiz | 28:b3509fd32b00 | 663 | case UP_LINK_LENGTH: { |
b0ssiz | 28:b3509fd32b00 | 664 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 665 | memory.read(ADDRESS_UP_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 666 | Utilities::ConvertInt32ToUInt8Array(data_buff,UpLinkLength); |
b0ssiz | 28:b3509fd32b00 | 667 | com.sendUpLinkLength(MY_ID,UpLinkLength); |
b0ssiz | 28:b3509fd32b00 | 668 | break; |
b0ssiz | 28:b3509fd32b00 | 669 | } |
b0ssiz | 28:b3509fd32b00 | 670 | case LOW_LINK_LENGTH: { |
b0ssiz | 28:b3509fd32b00 | 671 | int32_t data_buff; |
b0ssiz | 28:b3509fd32b00 | 672 | memory.read(ADDRESS_LOW_LINK_LENGTH,data_buff); |
b0ssiz | 28:b3509fd32b00 | 673 | Utilities::ConvertInt32ToUInt8Array(data_buff,LowLinkLength); |
b0ssiz | 28:b3509fd32b00 | 674 | com.sendLowLinkLength(MY_ID,LowLinkLength); |
b0ssiz | 28:b3509fd32b00 | 675 | break; |
b0ssiz | 28:b3509fd32b00 | 676 | } |
b0ssiz | 28:b3509fd32b00 | 677 | break; |
ParinyaT | 13:49cb002ad8fd | 678 | } |
ParinyaT | 13:49cb002ad8fd | 679 | } |
b0ssiz | 10:3b3d6bc88677 | 680 | } |
b0ssiz | 14:28e24fcc5a01 | 681 | } |
b0ssiz | 10:3b3d6bc88677 | 682 | } |
b0ssiz | 17:4c96838e579f | 683 | |
b0ssiz | 28:b3509fd32b00 | 684 | |
ParinyaT | 11:3dd92d1d542c | 685 | /******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 686 | void Start_Up() |
ParinyaT | 11:3dd92d1d542c | 687 | { |
ParinyaT | 11:3dd92d1d542c | 688 | // wait for reciever |
b0ssiz | 22:449f31da2d3d | 689 | memory.read(ADDRESS_ID,MY_ID); |
b0ssiz | 22:449f31da2d3d | 690 | memory.read(ADDRESS_UPPER_KP,U_Kc); |
b0ssiz | 22:449f31da2d3d | 691 | memory.read(ADDRESS_UPPER_KI,U_Ti); |
b0ssiz | 22:449f31da2d3d | 692 | memory.read(ADDRESS_UPPER_KD,U_Td); |
b0ssiz | 22:449f31da2d3d | 693 | memory.read(ADDRESS_LOWER_KP,L_Kc); |
b0ssiz | 22:449f31da2d3d | 694 | memory.read(ADDRESS_LOWER_KI,L_Ti); |
b0ssiz | 22:449f31da2d3d | 695 | memory.read(ADDRESS_LOWER_KD,L_Td); |
b0ssiz | 14:28e24fcc5a01 | 696 | |
ParinyaT | 11:3dd92d1d542c | 697 | } |
b0ssiz | 14:28e24fcc5a01 | 698 | /*******************************************************/ |
soulx | 27:718fc94e40ad | 699 | void Rc() |
ParinyaT | 11:3dd92d1d542c | 700 | { |
soulx | 21:1c04c4afe3b7 | 701 | myled =1; |
b0ssiz | 17:4c96838e579f | 702 | uint8_t data_array[30]; |
soulx | 27:718fc94e40ad | 703 | uint8_t id=0; |
soulx | 27:718fc94e40ad | 704 | uint8_t ins=0; |
soulx | 27:718fc94e40ad | 705 | uint8_t status=0xFF; |
b0ssiz | 14:28e24fcc5a01 | 706 | |
soulx | 27:718fc94e40ad | 707 | |
soulx | 20:7e6d56655336 | 708 | |
soulx | 18:face01c94152 | 709 | status = com.ReceiveCommand(&id,data_array,&ins); |
soulx | 29:5db0a9261161 | 710 | //PC.printf("status = 0x%02x\n\r",status); |
soulx | 18:face01c94152 | 711 | if(status == ANDANTE_ERRBIT_NONE) { |
soulx | 18:face01c94152 | 712 | CmdCheck((int16_t)id,data_array,ins); |
soulx | 18:face01c94152 | 713 | } |
soulx | 27:718fc94e40ad | 714 | |
ParinyaT | 11:3dd92d1d542c | 715 | } |
ParinyaT | 11:3dd92d1d542c | 716 | /*******************************************************/ |
ParinyaT | 0:451c27e4d55e | 717 | int main() |
ParinyaT | 0:451c27e4d55e | 718 | { |
soulx | 29:5db0a9261161 | 719 | PC.baud(115200); |
b0ssiz | 31:d6fa5e8e26b3 | 720 | /* |
b0ssiz | 31:d6fa5e8e26b3 | 721 | while(1) |
b0ssiz | 31:d6fa5e8e26b3 | 722 | { |
b0ssiz | 31:d6fa5e8e26b3 | 723 | Read_Encoder(EncoderA); |
b0ssiz | 31:d6fa5e8e26b3 | 724 | Upper_Position = Get_EnValue(data); |
b0ssiz | 31:d6fa5e8e26b3 | 725 | Read_Encoder(EncoderB); |
b0ssiz | 31:d6fa5e8e26b3 | 726 | Lower_Position = Get_EnValue(data); |
b0ssiz | 31:d6fa5e8e26b3 | 727 | PC.printf("Upper Position : %f\n",Upper_Position); |
b0ssiz | 31:d6fa5e8e26b3 | 728 | PC.printf("Lower_Position : %f\n",Lower_Position); |
b0ssiz | 31:d6fa5e8e26b3 | 729 | wait(0.5); |
b0ssiz | 31:d6fa5e8e26b3 | 730 | } |
b0ssiz | 31:d6fa5e8e26b3 | 731 | */ |
b0ssiz | 32:1f81f3e83889 | 732 | |
soulx | 27:718fc94e40ad | 733 | |
soulx | 21:1c04c4afe3b7 | 734 | //Recieve.attach(&Rc,0.025); |
soulx | 20:7e6d56655336 | 735 | //Start_Up(); |
b0ssiz | 23:999dd41eef14 | 736 | |
ParinyaT | 1:84167ca00307 | 737 | SET_UpperPID(); |
ParinyaT | 1:84167ca00307 | 738 | SET_LowerPID(); |
ParinyaT | 11:3dd92d1d542c | 739 | |
soulx | 29:5db0a9261161 | 740 | printf("BEAR MOTION\n\r"); |
b0ssiz | 14:28e24fcc5a01 | 741 | while(1) { |
soulx | 27:718fc94e40ad | 742 | myled =0; |
soulx | 27:718fc94e40ad | 743 | //wait_ms(10); |
soulx | 20:7e6d56655336 | 744 | ///////////////////////////////////////////////////// start |
b0ssiz | 14:28e24fcc5a01 | 745 | //Set Set_point |
b0ssiz | 14:28e24fcc5a01 | 746 | Up_PID.setSetPoint(Upper_SetPoint); |
b0ssiz | 14:28e24fcc5a01 | 747 | Low_PID.setSetPoint(Lower_SetPoint); |
ParinyaT | 1:84167ca00307 | 748 | |
b0ssiz | 14:28e24fcc5a01 | 749 | //Control Motor |
b0ssiz | 14:28e24fcc5a01 | 750 | Move_Upper(); |
b0ssiz | 14:28e24fcc5a01 | 751 | Move_Lower(); |
soulx | 20:7e6d56655336 | 752 | ///////////////////////////////////////////////////// stop =306us |
soulx | 21:1c04c4afe3b7 | 753 | //uint8_t aaa[1]={10}; |
soulx | 21:1c04c4afe3b7 | 754 | //com.sendBodyWidth(0x01,aaa); |
soulx | 21:1c04c4afe3b7 | 755 | Rc(); |
soulx | 21:1c04c4afe3b7 | 756 | //wait_ms(1); |
soulx | 20:7e6d56655336 | 757 | } |
ParinyaT | 0:451c27e4d55e | 758 | } |
b0ssiz | 6:98871feebea0 | 759 | |
ParinyaT | 0:451c27e4d55e | 760 | |
ParinyaT | 0:451c27e4d55e | 761 | |
b0ssiz | 6:98871feebea0 | 762 | |
b0ssiz | 6:98871feebea0 | 763 | |
b0ssiz | 6:98871feebea0 | 764 |