![](/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@19:3da61e637b2d, 2016-01-24 (annotated)
- Committer:
- soulx
- Date:
- Sun Jan 24 08:39:14 2016 +0000
- Revision:
- 19:3da61e637b2d
- Parent:
- 18:face01c94152
- Child:
- 20:7e6d56655336
- Child:
- 22:449f31da2d3d
-
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" |
ParinyaT | 0:451c27e4d55e | 9 | |
ParinyaT | 0:451c27e4d55e | 10 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 11 | //--PID parameter-- |
ParinyaT | 1:84167ca00307 | 12 | //-Upper-// |
b0ssiz | 10:3b3d6bc88677 | 13 | float U_Kc; |
b0ssiz | 10:3b3d6bc88677 | 14 | float U_Ti; |
b0ssiz | 10:3b3d6bc88677 | 15 | float U_Td; |
ParinyaT | 16:c0a1daeb9fa5 | 16 | float U_Ki=U_Kc*U_Ti; |
ParinyaT | 16:c0a1daeb9fa5 | 17 | float U_Kd=U_Kc*U_Td; |
ParinyaT | 1:84167ca00307 | 18 | //-lower-// |
b0ssiz | 10:3b3d6bc88677 | 19 | float L_Kc; |
b0ssiz | 10:3b3d6bc88677 | 20 | float L_Ti; |
b0ssiz | 10:3b3d6bc88677 | 21 | float L_Td; |
ParinyaT | 16:c0a1daeb9fa5 | 22 | float L_Ki=L_Kc*L_Ti; |
ParinyaT | 16:c0a1daeb9fa5 | 23 | float L_Kd=L_Kc*L_Td; |
ParinyaT | 0:451c27e4d55e | 24 | //*****************************************************/ |
ParinyaT | 0:451c27e4d55e | 25 | // Global // |
ParinyaT | 11:3dd92d1d542c | 26 | Ticker Recieve; |
ParinyaT | 1:84167ca00307 | 27 | //-- Communication -- |
ParinyaT | 0:451c27e4d55e | 28 | Serial PC(D1,D0); |
b0ssiz | 17:4c96838e579f | 29 | Bear_Receiver com(Tx,Rx,1000000); |
b0ssiz | 14:28e24fcc5a01 | 30 | int16_t MY_ID = 0x01; |
ParinyaT | 11:3dd92d1d542c | 31 | //-- Memorry -- |
ParinyaT | 11:3dd92d1d542c | 32 | EEPROM memory(PB_4,PA_8,0); |
b0ssiz | 14:28e24fcc5a01 | 33 | uint8_t UpMargin[4]; |
b0ssiz | 14:28e24fcc5a01 | 34 | uint8_t LowMargin[4]; |
ParinyaT | 13:49cb002ad8fd | 35 | uint8_t Height[4]; |
ParinyaT | 13:49cb002ad8fd | 36 | uint8_t Wheelpos[4]; |
ParinyaT | 13:49cb002ad8fd | 37 | uint8_t Mag[24]; |
ParinyaT | 16:c0a1daeb9fa5 | 38 | uint8_t Offset[8]; |
b0ssiz | 14:28e24fcc5a01 | 39 | uint8_t Body_width[4]; |
ParinyaT | 16:c0a1daeb9fa5 | 40 | uint8_t Angle_Range_Up[8]; |
ParinyaT | 16:c0a1daeb9fa5 | 41 | uint8_t Angle_Range_Low[8]; |
b0ssiz | 6:98871feebea0 | 42 | //-- encoder -- |
ParinyaT | 13:49cb002ad8fd | 43 | float up_angle,low_angle; |
ParinyaT | 1:84167ca00307 | 44 | int Upper_Position; |
ParinyaT | 1:84167ca00307 | 45 | int Lower_Position; |
ParinyaT | 0:451c27e4d55e | 46 | int data; |
ParinyaT | 1:84167ca00307 | 47 | SPI ENC(Emosi, Emiso, Esck); |
ParinyaT | 1:84167ca00307 | 48 | DigitalOut EncA(EncoderA); |
ParinyaT | 1:84167ca00307 | 49 | DigitalOut EncB(EncoderB); |
ParinyaT | 0:451c27e4d55e | 50 | //-- Motor -- |
ParinyaT | 1:84167ca00307 | 51 | int dir; |
ParinyaT | 1:84167ca00307 | 52 | Motor Upper(PWM_LU,A_LU,B_LU); |
ParinyaT | 1:84167ca00307 | 53 | Motor Lower(PWM_LL,A_LL,B_LL); |
ParinyaT | 0:451c27e4d55e | 54 | //-- PID -- |
ParinyaT | 1:84167ca00307 | 55 | int Upper_SetPoint; |
ParinyaT | 1:84167ca00307 | 56 | int Lower_SetPoint; |
ParinyaT | 1:84167ca00307 | 57 | PID Up_PID(U_Kc, U_Ti, U_Td, 0.001);//Kp,Ki,Kd,Rate |
ParinyaT | 1:84167ca00307 | 58 | PID Low_PID(L_Kc, L_Ti, L_Td, 0.001); |
ParinyaT | 0:451c27e4d55e | 59 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 60 | void Read_Encoder(PinName Encoder) |
ParinyaT | 0:451c27e4d55e | 61 | { |
soulx | 7:bf239d051e8c | 62 | ENC.format(8,0); |
soulx | 7:bf239d051e8c | 63 | ENC.frequency(200000);//due to rising time,have to decrease clock from 1M - 240k |
b0ssiz | 6:98871feebea0 | 64 | |
soulx | 7:bf239d051e8c | 65 | if(Encoder == EncoderA) { |
soulx | 7:bf239d051e8c | 66 | EncA = 0; |
soulx | 7:bf239d051e8c | 67 | } else { |
soulx | 7:bf239d051e8c | 68 | EncB = 0; |
soulx | 7:bf239d051e8c | 69 | } |
soulx | 7:bf239d051e8c | 70 | ENC.write(0x41); |
soulx | 7:bf239d051e8c | 71 | ENC.write(0x09); |
soulx | 7:bf239d051e8c | 72 | data = ENC.write(0x00); |
soulx | 7:bf239d051e8c | 73 | if(Encoder == EncoderA) { |
soulx | 7:bf239d051e8c | 74 | EncA = 1; |
soulx | 7:bf239d051e8c | 75 | } else { |
soulx | 7:bf239d051e8c | 76 | EncB = 1; |
soulx | 7:bf239d051e8c | 77 | } |
b0ssiz | 6:98871feebea0 | 78 | |
ParinyaT | 0:451c27e4d55e | 79 | } |
ParinyaT | 0:451c27e4d55e | 80 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 81 | int Get_EnValue(int Val) |
ParinyaT | 0:451c27e4d55e | 82 | { |
ParinyaT | 0:451c27e4d55e | 83 | int i = 0; |
ParinyaT | 0:451c27e4d55e | 84 | static unsigned char codes[] = { |
b0ssiz | 6:98871feebea0 | 85 | 127, 63, 62, 58, 56, 184, 152, 24, 8, 72, 73, 77, 79, 15, 47, 175, |
b0ssiz | 6:98871feebea0 | 86 | 191, 159, 31, 29, 28, 92, 76, 12, 4, 36, 164, 166, 167, 135, 151, 215, |
b0ssiz | 6:98871feebea0 | 87 | 223, 207, 143, 142, 14, 46, 38, 6, 2, 18, 82, 83, 211, 195, 203, 235, |
b0ssiz | 6:98871feebea0 | 88 | 239, 231, 199, 71, 7, 23, 19, 3, 1, 9, 41, 169, 233, 225, 229, 245, |
b0ssiz | 6:98871feebea0 | 89 | 247, 243, 227, 163, 131, 139, 137, 129, 128, 132, 148, 212, 244, 240, 242, 250, |
b0ssiz | 6:98871feebea0 | 90 | 251, 249, 241, 209, 193, 197, 196, 192, 64, 66, 74, 106, 122, 120, 121, 125, |
b0ssiz | 6:98871feebea0 | 91 | 253, 252, 248, 232, 224, 226, 98, 96, 32, 33, 37, 53, 61, 60, 188, 190, |
b0ssiz | 6:98871feebea0 | 92 | 254, 126, 124, 116, 112, 113, 49, 48, 16, 144, 146, 154, 158, 30, 94, 95 |
b0ssiz | 6:98871feebea0 | 93 | }; |
b0ssiz | 6:98871feebea0 | 94 | |
b0ssiz | 6:98871feebea0 | 95 | while(Val != codes[i]) { |
ParinyaT | 1:84167ca00307 | 96 | i++; |
ParinyaT | 0:451c27e4d55e | 97 | } |
b0ssiz | 6:98871feebea0 | 98 | |
ParinyaT | 1:84167ca00307 | 99 | return i; |
b0ssiz | 6:98871feebea0 | 100 | |
ParinyaT | 0:451c27e4d55e | 101 | } |
ParinyaT | 0:451c27e4d55e | 102 | //*****************************************************/ |
ParinyaT | 1:84167ca00307 | 103 | void SET_UpperPID() |
ParinyaT | 1:84167ca00307 | 104 | { |
ParinyaT | 1:84167ca00307 | 105 | Upper.period(0.001); |
ParinyaT | 1:84167ca00307 | 106 | Up_PID.setMode(0); |
ParinyaT | 1:84167ca00307 | 107 | Up_PID.setInputLimits(0,127); |
ParinyaT | 1:84167ca00307 | 108 | Up_PID.setOutputLimits(0,1); |
ParinyaT | 1:84167ca00307 | 109 | } |
ParinyaT | 1:84167ca00307 | 110 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 111 | void SET_LowerPID() |
ParinyaT | 1:84167ca00307 | 112 | { |
ParinyaT | 1:84167ca00307 | 113 | Lower.period(0.001); |
ParinyaT | 1:84167ca00307 | 114 | Low_PID.setMode(0); |
b0ssiz | 10:3b3d6bc88677 | 115 | Low_PID.setInputLimits(0,127); // set range |
ParinyaT | 1:84167ca00307 | 116 | Low_PID.setOutputLimits(0,1); |
ParinyaT | 1:84167ca00307 | 117 | } |
b0ssiz | 6:98871feebea0 | 118 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 119 | void Move_Upper() |
ParinyaT | 1:84167ca00307 | 120 | { |
ParinyaT | 1:84167ca00307 | 121 | Read_Encoder(EncoderA); |
ParinyaT | 1:84167ca00307 | 122 | Upper_Position = Get_EnValue(data); |
ParinyaT | 1:84167ca00307 | 123 | |
ParinyaT | 1:84167ca00307 | 124 | Up_PID.setProcessValue(Upper_Position); |
b0ssiz | 6:98871feebea0 | 125 | |
soulx | 7:bf239d051e8c | 126 | if(Upper_Position - Upper_SetPoint > 0 ) { |
ParinyaT | 1:84167ca00307 | 127 | dir = 1; |
b0ssiz | 6:98871feebea0 | 128 | } |
soulx | 7:bf239d051e8c | 129 | if(Upper_Position - Upper_SetPoint < 0 ) { |
ParinyaT | 1:84167ca00307 | 130 | dir = -1; |
b0ssiz | 6:98871feebea0 | 131 | } |
soulx | 7:bf239d051e8c | 132 | Upper.speed(Up_PID.compute() * dir); |
ParinyaT | 1:84167ca00307 | 133 | } |
ParinyaT | 1:84167ca00307 | 134 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 135 | void Move_Lower() |
ParinyaT | 1:84167ca00307 | 136 | { |
ParinyaT | 1:84167ca00307 | 137 | Read_Encoder(EncoderB); |
ParinyaT | 1:84167ca00307 | 138 | Lower_Position = Get_EnValue(data); |
ParinyaT | 1:84167ca00307 | 139 | |
ParinyaT | 1:84167ca00307 | 140 | Low_PID.setProcessValue(Lower_Position); |
b0ssiz | 6:98871feebea0 | 141 | |
b0ssiz | 6:98871feebea0 | 142 | if(Lower_Position - Lower_SetPoint > 0 ) { |
ParinyaT | 1:84167ca00307 | 143 | dir = 1; |
b0ssiz | 6:98871feebea0 | 144 | } |
b0ssiz | 6:98871feebea0 | 145 | if(Lower_Position - Lower_SetPoint < 0 ) { |
ParinyaT | 1:84167ca00307 | 146 | dir = -1; |
b0ssiz | 6:98871feebea0 | 147 | } |
soulx | 7:bf239d051e8c | 148 | Lower.speed(Low_PID.compute() * dir); |
b0ssiz | 6:98871feebea0 | 149 | } |
ParinyaT | 1:84167ca00307 | 150 | //******************************************************/ |
ParinyaT | 1:84167ca00307 | 151 | |
b0ssiz | 15:6ebca0a1aaca | 152 | void CmdCheck(int16_t id,uint8_t *cmd,uint8_t ins) |
b0ssiz | 10:3b3d6bc88677 | 153 | { |
b0ssiz | 14:28e24fcc5a01 | 154 | if(id==MY_ID) { |
b0ssiz | 14:28e24fcc5a01 | 155 | switch (ins) { |
b0ssiz | 14:28e24fcc5a01 | 156 | case PING: { |
soulx | 18:face01c94152 | 157 | break; |
b0ssiz | 14:28e24fcc5a01 | 158 | } |
b0ssiz | 14:28e24fcc5a01 | 159 | case WRITE_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 160 | switch (cmd[0]) { |
b0ssiz | 14:28e24fcc5a01 | 161 | case ID: { |
soulx | 18:face01c94152 | 162 | /// |
soulx | 18:face01c94152 | 163 | MY_ID = (int16_t)cmd[1]; |
soulx | 18:face01c94152 | 164 | break; |
b0ssiz | 14:28e24fcc5a01 | 165 | } |
b0ssiz | 14:28e24fcc5a01 | 166 | case MOTOR_UPPER_ANG: { |
b0ssiz | 14:28e24fcc5a01 | 167 | uint8_t IntUpAngle[2],FloatUpAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 168 | uint8_t IntLowAngle[2],FloatLowAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 169 | float int_buffer,float_buffer; |
b0ssiz | 14:28e24fcc5a01 | 170 | |
b0ssiz | 14:28e24fcc5a01 | 171 | IntUpAngle[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 172 | IntUpAngle[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 173 | FloatUpAngle[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 174 | FloatUpAngle[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 175 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntUpAngle); |
b0ssiz | 14:28e24fcc5a01 | 176 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatUpAngle)/FLOAT_CONVERTER; |
b0ssiz | 14:28e24fcc5a01 | 177 | up_angle=int_buffer+float_buffer; |
b0ssiz | 14:28e24fcc5a01 | 178 | |
b0ssiz | 14:28e24fcc5a01 | 179 | IntLowAngle[0]=cmd[5]; |
b0ssiz | 14:28e24fcc5a01 | 180 | IntLowAngle[1]=cmd[6]; |
b0ssiz | 14:28e24fcc5a01 | 181 | FloatLowAngle[0]=cmd[7]; |
b0ssiz | 14:28e24fcc5a01 | 182 | FloatLowAngle[1]=cmd[8]; |
b0ssiz | 14:28e24fcc5a01 | 183 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntLowAngle); |
b0ssiz | 14:28e24fcc5a01 | 184 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatLowAngle)/FLOAT_CONVERTER; |
soulx | 18:face01c94152 | 185 | low_angle=int_buffer+float_buffer; |
soulx | 18:face01c94152 | 186 | break; |
b0ssiz | 14:28e24fcc5a01 | 187 | } |
b0ssiz | 14:28e24fcc5a01 | 188 | case UP_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 189 | int i; |
b0ssiz | 14:28e24fcc5a01 | 190 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 191 | UpMargin[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 192 | } |
soulx | 18:face01c94152 | 193 | break; |
b0ssiz | 14:28e24fcc5a01 | 194 | } |
b0ssiz | 14:28e24fcc5a01 | 195 | case LOW_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 196 | int i; |
b0ssiz | 14:28e24fcc5a01 | 197 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 198 | LowMargin[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 199 | } |
soulx | 18:face01c94152 | 200 | break; |
b0ssiz | 14:28e24fcc5a01 | 201 | } |
b0ssiz | 14:28e24fcc5a01 | 202 | case KP_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 203 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 204 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 205 | float Int,Float; |
b0ssiz | 14:28e24fcc5a01 | 206 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 207 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 208 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 209 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 210 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 211 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 212 | U_Kc=Int+Float; |
soulx | 18:face01c94152 | 213 | break; |
b0ssiz | 14:28e24fcc5a01 | 214 | } |
b0ssiz | 14:28e24fcc5a01 | 215 | case KI_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 216 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 217 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 218 | float Int,Float,KI; |
b0ssiz | 14:28e24fcc5a01 | 219 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 220 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 221 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 222 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 223 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 224 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 225 | KI=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 226 | U_Ti=KI/U_Kc; |
soulx | 18:face01c94152 | 227 | break; |
b0ssiz | 14:28e24fcc5a01 | 228 | } |
b0ssiz | 14:28e24fcc5a01 | 229 | case KD_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 230 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 231 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 232 | float Int,Float,KD; |
b0ssiz | 14:28e24fcc5a01 | 233 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 234 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 235 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 236 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 237 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 238 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 239 | KD=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 240 | U_Td=KD/U_Kc; |
soulx | 18:face01c94152 | 241 | break; |
b0ssiz | 14:28e24fcc5a01 | 242 | } |
b0ssiz | 14:28e24fcc5a01 | 243 | case KP_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 244 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 245 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 246 | float Int,Float; |
b0ssiz | 14:28e24fcc5a01 | 247 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 248 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 249 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 250 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 251 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 252 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 253 | L_Kc=Int+Float; |
soulx | 18:face01c94152 | 254 | break; |
b0ssiz | 14:28e24fcc5a01 | 255 | } |
b0ssiz | 14:28e24fcc5a01 | 256 | case KI_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 257 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 258 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 259 | float Int,Float,KI; |
b0ssiz | 14:28e24fcc5a01 | 260 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 261 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 262 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 263 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 264 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 265 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 266 | KI=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 267 | L_Ti=KI/L_Kc; |
soulx | 18:face01c94152 | 268 | break; |
b0ssiz | 14:28e24fcc5a01 | 269 | } |
b0ssiz | 14:28e24fcc5a01 | 270 | case KD_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 271 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 272 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 273 | float Int,Float,KD; |
b0ssiz | 14:28e24fcc5a01 | 274 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 275 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 276 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 277 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 278 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 279 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 280 | KD=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 281 | L_Td=KD/L_Kc; |
soulx | 18:face01c94152 | 282 | break; |
b0ssiz | 14:28e24fcc5a01 | 283 | } |
b0ssiz | 14:28e24fcc5a01 | 284 | case HEIGHT: { |
b0ssiz | 14:28e24fcc5a01 | 285 | int i; |
b0ssiz | 14:28e24fcc5a01 | 286 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 287 | Height[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 288 | } |
soulx | 18:face01c94152 | 289 | break; |
b0ssiz | 14:28e24fcc5a01 | 290 | } |
b0ssiz | 14:28e24fcc5a01 | 291 | case WHEELPOS: { |
b0ssiz | 14:28e24fcc5a01 | 292 | int i; |
b0ssiz | 14:28e24fcc5a01 | 293 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 294 | Wheelpos[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 295 | } |
soulx | 18:face01c94152 | 296 | break; |
b0ssiz | 14:28e24fcc5a01 | 297 | } |
b0ssiz | 14:28e24fcc5a01 | 298 | case MAG_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 299 | int i; |
b0ssiz | 14:28e24fcc5a01 | 300 | for(i=0; i<24; i++) { |
b0ssiz | 14:28e24fcc5a01 | 301 | Mag[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 302 | } |
soulx | 18:face01c94152 | 303 | break; |
b0ssiz | 14:28e24fcc5a01 | 304 | } |
b0ssiz | 14:28e24fcc5a01 | 305 | case OFFSET: { |
b0ssiz | 14:28e24fcc5a01 | 306 | int i; |
b0ssiz | 14:28e24fcc5a01 | 307 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 308 | Offset[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 309 | } |
soulx | 18:face01c94152 | 310 | break; |
b0ssiz | 14:28e24fcc5a01 | 311 | } |
b0ssiz | 14:28e24fcc5a01 | 312 | case BODY_WIDTH: { |
b0ssiz | 14:28e24fcc5a01 | 313 | int i; |
b0ssiz | 14:28e24fcc5a01 | 314 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 315 | Body_width[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 316 | } |
soulx | 18:face01c94152 | 317 | break; |
b0ssiz | 14:28e24fcc5a01 | 318 | } |
b0ssiz | 14:28e24fcc5a01 | 319 | case ANGLE_RANGE_UP: { |
b0ssiz | 14:28e24fcc5a01 | 320 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 321 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 322 | Angle_Range_Up[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 323 | } |
soulx | 18:face01c94152 | 324 | break; |
b0ssiz | 14:28e24fcc5a01 | 325 | } |
b0ssiz | 14:28e24fcc5a01 | 326 | case ANGLE_RANGE_LOW: { |
b0ssiz | 14:28e24fcc5a01 | 327 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 328 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 329 | Angle_Range_Low[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 330 | } |
soulx | 18:face01c94152 | 331 | break; |
ParinyaT | 13:49cb002ad8fd | 332 | } |
ParinyaT | 16:c0a1daeb9fa5 | 333 | // unfinish yet!!!!!!!!!!!!!!!!! |
ParinyaT | 16:c0a1daeb9fa5 | 334 | case SAVE_EEPROM_DATA: { |
soulx | 18:face01c94152 | 335 | if (cmd[1]==ID) { |
soulx | 18:face01c94152 | 336 | |
ParinyaT | 16:c0a1daeb9fa5 | 337 | } |
soulx | 18:face01c94152 | 338 | break; |
ParinyaT | 16:c0a1daeb9fa5 | 339 | } |
soulx | 18:face01c94152 | 340 | |
soulx | 19:3da61e637b2d | 341 | // break; |
ParinyaT | 12:6b3b997dd7c2 | 342 | } |
b0ssiz | 14:28e24fcc5a01 | 343 | case READ_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 344 | switch (cmd[0]) { |
b0ssiz | 14:28e24fcc5a01 | 345 | case MOTOR_UPPER_ANG: { |
soulx | 18:face01c94152 | 346 | uint8_t status; |
soulx | 18:face01c94152 | 347 | status =com.sendMotorPos(MY_ID,Upper_Position,Lower_Position); |
soulx | 18:face01c94152 | 348 | printf("status = 0x%02x\n\r",status); |
soulx | 18:face01c94152 | 349 | break; |
b0ssiz | 14:28e24fcc5a01 | 350 | } |
b0ssiz | 14:28e24fcc5a01 | 351 | case UP_MARGIN: { |
soulx | 18:face01c94152 | 352 | com.sendUpMargin(MY_ID,UpMargin); |
soulx | 18:face01c94152 | 353 | break; |
b0ssiz | 14:28e24fcc5a01 | 354 | } |
b0ssiz | 14:28e24fcc5a01 | 355 | case LOW_MARGIN: { |
ParinyaT | 16:c0a1daeb9fa5 | 356 | com.sendLowMargin(MY_ID,LowMargin); |
soulx | 18:face01c94152 | 357 | break; |
b0ssiz | 14:28e24fcc5a01 | 358 | } |
b0ssiz | 14:28e24fcc5a01 | 359 | case PID_UPPER_MOTOR: { |
ParinyaT | 16:c0a1daeb9fa5 | 360 | com.sendUpMotorKpKiKd(MY_ID,U_Kc,U_Ki,U_Kd); |
soulx | 18:face01c94152 | 361 | break; |
b0ssiz | 14:28e24fcc5a01 | 362 | } |
b0ssiz | 14:28e24fcc5a01 | 363 | case PID_LOWER_MOTOR: { |
ParinyaT | 16:c0a1daeb9fa5 | 364 | com.sendLowMotorKpKiKd(MY_ID,L_Kc,L_Ki,L_Kd); |
soulx | 18:face01c94152 | 365 | break; |
b0ssiz | 14:28e24fcc5a01 | 366 | } |
b0ssiz | 14:28e24fcc5a01 | 367 | case HEIGHT: { |
ParinyaT | 16:c0a1daeb9fa5 | 368 | com.sendHeight(MY_ID,Height); |
soulx | 18:face01c94152 | 369 | break; |
b0ssiz | 14:28e24fcc5a01 | 370 | } |
b0ssiz | 14:28e24fcc5a01 | 371 | case WHEELPOS: { |
ParinyaT | 16:c0a1daeb9fa5 | 372 | com.sendWheelPos(MY_ID,Wheelpos); |
soulx | 18:face01c94152 | 373 | break; |
b0ssiz | 14:28e24fcc5a01 | 374 | } |
b0ssiz | 14:28e24fcc5a01 | 375 | case MAG_DATA: { |
ParinyaT | 16:c0a1daeb9fa5 | 376 | com.sendMagData(MY_ID,Mag); |
soulx | 18:face01c94152 | 377 | break; |
b0ssiz | 14:28e24fcc5a01 | 378 | } |
b0ssiz | 14:28e24fcc5a01 | 379 | case OFFSET: { |
ParinyaT | 16:c0a1daeb9fa5 | 380 | com.sendOffset(MY_ID,Offset); |
soulx | 18:face01c94152 | 381 | break; |
b0ssiz | 14:28e24fcc5a01 | 382 | } |
b0ssiz | 14:28e24fcc5a01 | 383 | case BODY_WIDTH: { |
ParinyaT | 16:c0a1daeb9fa5 | 384 | com.sendBodyWidth(MY_ID,Body_width); |
soulx | 18:face01c94152 | 385 | break; |
b0ssiz | 14:28e24fcc5a01 | 386 | } |
b0ssiz | 14:28e24fcc5a01 | 387 | case ANGLE_RANGE_UP: { |
ParinyaT | 16:c0a1daeb9fa5 | 388 | com.sendUpAngleRange(MY_ID,Angle_Range_Up); |
soulx | 18:face01c94152 | 389 | break; |
b0ssiz | 14:28e24fcc5a01 | 390 | } |
b0ssiz | 14:28e24fcc5a01 | 391 | case ANGLE_RANGE_LOW: { |
ParinyaT | 16:c0a1daeb9fa5 | 392 | com.sendLowAngleRange(MY_ID,Angle_Range_Low); |
soulx | 18:face01c94152 | 393 | break; |
b0ssiz | 14:28e24fcc5a01 | 394 | } |
soulx | 19:3da61e637b2d | 395 | // break; |
ParinyaT | 13:49cb002ad8fd | 396 | } |
ParinyaT | 13:49cb002ad8fd | 397 | } |
ParinyaT | 13:49cb002ad8fd | 398 | } |
b0ssiz | 10:3b3d6bc88677 | 399 | } |
b0ssiz | 14:28e24fcc5a01 | 400 | } |
b0ssiz | 10:3b3d6bc88677 | 401 | } |
b0ssiz | 17:4c96838e579f | 402 | |
ParinyaT | 11:3dd92d1d542c | 403 | /******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 404 | void Start_Up() |
ParinyaT | 11:3dd92d1d542c | 405 | { |
ParinyaT | 11:3dd92d1d542c | 406 | // wait for reciever |
ParinyaT | 11:3dd92d1d542c | 407 | memory.read(0x00,MY_ID); |
ParinyaT | 11:3dd92d1d542c | 408 | memory.read(0x04,Upper_SetPoint); |
ParinyaT | 11:3dd92d1d542c | 409 | memory.read(0x08,Lower_SetPoint); |
ParinyaT | 11:3dd92d1d542c | 410 | memory.read(0x10,U_Kc); |
ParinyaT | 11:3dd92d1d542c | 411 | memory.read(0x14,U_Ti); |
ParinyaT | 11:3dd92d1d542c | 412 | memory.read(0x18,U_Td); |
ParinyaT | 11:3dd92d1d542c | 413 | memory.read(0x1c,L_Kc); |
ParinyaT | 11:3dd92d1d542c | 414 | memory.read(0x20,L_Ti); |
ParinyaT | 11:3dd92d1d542c | 415 | memory.read(0x24,L_Td); |
b0ssiz | 14:28e24fcc5a01 | 416 | |
ParinyaT | 11:3dd92d1d542c | 417 | } |
b0ssiz | 14:28e24fcc5a01 | 418 | /*******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 419 | void Rc() |
ParinyaT | 11:3dd92d1d542c | 420 | { |
b0ssiz | 17:4c96838e579f | 421 | uint8_t data_array[30]; |
b0ssiz | 14:28e24fcc5a01 | 422 | uint8_t id; |
b0ssiz | 14:28e24fcc5a01 | 423 | uint8_t ins; |
soulx | 18:face01c94152 | 424 | uint8_t status; |
b0ssiz | 14:28e24fcc5a01 | 425 | |
soulx | 18:face01c94152 | 426 | status = com.ReceiveCommand(&id,data_array,&ins); |
soulx | 18:face01c94152 | 427 | if(status == ANDANTE_ERRBIT_NONE) { |
soulx | 18:face01c94152 | 428 | CmdCheck((int16_t)id,data_array,ins); |
soulx | 18:face01c94152 | 429 | } |
ParinyaT | 11:3dd92d1d542c | 430 | } |
ParinyaT | 11:3dd92d1d542c | 431 | /*******************************************************/ |
ParinyaT | 0:451c27e4d55e | 432 | int main() |
ParinyaT | 0:451c27e4d55e | 433 | { |
soulx | 18:face01c94152 | 434 | while(1) { |
b0ssiz | 17:4c96838e579f | 435 | Rc(); |
soulx | 18:face01c94152 | 436 | } |
b0ssiz | 17:4c96838e579f | 437 | /*Recieve.attach(&Rc,0.000001); |
ParinyaT | 11:3dd92d1d542c | 438 | Start_Up(); |
ParinyaT | 1:84167ca00307 | 439 | SET_UpperPID(); |
ParinyaT | 1:84167ca00307 | 440 | SET_LowerPID(); |
ParinyaT | 11:3dd92d1d542c | 441 | |
b0ssiz | 14:28e24fcc5a01 | 442 | while(1) { |
b0ssiz | 14:28e24fcc5a01 | 443 | //Set Set_point |
b0ssiz | 14:28e24fcc5a01 | 444 | Up_PID.setSetPoint(Upper_SetPoint); |
b0ssiz | 14:28e24fcc5a01 | 445 | Low_PID.setSetPoint(Lower_SetPoint); |
ParinyaT | 1:84167ca00307 | 446 | |
b0ssiz | 14:28e24fcc5a01 | 447 | //Control Motor |
b0ssiz | 14:28e24fcc5a01 | 448 | Move_Upper(); |
b0ssiz | 14:28e24fcc5a01 | 449 | Move_Lower(); |
b0ssiz | 17:4c96838e579f | 450 | }*/ |
ParinyaT | 0:451c27e4d55e | 451 | } |
b0ssiz | 6:98871feebea0 | 452 | |
ParinyaT | 0:451c27e4d55e | 453 | |
ParinyaT | 0:451c27e4d55e | 454 | |
b0ssiz | 6:98871feebea0 | 455 | |
b0ssiz | 6:98871feebea0 | 456 | |
b0ssiz | 6:98871feebea0 | 457 |