![](/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@17:4c96838e579f, 2016-01-24 (annotated)
- Committer:
- b0ssiz
- Date:
- Sun Jan 24 07:57:55 2016 +0000
- Revision:
- 17:4c96838e579f
- Parent:
- 16:c0a1daeb9fa5
- Child:
- 18:face01c94152
Fixed bug Rc function
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: { |
b0ssiz | 14:28e24fcc5a01 | 157 | |
b0ssiz | 14:28e24fcc5a01 | 158 | } |
b0ssiz | 14:28e24fcc5a01 | 159 | case WRITE_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 160 | switch (cmd[0]) { |
b0ssiz | 14:28e24fcc5a01 | 161 | case ID: { |
b0ssiz | 14:28e24fcc5a01 | 162 | MY_ID = cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 163 | } |
b0ssiz | 14:28e24fcc5a01 | 164 | case MOTOR_UPPER_ANG: { |
b0ssiz | 14:28e24fcc5a01 | 165 | uint8_t IntUpAngle[2],FloatUpAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 166 | uint8_t IntLowAngle[2],FloatLowAngle[2]; |
b0ssiz | 14:28e24fcc5a01 | 167 | float int_buffer,float_buffer; |
b0ssiz | 14:28e24fcc5a01 | 168 | |
b0ssiz | 14:28e24fcc5a01 | 169 | IntUpAngle[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 170 | IntUpAngle[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 171 | FloatUpAngle[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 172 | FloatUpAngle[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 173 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntUpAngle); |
b0ssiz | 14:28e24fcc5a01 | 174 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatUpAngle)/FLOAT_CONVERTER; |
b0ssiz | 14:28e24fcc5a01 | 175 | up_angle=int_buffer+float_buffer; |
b0ssiz | 14:28e24fcc5a01 | 176 | |
b0ssiz | 14:28e24fcc5a01 | 177 | IntLowAngle[0]=cmd[5]; |
b0ssiz | 14:28e24fcc5a01 | 178 | IntLowAngle[1]=cmd[6]; |
b0ssiz | 14:28e24fcc5a01 | 179 | FloatLowAngle[0]=cmd[7]; |
b0ssiz | 14:28e24fcc5a01 | 180 | FloatLowAngle[1]=cmd[8]; |
b0ssiz | 14:28e24fcc5a01 | 181 | int_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(IntLowAngle); |
b0ssiz | 14:28e24fcc5a01 | 182 | float_buffer=(float)Utilities::ConvertUInt8ArrayToInt16(FloatLowAngle)/FLOAT_CONVERTER; |
b0ssiz | 14:28e24fcc5a01 | 183 | low_angle=int_buffer+float_buffer;; |
b0ssiz | 14:28e24fcc5a01 | 184 | } |
b0ssiz | 14:28e24fcc5a01 | 185 | case UP_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 186 | int i; |
b0ssiz | 14:28e24fcc5a01 | 187 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 188 | UpMargin[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 189 | } |
b0ssiz | 14:28e24fcc5a01 | 190 | } |
b0ssiz | 14:28e24fcc5a01 | 191 | case LOW_MARGIN: { |
b0ssiz | 14:28e24fcc5a01 | 192 | int i; |
b0ssiz | 14:28e24fcc5a01 | 193 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 194 | LowMargin[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 195 | } |
b0ssiz | 14:28e24fcc5a01 | 196 | } |
b0ssiz | 14:28e24fcc5a01 | 197 | case KP_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 198 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 199 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 200 | float Int,Float; |
b0ssiz | 14:28e24fcc5a01 | 201 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 202 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 203 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 204 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 205 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 206 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 207 | U_Kc=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 208 | } |
b0ssiz | 14:28e24fcc5a01 | 209 | case KI_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 210 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 211 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 212 | float Int,Float,KI; |
b0ssiz | 14:28e24fcc5a01 | 213 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 214 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 215 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 216 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 217 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 218 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 219 | KI=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 220 | U_Ti=KI/U_Kc; |
b0ssiz | 14:28e24fcc5a01 | 221 | } |
b0ssiz | 14:28e24fcc5a01 | 222 | case KD_UPPER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 223 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 224 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 225 | float Int,Float,KD; |
b0ssiz | 14:28e24fcc5a01 | 226 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 227 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 228 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 229 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 230 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 231 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 232 | KD=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 233 | U_Td=KD/U_Kc; |
b0ssiz | 14:28e24fcc5a01 | 234 | } |
b0ssiz | 14:28e24fcc5a01 | 235 | case KP_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 236 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 237 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 238 | float Int,Float; |
b0ssiz | 14:28e24fcc5a01 | 239 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 240 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 241 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 242 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 243 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 244 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 245 | L_Kc=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 246 | } |
b0ssiz | 14:28e24fcc5a01 | 247 | case KI_LOWER_MOTOR: { |
b0ssiz | 14:28e24fcc5a01 | 248 | uint8_t int_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 249 | uint8_t float_buffer[2]; |
b0ssiz | 14:28e24fcc5a01 | 250 | float Int,Float,KI; |
b0ssiz | 14:28e24fcc5a01 | 251 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 252 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 253 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 254 | float_buffer[1]=cmd[4]; |
b0ssiz | 14:28e24fcc5a01 | 255 | Int=(float)Utilities::ConvertUInt8ArrayToInt16(int_buffer); |
b0ssiz | 14:28e24fcc5a01 | 256 | Float=(float)Utilities::ConvertUInt8ArrayToInt16(float_buffer)/10000; |
b0ssiz | 14:28e24fcc5a01 | 257 | KI=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 258 | L_Ti=KI/L_Kc; |
b0ssiz | 14:28e24fcc5a01 | 259 | } |
b0ssiz | 14:28e24fcc5a01 | 260 | case KD_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,KD; |
b0ssiz | 14:28e24fcc5a01 | 264 | int_buffer[0]=cmd[1]; |
b0ssiz | 14:28e24fcc5a01 | 265 | int_buffer[1]=cmd[2]; |
b0ssiz | 14:28e24fcc5a01 | 266 | float_buffer[0]=cmd[3]; |
b0ssiz | 14:28e24fcc5a01 | 267 | float_buffer[1]=cmd[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 | KD=Int+Float; |
b0ssiz | 14:28e24fcc5a01 | 271 | L_Td=KD/L_Kc; |
b0ssiz | 14:28e24fcc5a01 | 272 | } |
b0ssiz | 14:28e24fcc5a01 | 273 | case HEIGHT: { |
b0ssiz | 14:28e24fcc5a01 | 274 | int i; |
b0ssiz | 14:28e24fcc5a01 | 275 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 276 | Height[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 277 | } |
b0ssiz | 14:28e24fcc5a01 | 278 | } |
b0ssiz | 14:28e24fcc5a01 | 279 | case WHEELPOS: { |
b0ssiz | 14:28e24fcc5a01 | 280 | int i; |
b0ssiz | 14:28e24fcc5a01 | 281 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 282 | Wheelpos[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 283 | } |
b0ssiz | 14:28e24fcc5a01 | 284 | } |
b0ssiz | 14:28e24fcc5a01 | 285 | case MAG_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 286 | int i; |
b0ssiz | 14:28e24fcc5a01 | 287 | for(i=0; i<24; i++) { |
b0ssiz | 14:28e24fcc5a01 | 288 | Mag[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 289 | } |
b0ssiz | 14:28e24fcc5a01 | 290 | } |
b0ssiz | 14:28e24fcc5a01 | 291 | case OFFSET: { |
b0ssiz | 14:28e24fcc5a01 | 292 | int i; |
b0ssiz | 14:28e24fcc5a01 | 293 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 294 | Offset[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 295 | } |
b0ssiz | 14:28e24fcc5a01 | 296 | } |
b0ssiz | 14:28e24fcc5a01 | 297 | case BODY_WIDTH: { |
b0ssiz | 14:28e24fcc5a01 | 298 | int i; |
b0ssiz | 14:28e24fcc5a01 | 299 | for(i=0; i<4; i++) { |
b0ssiz | 14:28e24fcc5a01 | 300 | Body_width[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 301 | } |
b0ssiz | 14:28e24fcc5a01 | 302 | } |
b0ssiz | 14:28e24fcc5a01 | 303 | case ANGLE_RANGE_UP: { |
b0ssiz | 14:28e24fcc5a01 | 304 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 305 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 306 | Angle_Range_Up[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 307 | } |
b0ssiz | 14:28e24fcc5a01 | 308 | } |
b0ssiz | 14:28e24fcc5a01 | 309 | case ANGLE_RANGE_LOW: { |
b0ssiz | 14:28e24fcc5a01 | 310 | int i; |
ParinyaT | 16:c0a1daeb9fa5 | 311 | for(i=0; i<8; i++) { |
b0ssiz | 14:28e24fcc5a01 | 312 | Angle_Range_Low[0+i]=cmd[1+i]; |
b0ssiz | 14:28e24fcc5a01 | 313 | } |
ParinyaT | 13:49cb002ad8fd | 314 | } |
ParinyaT | 16:c0a1daeb9fa5 | 315 | // unfinish yet!!!!!!!!!!!!!!!!! |
ParinyaT | 16:c0a1daeb9fa5 | 316 | case SAVE_EEPROM_DATA: { |
b0ssiz | 17:4c96838e579f | 317 | if (cmd[1]==ID){ |
ParinyaT | 16:c0a1daeb9fa5 | 318 | |
ParinyaT | 16:c0a1daeb9fa5 | 319 | } |
ParinyaT | 16:c0a1daeb9fa5 | 320 | } |
ParinyaT | 12:6b3b997dd7c2 | 321 | } |
b0ssiz | 14:28e24fcc5a01 | 322 | case READ_DATA: { |
b0ssiz | 14:28e24fcc5a01 | 323 | switch (cmd[0]) { |
b0ssiz | 14:28e24fcc5a01 | 324 | case MOTOR_UPPER_ANG: { |
ParinyaT | 16:c0a1daeb9fa5 | 325 | com.sendMotorPos(MY_ID,Upper_Position,Lower_Position); |
b0ssiz | 14:28e24fcc5a01 | 326 | } |
b0ssiz | 14:28e24fcc5a01 | 327 | case UP_MARGIN: { |
ParinyaT | 16:c0a1daeb9fa5 | 328 | com.sendUpMargin(MY_ID,UpMargin); |
b0ssiz | 14:28e24fcc5a01 | 329 | } |
b0ssiz | 14:28e24fcc5a01 | 330 | case LOW_MARGIN: { |
ParinyaT | 16:c0a1daeb9fa5 | 331 | com.sendLowMargin(MY_ID,LowMargin); |
b0ssiz | 14:28e24fcc5a01 | 332 | } |
b0ssiz | 14:28e24fcc5a01 | 333 | case PID_UPPER_MOTOR: { |
ParinyaT | 16:c0a1daeb9fa5 | 334 | com.sendUpMotorKpKiKd(MY_ID,U_Kc,U_Ki,U_Kd); |
b0ssiz | 14:28e24fcc5a01 | 335 | } |
b0ssiz | 14:28e24fcc5a01 | 336 | case PID_LOWER_MOTOR: { |
ParinyaT | 16:c0a1daeb9fa5 | 337 | com.sendLowMotorKpKiKd(MY_ID,L_Kc,L_Ki,L_Kd); |
b0ssiz | 14:28e24fcc5a01 | 338 | } |
b0ssiz | 14:28e24fcc5a01 | 339 | case HEIGHT: { |
ParinyaT | 16:c0a1daeb9fa5 | 340 | com.sendHeight(MY_ID,Height); |
b0ssiz | 14:28e24fcc5a01 | 341 | } |
b0ssiz | 14:28e24fcc5a01 | 342 | case WHEELPOS: { |
ParinyaT | 16:c0a1daeb9fa5 | 343 | com.sendWheelPos(MY_ID,Wheelpos); |
b0ssiz | 14:28e24fcc5a01 | 344 | } |
b0ssiz | 14:28e24fcc5a01 | 345 | case MAG_DATA: { |
ParinyaT | 16:c0a1daeb9fa5 | 346 | com.sendMagData(MY_ID,Mag); |
b0ssiz | 14:28e24fcc5a01 | 347 | } |
b0ssiz | 14:28e24fcc5a01 | 348 | case OFFSET: { |
ParinyaT | 16:c0a1daeb9fa5 | 349 | com.sendOffset(MY_ID,Offset); |
b0ssiz | 14:28e24fcc5a01 | 350 | } |
b0ssiz | 14:28e24fcc5a01 | 351 | case BODY_WIDTH: { |
ParinyaT | 16:c0a1daeb9fa5 | 352 | com.sendBodyWidth(MY_ID,Body_width); |
b0ssiz | 14:28e24fcc5a01 | 353 | } |
b0ssiz | 14:28e24fcc5a01 | 354 | case ANGLE_RANGE_UP: { |
ParinyaT | 16:c0a1daeb9fa5 | 355 | com.sendUpAngleRange(MY_ID,Angle_Range_Up); |
b0ssiz | 14:28e24fcc5a01 | 356 | } |
b0ssiz | 14:28e24fcc5a01 | 357 | case ANGLE_RANGE_LOW: { |
ParinyaT | 16:c0a1daeb9fa5 | 358 | com.sendLowAngleRange(MY_ID,Angle_Range_Low); |
b0ssiz | 14:28e24fcc5a01 | 359 | } |
ParinyaT | 13:49cb002ad8fd | 360 | } |
ParinyaT | 13:49cb002ad8fd | 361 | } |
ParinyaT | 13:49cb002ad8fd | 362 | } |
b0ssiz | 10:3b3d6bc88677 | 363 | } |
b0ssiz | 14:28e24fcc5a01 | 364 | } |
b0ssiz | 10:3b3d6bc88677 | 365 | } |
b0ssiz | 17:4c96838e579f | 366 | |
ParinyaT | 11:3dd92d1d542c | 367 | /******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 368 | void Start_Up() |
ParinyaT | 11:3dd92d1d542c | 369 | { |
ParinyaT | 11:3dd92d1d542c | 370 | // wait for reciever |
ParinyaT | 11:3dd92d1d542c | 371 | memory.read(0x00,MY_ID); |
ParinyaT | 11:3dd92d1d542c | 372 | memory.read(0x04,Upper_SetPoint); |
ParinyaT | 11:3dd92d1d542c | 373 | memory.read(0x08,Lower_SetPoint); |
ParinyaT | 11:3dd92d1d542c | 374 | memory.read(0x10,U_Kc); |
ParinyaT | 11:3dd92d1d542c | 375 | memory.read(0x14,U_Ti); |
ParinyaT | 11:3dd92d1d542c | 376 | memory.read(0x18,U_Td); |
ParinyaT | 11:3dd92d1d542c | 377 | memory.read(0x1c,L_Kc); |
ParinyaT | 11:3dd92d1d542c | 378 | memory.read(0x20,L_Ti); |
ParinyaT | 11:3dd92d1d542c | 379 | memory.read(0x24,L_Td); |
b0ssiz | 14:28e24fcc5a01 | 380 | |
ParinyaT | 11:3dd92d1d542c | 381 | } |
b0ssiz | 14:28e24fcc5a01 | 382 | /*******************************************************/ |
ParinyaT | 13:49cb002ad8fd | 383 | void Rc() |
ParinyaT | 11:3dd92d1d542c | 384 | { |
b0ssiz | 17:4c96838e579f | 385 | uint8_t data_array[30]; |
b0ssiz | 14:28e24fcc5a01 | 386 | uint8_t id; |
b0ssiz | 14:28e24fcc5a01 | 387 | uint8_t ins; |
b0ssiz | 14:28e24fcc5a01 | 388 | |
b0ssiz | 14:28e24fcc5a01 | 389 | com.ReceiveCommand(&id,data_array,&ins); |
b0ssiz | 15:6ebca0a1aaca | 390 | CmdCheck((int16_t)id,data_array,ins); |
ParinyaT | 11:3dd92d1d542c | 391 | } |
ParinyaT | 11:3dd92d1d542c | 392 | /*******************************************************/ |
ParinyaT | 0:451c27e4d55e | 393 | int main() |
ParinyaT | 0:451c27e4d55e | 394 | { |
b0ssiz | 17:4c96838e579f | 395 | while(1) |
b0ssiz | 17:4c96838e579f | 396 | { |
b0ssiz | 17:4c96838e579f | 397 | Rc(); |
b0ssiz | 17:4c96838e579f | 398 | } |
b0ssiz | 17:4c96838e579f | 399 | /*Recieve.attach(&Rc,0.000001); |
ParinyaT | 11:3dd92d1d542c | 400 | Start_Up(); |
ParinyaT | 1:84167ca00307 | 401 | SET_UpperPID(); |
ParinyaT | 1:84167ca00307 | 402 | SET_LowerPID(); |
ParinyaT | 11:3dd92d1d542c | 403 | |
b0ssiz | 14:28e24fcc5a01 | 404 | while(1) { |
b0ssiz | 14:28e24fcc5a01 | 405 | //Set Set_point |
b0ssiz | 14:28e24fcc5a01 | 406 | Up_PID.setSetPoint(Upper_SetPoint); |
b0ssiz | 14:28e24fcc5a01 | 407 | Low_PID.setSetPoint(Lower_SetPoint); |
ParinyaT | 1:84167ca00307 | 408 | |
b0ssiz | 14:28e24fcc5a01 | 409 | //Control Motor |
b0ssiz | 14:28e24fcc5a01 | 410 | Move_Upper(); |
b0ssiz | 14:28e24fcc5a01 | 411 | Move_Lower(); |
b0ssiz | 17:4c96838e579f | 412 | }*/ |
ParinyaT | 0:451c27e4d55e | 413 | } |
b0ssiz | 6:98871feebea0 | 414 | |
ParinyaT | 0:451c27e4d55e | 415 | |
ParinyaT | 0:451c27e4d55e | 416 | |
b0ssiz | 6:98871feebea0 | 417 | |
b0ssiz | 6:98871feebea0 | 418 | |
b0ssiz | 6:98871feebea0 | 419 |