Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Communication_Robot QEI iSerial mbed motion_control
Fork of dog_V3_2_testmotor by
main.cpp@8:8d30ce9a9463, 2015-07-17 (annotated)
- Committer:
- soulx
- Date:
- Fri Jul 17 12:07:19 2015 +0000
- Revision:
- 8:8d30ce9a9463
- Parent:
- 7:6a99be179329
- Child:
- 9:0de6ce956985
set lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soulx | 0:2433ddae2772 | 1 | #include "mbed.h" |
soulx | 0:2433ddae2772 | 2 | #include "pin_config.h" |
soulx | 0:2433ddae2772 | 3 | |
soulx | 6:8d80c84e0c09 | 4 | #include "communication.h" |
soulx | 6:8d80c84e0c09 | 5 | #include "protocol.h" |
soulx | 6:8d80c84e0c09 | 6 | |
soulx | 8:8d30ce9a9463 | 7 | #include "motor_relay.h" |
soulx | 8:8d30ce9a9463 | 8 | |
soulx | 6:8d80c84e0c09 | 9 | //set frequancy unit in Hz |
soulx | 6:8d80c84e0c09 | 10 | #define F_UPDATE 10.0f |
soulx | 6:8d80c84e0c09 | 11 | #define TIMER_UPDATE 1.0f/F_UPDATE |
soulx | 6:8d80c84e0c09 | 12 | |
soulx | 6:8d80c84e0c09 | 13 | //counter not receive from station |
soulx | 6:8d80c84e0c09 | 14 | #define TIMEOUT_RESPONE_COMMAND 5 |
soulx | 6:8d80c84e0c09 | 15 | |
soulx | 0:2433ddae2772 | 16 | //define pin class |
soulx | 8:8d30ce9a9463 | 17 | /* |
soulx | 0:2433ddae2772 | 18 | DigitalOut dirA_LU(INA_L_U); |
soulx | 0:2433ddae2772 | 19 | DigitalOut dirB_LU(INB_L_U); |
soulx | 0:2433ddae2772 | 20 | |
soulx | 0:2433ddae2772 | 21 | DigitalOut dirA_LL(INA_L_L); |
soulx | 0:2433ddae2772 | 22 | DigitalOut dirB_LL(INB_L_L); |
soulx | 0:2433ddae2772 | 23 | |
soulx | 0:2433ddae2772 | 24 | DigitalOut dirA_RU(INA_R_U); |
soulx | 0:2433ddae2772 | 25 | DigitalOut dirB_RU(INB_R_U); |
soulx | 0:2433ddae2772 | 26 | |
soulx | 0:2433ddae2772 | 27 | DigitalOut dirA_RL(INA_R_L); |
soulx | 0:2433ddae2772 | 28 | DigitalOut dirB_RL(INB_R_L); |
soulx | 8:8d30ce9a9463 | 29 | */ |
soulx | 8:8d30ce9a9463 | 30 | MOTOR_RELAY leg_left_upper(INA_L_U,INB_L_U); |
soulx | 8:8d30ce9a9463 | 31 | MOTOR_RELAY leg_left_lower(INA_L_L,INB_L_L); |
soulx | 8:8d30ce9a9463 | 32 | MOTOR_RELAY leg_right_upper(INA_R_U,INB_R_U); |
soulx | 8:8d30ce9a9463 | 33 | MOTOR_RELAY leg_right_lower(INA_R_L,INB_R_L); |
soulx | 0:2433ddae2772 | 34 | |
soulx | 8:8d30ce9a9463 | 35 | /* |
soulx | 0:2433ddae2772 | 36 | DigitalIn sw_LU_U(LIMIT_LU_U,PullUp); |
soulx | 0:2433ddae2772 | 37 | DigitalIn sw_LU_D(LIMIT_LU_D,PullUp); |
soulx | 0:2433ddae2772 | 38 | |
soulx | 0:2433ddae2772 | 39 | DigitalIn sw_LL_U(LIMIT_LL_U,PullUp); |
soulx | 0:2433ddae2772 | 40 | DigitalIn sw_LL_D(LIMIT_LL_D,PullUp); |
soulx | 0:2433ddae2772 | 41 | |
soulx | 0:2433ddae2772 | 42 | DigitalIn sw_RU_U(LIMIT_RU_U,PullUp); |
soulx | 0:2433ddae2772 | 43 | DigitalIn sw_RU_D(LIMIT_RU_D,PullUp); |
soulx | 0:2433ddae2772 | 44 | |
soulx | 0:2433ddae2772 | 45 | DigitalIn sw_RL_U(LIMIT_RL_U,PullUp); |
soulx | 0:2433ddae2772 | 46 | DigitalIn sw_RL_D(LIMIT_RL_D,PullUp); |
soulx | 8:8d30ce9a9463 | 47 | */ |
soulx | 0:2433ddae2772 | 48 | |
soulx | 0:2433ddae2772 | 49 | AnalogIn position_LU(VR_LU); |
soulx | 0:2433ddae2772 | 50 | AnalogIn position_LL(VR_LL); |
soulx | 0:2433ddae2772 | 51 | AnalogIn position_RU(VR_RU); |
soulx | 0:2433ddae2772 | 52 | AnalogIn position_RL(VR_RL); |
soulx | 0:2433ddae2772 | 53 | |
soulx | 0:2433ddae2772 | 54 | DigitalOut myled(LED1); |
soulx | 0:2433ddae2772 | 55 | DigitalIn mybutton(USER_BUTTON); |
soulx | 0:2433ddae2772 | 56 | |
soulx | 6:8d80c84e0c09 | 57 | //communication config |
soulx | 6:8d80c84e0c09 | 58 | //serial for debug |
soulx | 0:2433ddae2772 | 59 | Serial pc(USBTX, USBRX); |
soulx | 6:8d80c84e0c09 | 60 | //serial for xbee |
soulx | 6:8d80c84e0c09 | 61 | COMMUNICATION pan_a(TX_XBEE, RX_XBEE,115200,1000,1000); // tx, rx |
soulx | 0:2433ddae2772 | 62 | |
soulx | 0:2433ddae2772 | 63 | //Function Prototype |
soulx | 0:2433ddae2772 | 64 | void motor_set(uint8_t id, uint8_t direct); |
soulx | 0:2433ddae2772 | 65 | void motor_stop(uint8_t id); |
soulx | 0:2433ddae2772 | 66 | |
soulx | 0:2433ddae2772 | 67 | uint8_t limit_motor(uint8_t id, uint8_t dirction); |
soulx | 0:2433ddae2772 | 68 | uint8_t position_control(uint8_t id, uint16_t current, uint16_t target); |
soulx | 0:2433ddae2772 | 69 | |
soulx | 0:2433ddae2772 | 70 | void calibration(uint8_t id); |
soulx | 0:2433ddae2772 | 71 | |
soulx | 6:8d80c84e0c09 | 72 | void getCommand(); |
soulx | 6:8d80c84e0c09 | 73 | |
soulx | 0:2433ddae2772 | 74 | |
soulx | 0:2433ddae2772 | 75 | //Globle Variable |
soulx | 0:2433ddae2772 | 76 | uint16_t max_pos_LU= 10000; |
soulx | 0:2433ddae2772 | 77 | uint16_t min_pos_LU= 6000; |
soulx | 0:2433ddae2772 | 78 | uint16_t max_pos_LL= 50000; |
soulx | 0:2433ddae2772 | 79 | uint16_t min_pos_LL= 37000; |
soulx | 0:2433ddae2772 | 80 | |
soulx | 0:2433ddae2772 | 81 | uint16_t max_pos_RU= 17800; |
soulx | 0:2433ddae2772 | 82 | uint16_t min_pos_RU= 9000; |
soulx | 0:2433ddae2772 | 83 | uint16_t max_pos_RL= 51000; |
soulx | 0:2433ddae2772 | 84 | uint16_t min_pos_RL= 11000; |
soulx | 0:2433ddae2772 | 85 | |
soulx | 1:768d359e9d96 | 86 | uint16_t offset_pos =1000; |
Pitiwut | 3:bb5fbe510fa5 | 87 | uint16_t test_variable =1000; |
soulx | 1:768d359e9d96 | 88 | |
soulx | 0:2433ddae2772 | 89 | int16_t MARGIN = 500; |
soulx | 0:2433ddae2772 | 90 | |
soulx | 6:8d80c84e0c09 | 91 | //set foreground |
soulx | 6:8d80c84e0c09 | 92 | Ticker Update_command; |
soulx | 6:8d80c84e0c09 | 93 | |
soulx | 6:8d80c84e0c09 | 94 | |
soulx | 0:2433ddae2772 | 95 | //Main function |
soulx | 0:2433ddae2772 | 96 | int main() |
soulx | 0:2433ddae2772 | 97 | { |
soulx | 0:2433ddae2772 | 98 | uint16_t vr_lu,vr_ru; |
soulx | 0:2433ddae2772 | 99 | uint16_t vr_ll,vr_rl; |
soulx | 0:2433ddae2772 | 100 | pc.printf("wait\n"); |
soulx | 8:8d30ce9a9463 | 101 | //motor_stop(0); |
soulx | 0:2433ddae2772 | 102 | // wait(10); |
soulx | 0:2433ddae2772 | 103 | /* |
soulx | 0:2433ddae2772 | 104 | while(1) { |
soulx | 0:2433ddae2772 | 105 | motor_set(1,1); |
soulx | 0:2433ddae2772 | 106 | motor_set(2,1); |
soulx | 0:2433ddae2772 | 107 | motor_set(3,1); |
soulx | 0:2433ddae2772 | 108 | motor_set(4,1); |
soulx | 0:2433ddae2772 | 109 | wait(1); |
soulx | 0:2433ddae2772 | 110 | motor_set(1,2); |
soulx | 0:2433ddae2772 | 111 | motor_set(2,2); |
soulx | 0:2433ddae2772 | 112 | motor_set(3,2); |
soulx | 0:2433ddae2772 | 113 | motor_set(4,2); |
soulx | 0:2433ddae2772 | 114 | wait(1); |
soulx | 0:2433ddae2772 | 115 | } |
soulx | 0:2433ddae2772 | 116 | */ |
soulx | 8:8d30ce9a9463 | 117 | while(1) { |
soulx | 8:8d30ce9a9463 | 118 | leg_left_upper.SetMotor(1); |
soulx | 8:8d30ce9a9463 | 119 | leg_left_lower.SetMotor(1); |
soulx | 8:8d30ce9a9463 | 120 | leg_right_upper.SetMotor(1); |
soulx | 8:8d30ce9a9463 | 121 | leg_right_lower.SetMotor(1); |
soulx | 8:8d30ce9a9463 | 122 | } |
soulx | 0:2433ddae2772 | 123 | |
soulx | 0:2433ddae2772 | 124 | /* |
soulx | 0:2433ddae2772 | 125 | while(1) { |
soulx | 0:2433ddae2772 | 126 | //Read position |
soulx | 0:2433ddae2772 | 127 | vr_ll = position_LL.read_u16(); |
soulx | 0:2433ddae2772 | 128 | vr_lu = position_LU.read_u16(); |
soulx | 0:2433ddae2772 | 129 | vr_rl = position_RL.read_u16(); |
soulx | 0:2433ddae2772 | 130 | vr_ru = position_RU.read_u16(); |
soulx | 0:2433ddae2772 | 131 | pc.printf("vr_LL = %d\t",vr_ll); |
soulx | 0:2433ddae2772 | 132 | pc.printf("vr_LU = %d\t",vr_lu); |
soulx | 0:2433ddae2772 | 133 | pc.printf("vr_RL = %d\t",vr_rl); |
soulx | 0:2433ddae2772 | 134 | pc.printf("vr_RU = %d\n",vr_ru); |
soulx | 0:2433ddae2772 | 135 | } |
soulx | 0:2433ddae2772 | 136 | */ |
soulx | 0:2433ddae2772 | 137 | |
soulx | 0:2433ddae2772 | 138 | /* |
soulx | 5:f07cbb5a86c3 | 139 | while(1) { |
soulx | 5:f07cbb5a86c3 | 140 | if(sw_LU_U) { |
soulx | 5:f07cbb5a86c3 | 141 | pc.printf("sw_LU_U = 1"); |
soulx | 5:f07cbb5a86c3 | 142 | } else { |
soulx | 5:f07cbb5a86c3 | 143 | pc.printf("sw_LU_U = 0"); |
soulx | 5:f07cbb5a86c3 | 144 | } |
soulx | 5:f07cbb5a86c3 | 145 | |
soulx | 5:f07cbb5a86c3 | 146 | if(sw_LU_D) { |
soulx | 5:f07cbb5a86c3 | 147 | pc.printf(" sw_LU_D = 1"); |
soulx | 5:f07cbb5a86c3 | 148 | } else { |
soulx | 5:f07cbb5a86c3 | 149 | pc.printf(" sw_LU_D = 0"); |
soulx | 5:f07cbb5a86c3 | 150 | } |
soulx | 5:f07cbb5a86c3 | 151 | pc.printf("\n"); |
soulx | 5:f07cbb5a86c3 | 152 | /////////////////////////////////// |
soulx | 5:f07cbb5a86c3 | 153 | if(sw_LL_U) { |
soulx | 5:f07cbb5a86c3 | 154 | pc.printf("sw_LL_U = 1"); |
soulx | 5:f07cbb5a86c3 | 155 | } else { |
soulx | 5:f07cbb5a86c3 | 156 | pc.printf("sw_LL_U = 0"); |
soulx | 5:f07cbb5a86c3 | 157 | } |
soulx | 5:f07cbb5a86c3 | 158 | |
soulx | 5:f07cbb5a86c3 | 159 | if(sw_LL_D) { |
soulx | 5:f07cbb5a86c3 | 160 | pc.printf(" sw_LL_D = 1"); |
soulx | 5:f07cbb5a86c3 | 161 | } else { |
soulx | 5:f07cbb5a86c3 | 162 | pc.printf(" sw_LL_D = 0"); |
soulx | 5:f07cbb5a86c3 | 163 | } |
soulx | 5:f07cbb5a86c3 | 164 | pc.printf("\n"); |
soulx | 5:f07cbb5a86c3 | 165 | ////////////////////////////////////// |
soulx | 5:f07cbb5a86c3 | 166 | /////////////////////////////////// |
soulx | 5:f07cbb5a86c3 | 167 | |
soulx | 5:f07cbb5a86c3 | 168 | ///////////////////// |
soulx | 5:f07cbb5a86c3 | 169 | if(sw_RU_U) { |
soulx | 5:f07cbb5a86c3 | 170 | pc.printf("sw_RU_U = 1"); |
soulx | 5:f07cbb5a86c3 | 171 | } else { |
soulx | 5:f07cbb5a86c3 | 172 | pc.printf("sw_RU_U = 0"); |
soulx | 5:f07cbb5a86c3 | 173 | } |
soulx | 5:f07cbb5a86c3 | 174 | |
soulx | 5:f07cbb5a86c3 | 175 | if(sw_RU_D) { |
soulx | 5:f07cbb5a86c3 | 176 | pc.printf(" sw_RU_D = 1"); |
soulx | 5:f07cbb5a86c3 | 177 | } else { |
soulx | 5:f07cbb5a86c3 | 178 | pc.printf(" sw_RU_D = 0"); |
soulx | 5:f07cbb5a86c3 | 179 | } |
soulx | 5:f07cbb5a86c3 | 180 | pc.printf("\n"); |
soulx | 5:f07cbb5a86c3 | 181 | /////////////////////////////////// |
soulx | 5:f07cbb5a86c3 | 182 | if(sw_RL_U) { |
soulx | 5:f07cbb5a86c3 | 183 | pc.printf("sw_RL_U = 1"); |
soulx | 5:f07cbb5a86c3 | 184 | } else { |
soulx | 5:f07cbb5a86c3 | 185 | pc.printf("sw_RL_U = 0"); |
soulx | 5:f07cbb5a86c3 | 186 | } |
soulx | 5:f07cbb5a86c3 | 187 | |
soulx | 5:f07cbb5a86c3 | 188 | if(sw_RL_D) { |
soulx | 5:f07cbb5a86c3 | 189 | pc.printf(" sw_RL_D = 1"); |
soulx | 5:f07cbb5a86c3 | 190 | } else { |
soulx | 5:f07cbb5a86c3 | 191 | pc.printf(" sw_RL_D = 0"); |
soulx | 5:f07cbb5a86c3 | 192 | } |
soulx | 5:f07cbb5a86c3 | 193 | ////////////////////////////////////// |
soulx | 5:f07cbb5a86c3 | 194 | pc.printf("\n"); |
soulx | 5:f07cbb5a86c3 | 195 | pc.printf("\n"); |
soulx | 5:f07cbb5a86c3 | 196 | wait(1); |
soulx | 5:f07cbb5a86c3 | 197 | } |
soulx | 5:f07cbb5a86c3 | 198 | */ |
soulx | 5:f07cbb5a86c3 | 199 | |
soulx | 5:f07cbb5a86c3 | 200 | /* |
soulx | 0:2433ddae2772 | 201 | while(1) { |
soulx | 0:2433ddae2772 | 202 | myled =1; |
soulx | 0:2433ddae2772 | 203 | wait_ms(200); |
soulx | 0:2433ddae2772 | 204 | |
soulx | 0:2433ddae2772 | 205 | if(mybutton == 0) { |
soulx | 0:2433ddae2772 | 206 | myled =0; |
soulx | 0:2433ddae2772 | 207 | wait_ms(200); |
soulx | 0:2433ddae2772 | 208 | } |
soulx | 0:2433ddae2772 | 209 | |
soulx | 0:2433ddae2772 | 210 | if(sw_LU_U == 0) { |
soulx | 0:2433ddae2772 | 211 | myled =0; |
soulx | 0:2433ddae2772 | 212 | wait_ms(200); |
soulx | 0:2433ddae2772 | 213 | } |
soulx | 0:2433ddae2772 | 214 | |
soulx | 0:2433ddae2772 | 215 | if(sw_LU_D == 0) { |
soulx | 0:2433ddae2772 | 216 | myled =0; |
soulx | 0:2433ddae2772 | 217 | wait_ms(200); |
soulx | 0:2433ddae2772 | 218 | } |
soulx | 0:2433ddae2772 | 219 | |
soulx | 0:2433ddae2772 | 220 | if(sw_LL_U == 0) { |
soulx | 0:2433ddae2772 | 221 | myled =0; |
soulx | 0:2433ddae2772 | 222 | wait_ms(200); |
soulx | 0:2433ddae2772 | 223 | } |
soulx | 0:2433ddae2772 | 224 | |
soulx | 0:2433ddae2772 | 225 | if(sw_LL_D == 0) { |
soulx | 0:2433ddae2772 | 226 | myled =0; |
soulx | 0:2433ddae2772 | 227 | wait_ms(200); |
soulx | 0:2433ddae2772 | 228 | } |
soulx | 0:2433ddae2772 | 229 | |
soulx | 0:2433ddae2772 | 230 | if(sw_RU_U == 0) { |
soulx | 0:2433ddae2772 | 231 | myled =0; |
soulx | 0:2433ddae2772 | 232 | wait_ms(200); |
soulx | 0:2433ddae2772 | 233 | } |
soulx | 0:2433ddae2772 | 234 | |
soulx | 0:2433ddae2772 | 235 | if(sw_RU_D == 0) { |
soulx | 0:2433ddae2772 | 236 | myled =0; |
soulx | 0:2433ddae2772 | 237 | wait_ms(200); |
soulx | 0:2433ddae2772 | 238 | } |
soulx | 0:2433ddae2772 | 239 | |
soulx | 0:2433ddae2772 | 240 | if(sw_RL_U == 0) { |
soulx | 0:2433ddae2772 | 241 | myled =0; |
soulx | 0:2433ddae2772 | 242 | wait_ms(200); |
soulx | 0:2433ddae2772 | 243 | } |
soulx | 0:2433ddae2772 | 244 | |
soulx | 0:2433ddae2772 | 245 | if(sw_RL_D == 0) { |
soulx | 0:2433ddae2772 | 246 | myled =0; |
soulx | 0:2433ddae2772 | 247 | wait_ms(200); |
soulx | 0:2433ddae2772 | 248 | } |
soulx | 0:2433ddae2772 | 249 | |
soulx | 0:2433ddae2772 | 250 | } |
soulx | 0:2433ddae2772 | 251 | */ |
soulx | 0:2433ddae2772 | 252 | |
soulx | 7:6a99be179329 | 253 | while(1) { |
soulx | 7:6a99be179329 | 254 | //calibration |
soulx | 7:6a99be179329 | 255 | pc.printf("Welcome to DOGWHEELSCHAIR\n"); |
soulx | 7:6a99be179329 | 256 | pc.printf("Calibration [START]\n"); |
soulx | 7:6a99be179329 | 257 | calibration(1); |
soulx | 7:6a99be179329 | 258 | calibration(2); |
soulx | 7:6a99be179329 | 259 | calibration(3); |
soulx | 7:6a99be179329 | 260 | calibration(4); |
soulx | 7:6a99be179329 | 261 | pc.printf("Calibration [FINISH]\n"); |
soulx | 7:6a99be179329 | 262 | pc.printf("RUN mode [START]\n"); |
soulx | 7:6a99be179329 | 263 | } |
soulx | 0:2433ddae2772 | 264 | |
soulx | 6:8d80c84e0c09 | 265 | Update_command.attach(&getCommand,TIMER_UPDATE); |
soulx | 0:2433ddae2772 | 266 | |
soulx | 0:2433ddae2772 | 267 | uint8_t count=0; |
soulx | 0:2433ddae2772 | 268 | |
soulx | 0:2433ddae2772 | 269 | while(1) { |
soulx | 0:2433ddae2772 | 270 | uint8_t state=0; |
soulx | 0:2433ddae2772 | 271 | pc.printf("Count %d",count); |
soulx | 0:2433ddae2772 | 272 | do { |
soulx | 0:2433ddae2772 | 273 | state=0; |
soulx | 0:2433ddae2772 | 274 | |
soulx | 1:768d359e9d96 | 275 | if(position_control(1,position_LU.read_u16(),max_pos_LU-offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 276 | state++; |
soulx | 0:2433ddae2772 | 277 | } |
soulx | 0:2433ddae2772 | 278 | |
soulx | 1:768d359e9d96 | 279 | if(position_control(2,position_LL.read_u16(),max_pos_LL-offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 280 | state++; |
soulx | 0:2433ddae2772 | 281 | } |
soulx | 0:2433ddae2772 | 282 | |
soulx | 1:768d359e9d96 | 283 | if(position_control(3,position_RU.read_u16(),max_pos_RU-offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 284 | state++; |
soulx | 0:2433ddae2772 | 285 | } |
soulx | 0:2433ddae2772 | 286 | |
soulx | 1:768d359e9d96 | 287 | if(position_control(4,position_RL.read_u16(),max_pos_RL-offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 288 | state++; |
soulx | 0:2433ddae2772 | 289 | } |
soulx | 0:2433ddae2772 | 290 | |
soulx | 0:2433ddae2772 | 291 | pc.printf("state = %d",state); |
soulx | 0:2433ddae2772 | 292 | } while(state <= 4 ); |
soulx | 0:2433ddae2772 | 293 | |
soulx | 0:2433ddae2772 | 294 | do { |
soulx | 0:2433ddae2772 | 295 | state=0; |
soulx | 0:2433ddae2772 | 296 | |
soulx | 1:768d359e9d96 | 297 | if(position_control(1,position_LU.read_u16(),min_pos_LU+offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 298 | state++; |
soulx | 0:2433ddae2772 | 299 | } |
soulx | 0:2433ddae2772 | 300 | |
soulx | 1:768d359e9d96 | 301 | if(position_control(2,position_LL.read_u16(),min_pos_LL+offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 302 | state++; |
soulx | 0:2433ddae2772 | 303 | } |
soulx | 0:2433ddae2772 | 304 | |
soulx | 1:768d359e9d96 | 305 | if(position_control(3,position_RU.read_u16(),min_pos_RU+offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 306 | state++; |
soulx | 0:2433ddae2772 | 307 | } |
soulx | 0:2433ddae2772 | 308 | |
soulx | 1:768d359e9d96 | 309 | if(position_control(4,position_RL.read_u16(),min_pos_RL+offset_pos) == 1) { |
soulx | 0:2433ddae2772 | 310 | state++; |
soulx | 0:2433ddae2772 | 311 | } |
soulx | 0:2433ddae2772 | 312 | |
soulx | 0:2433ddae2772 | 313 | pc.printf("state = %d",state); |
soulx | 0:2433ddae2772 | 314 | } while(state <= 4 ); |
soulx | 0:2433ddae2772 | 315 | count++; |
soulx | 0:2433ddae2772 | 316 | } |
soulx | 0:2433ddae2772 | 317 | } |
soulx | 0:2433ddae2772 | 318 | |
soulx | 0:2433ddae2772 | 319 | |
soulx | 8:8d30ce9a9463 | 320 | /* |
soulx | 0:2433ddae2772 | 321 | void motor_set(uint8_t id, uint8_t direct) |
soulx | 0:2433ddae2772 | 322 | { |
soulx | 0:2433ddae2772 | 323 | //direct: Should be between 0 and 3, with the following result |
soulx | 0:2433ddae2772 | 324 | //0: Brake to VCC |
soulx | 0:2433ddae2772 | 325 | //1: Clockwise |
soulx | 0:2433ddae2772 | 326 | //2: CounterClockwise |
soulx | 0:2433ddae2772 | 327 | //3: Brake to GND |
soulx | 0:2433ddae2772 | 328 | |
soulx | 0:2433ddae2772 | 329 | if(direct <=4) { |
soulx | 0:2433ddae2772 | 330 | switch(id) { |
soulx | 0:2433ddae2772 | 331 | case 1: |
soulx | 0:2433ddae2772 | 332 | // Set inA[motor] |
soulx | 0:2433ddae2772 | 333 | if (direct <=1) |
soulx | 0:2433ddae2772 | 334 | dirA_LU=0; |
soulx | 0:2433ddae2772 | 335 | else |
soulx | 0:2433ddae2772 | 336 | dirA_LU=1; |
soulx | 0:2433ddae2772 | 337 | |
soulx | 0:2433ddae2772 | 338 | // Set inB[motor] |
soulx | 0:2433ddae2772 | 339 | if ((direct==0)||(direct==2)) |
soulx | 0:2433ddae2772 | 340 | dirB_LU=0; |
soulx | 0:2433ddae2772 | 341 | else |
soulx | 0:2433ddae2772 | 342 | dirB_LU=1; |
soulx | 0:2433ddae2772 | 343 | break; |
soulx | 0:2433ddae2772 | 344 | |
soulx | 0:2433ddae2772 | 345 | case 2: |
soulx | 0:2433ddae2772 | 346 | // Set inA[motor] |
soulx | 0:2433ddae2772 | 347 | if (direct <=1) |
soulx | 0:2433ddae2772 | 348 | dirA_LL=0; |
soulx | 0:2433ddae2772 | 349 | else |
soulx | 0:2433ddae2772 | 350 | dirA_LL=1; |
soulx | 0:2433ddae2772 | 351 | |
soulx | 0:2433ddae2772 | 352 | // Set inB[motor] |
soulx | 0:2433ddae2772 | 353 | if ((direct==0)||(direct==2)) |
soulx | 0:2433ddae2772 | 354 | dirB_LL=0; |
soulx | 0:2433ddae2772 | 355 | else |
soulx | 0:2433ddae2772 | 356 | dirB_LL=1; |
soulx | 0:2433ddae2772 | 357 | break; |
soulx | 0:2433ddae2772 | 358 | |
soulx | 0:2433ddae2772 | 359 | case 3: |
soulx | 0:2433ddae2772 | 360 | // Set inA[motor] |
soulx | 0:2433ddae2772 | 361 | if (direct <=1) |
soulx | 0:2433ddae2772 | 362 | dirA_RU=0; |
soulx | 0:2433ddae2772 | 363 | else |
soulx | 0:2433ddae2772 | 364 | dirA_RU=1; |
soulx | 0:2433ddae2772 | 365 | |
soulx | 0:2433ddae2772 | 366 | // Set inB[motor] |
soulx | 0:2433ddae2772 | 367 | if ((direct==0)||(direct==2)) |
soulx | 0:2433ddae2772 | 368 | dirB_RU=0; |
soulx | 0:2433ddae2772 | 369 | else |
soulx | 0:2433ddae2772 | 370 | dirB_RU=1; |
soulx | 0:2433ddae2772 | 371 | break; |
soulx | 0:2433ddae2772 | 372 | |
soulx | 0:2433ddae2772 | 373 | case 4: |
soulx | 0:2433ddae2772 | 374 | // Set inA[motor] |
soulx | 0:2433ddae2772 | 375 | if (direct <=1) |
soulx | 0:2433ddae2772 | 376 | dirA_RL=0; |
soulx | 0:2433ddae2772 | 377 | else |
soulx | 0:2433ddae2772 | 378 | dirA_RL=1; |
soulx | 0:2433ddae2772 | 379 | // Set inB[motor] |
soulx | 0:2433ddae2772 | 380 | if ((direct==0)||(direct==2)) |
soulx | 0:2433ddae2772 | 381 | dirB_RL=0; |
soulx | 0:2433ddae2772 | 382 | else |
soulx | 0:2433ddae2772 | 383 | dirB_RL=1; |
soulx | 0:2433ddae2772 | 384 | break; |
soulx | 0:2433ddae2772 | 385 | } |
soulx | 0:2433ddae2772 | 386 | } |
soulx | 0:2433ddae2772 | 387 | } |
soulx | 0:2433ddae2772 | 388 | |
soulx | 0:2433ddae2772 | 389 | void motor_stop(uint8_t id) |
soulx | 0:2433ddae2772 | 390 | { |
soulx | 0:2433ddae2772 | 391 | switch(id) { |
soulx | 0:2433ddae2772 | 392 | case 1: |
soulx | 0:2433ddae2772 | 393 | dirA_LU=1; |
soulx | 0:2433ddae2772 | 394 | dirB_LU=1; |
soulx | 0:2433ddae2772 | 395 | break; |
soulx | 0:2433ddae2772 | 396 | |
soulx | 0:2433ddae2772 | 397 | case 2: |
soulx | 0:2433ddae2772 | 398 | dirA_LL=1; |
soulx | 0:2433ddae2772 | 399 | dirB_LL=1; |
soulx | 0:2433ddae2772 | 400 | break; |
soulx | 0:2433ddae2772 | 401 | |
soulx | 0:2433ddae2772 | 402 | case 3: |
soulx | 0:2433ddae2772 | 403 | dirA_RU=1; |
soulx | 0:2433ddae2772 | 404 | dirB_RU=1; |
soulx | 0:2433ddae2772 | 405 | break; |
soulx | 0:2433ddae2772 | 406 | |
soulx | 0:2433ddae2772 | 407 | case 4: |
soulx | 0:2433ddae2772 | 408 | dirA_RL=1; |
soulx | 0:2433ddae2772 | 409 | dirB_RL=1; |
soulx | 0:2433ddae2772 | 410 | break; |
soulx | 0:2433ddae2772 | 411 | |
soulx | 0:2433ddae2772 | 412 | case 0: |
soulx | 0:2433ddae2772 | 413 | dirA_LU=1; |
soulx | 0:2433ddae2772 | 414 | dirB_LU=1; |
soulx | 0:2433ddae2772 | 415 | |
soulx | 0:2433ddae2772 | 416 | dirA_LL=1; |
soulx | 0:2433ddae2772 | 417 | dirB_LL=1; |
soulx | 0:2433ddae2772 | 418 | |
soulx | 0:2433ddae2772 | 419 | dirA_RU=1; |
soulx | 0:2433ddae2772 | 420 | dirB_RU=1; |
soulx | 0:2433ddae2772 | 421 | |
soulx | 0:2433ddae2772 | 422 | dirA_RL=1; |
soulx | 0:2433ddae2772 | 423 | dirB_RL=1; |
soulx | 0:2433ddae2772 | 424 | break; |
soulx | 0:2433ddae2772 | 425 | } |
soulx | 0:2433ddae2772 | 426 | } |
soulx | 8:8d30ce9a9463 | 427 | */ |
soulx | 0:2433ddae2772 | 428 | |
soulx | 8:8d30ce9a9463 | 429 | /* |
soulx | 0:2433ddae2772 | 430 | uint8_t limit_motor(uint8_t id, uint8_t dirction) |
soulx | 0:2433ddae2772 | 431 | { |
soulx | 0:2433ddae2772 | 432 | switch(id) { |
soulx | 0:2433ddae2772 | 433 | case 1://Left Upper |
soulx | 5:f07cbb5a86c3 | 434 | if(~sw_LU_U && ~sw_LU_D) { |
soulx | 0:2433ddae2772 | 435 | motor_set(id,dirction); |
soulx | 0:2433ddae2772 | 436 | } else { |
soulx | 0:2433ddae2772 | 437 | motor_stop(id); |
soulx | 0:2433ddae2772 | 438 | return 0; |
soulx | 0:2433ddae2772 | 439 | } |
soulx | 0:2433ddae2772 | 440 | break; |
soulx | 0:2433ddae2772 | 441 | |
soulx | 0:2433ddae2772 | 442 | case 2://Left Lowwer |
soulx | 5:f07cbb5a86c3 | 443 | if(~sw_LL_U && ~sw_LL_D) { |
soulx | 0:2433ddae2772 | 444 | motor_set(id,dirction); |
soulx | 0:2433ddae2772 | 445 | } else { |
soulx | 0:2433ddae2772 | 446 | motor_stop(id); |
soulx | 0:2433ddae2772 | 447 | return 0; |
soulx | 0:2433ddae2772 | 448 | } |
soulx | 0:2433ddae2772 | 449 | break; |
soulx | 0:2433ddae2772 | 450 | |
soulx | 0:2433ddae2772 | 451 | case 3://Right Upper |
soulx | 5:f07cbb5a86c3 | 452 | if(~sw_RU_U && ~sw_RU_D) { |
soulx | 0:2433ddae2772 | 453 | motor_set(id,dirction); |
soulx | 0:2433ddae2772 | 454 | } else { |
soulx | 0:2433ddae2772 | 455 | motor_stop(id); |
soulx | 0:2433ddae2772 | 456 | return 0; |
soulx | 0:2433ddae2772 | 457 | } |
soulx | 0:2433ddae2772 | 458 | break; |
soulx | 0:2433ddae2772 | 459 | |
soulx | 0:2433ddae2772 | 460 | case 4://Right Lowwer |
soulx | 5:f07cbb5a86c3 | 461 | if(~sw_RL_U && ~sw_RL_D) { |
soulx | 0:2433ddae2772 | 462 | motor_set(id,dirction); |
soulx | 0:2433ddae2772 | 463 | } else { |
soulx | 0:2433ddae2772 | 464 | motor_stop(id); |
soulx | 0:2433ddae2772 | 465 | return 0; |
soulx | 0:2433ddae2772 | 466 | } |
soulx | 0:2433ddae2772 | 467 | break; |
soulx | 0:2433ddae2772 | 468 | } |
soulx | 0:2433ddae2772 | 469 | return 1;//normally |
soulx | 0:2433ddae2772 | 470 | } |
soulx | 0:2433ddae2772 | 471 | |
soulx | 0:2433ddae2772 | 472 | |
soulx | 0:2433ddae2772 | 473 | uint8_t position_control(uint8_t id, uint16_t current, uint16_t target) |
soulx | 0:2433ddae2772 | 474 | { |
soulx | 0:2433ddae2772 | 475 | //uint8_t state=0; |
soulx | 0:2433ddae2772 | 476 | int16_t error = target-current; |
soulx | 0:2433ddae2772 | 477 | |
soulx | 0:2433ddae2772 | 478 | pc.printf("error[%d]=%d\n",id,error); |
soulx | 0:2433ddae2772 | 479 | if(error > MARGIN) { |
soulx | 0:2433ddae2772 | 480 | if(limit_motor(id,1)==0 ) { //limit sens |
soulx | 0:2433ddae2772 | 481 | |
soulx | 0:2433ddae2772 | 482 | pc.printf("motor[%d]=limit error\n",id); |
soulx | 0:2433ddae2772 | 483 | |
soulx | 0:2433ddae2772 | 484 | return 2; |
soulx | 0:2433ddae2772 | 485 | } |
soulx | 0:2433ddae2772 | 486 | |
soulx | 0:2433ddae2772 | 487 | |
soulx | 0:2433ddae2772 | 488 | } else if(error < -MARGIN) { |
soulx | 0:2433ddae2772 | 489 | if(limit_motor(id,2)==0 ) { //limit sens |
soulx | 0:2433ddae2772 | 490 | |
soulx | 0:2433ddae2772 | 491 | pc.printf("motor[%d]=limit error\n",id); |
soulx | 0:2433ddae2772 | 492 | |
soulx | 0:2433ddae2772 | 493 | return 2; |
soulx | 0:2433ddae2772 | 494 | } |
soulx | 0:2433ddae2772 | 495 | |
soulx | 0:2433ddae2772 | 496 | } else { //in zone |
soulx | 0:2433ddae2772 | 497 | motor_stop(2); |
soulx | 0:2433ddae2772 | 498 | |
soulx | 0:2433ddae2772 | 499 | pc.printf("motor[%d]=complete\n",id); |
soulx | 0:2433ddae2772 | 500 | |
soulx | 0:2433ddae2772 | 501 | return 1; //in zone complete |
soulx | 0:2433ddae2772 | 502 | } |
soulx | 0:2433ddae2772 | 503 | |
soulx | 0:2433ddae2772 | 504 | pc.printf("motor[%d]=in process\n",id); |
soulx | 0:2433ddae2772 | 505 | return 0; //in process |
soulx | 0:2433ddae2772 | 506 | } |
soulx | 0:2433ddae2772 | 507 | |
soulx | 0:2433ddae2772 | 508 | void calibration(uint8_t id) |
soulx | 0:2433ddae2772 | 509 | { |
soulx | 0:2433ddae2772 | 510 | switch(id) { |
soulx | 0:2433ddae2772 | 511 | case 1: |
soulx | 0:2433ddae2772 | 512 | pc.printf("motor[1] run up\n"); |
soulx | 5:f07cbb5a86c3 | 513 | do { |
soulx | 0:2433ddae2772 | 514 | motor_set(id,1); |
soulx | 5:f07cbb5a86c3 | 515 | } while(sw_LU_U ==0); |
soulx | 0:2433ddae2772 | 516 | pc.printf("motor[1] stop up\n"); |
soulx | 0:2433ddae2772 | 517 | //wait_ms(500); |
soulx | 0:2433ddae2772 | 518 | do { |
soulx | 0:2433ddae2772 | 519 | motor_set(id,2); |
soulx | 5:f07cbb5a86c3 | 520 | } while(sw_LU_U); |
soulx | 0:2433ddae2772 | 521 | motor_stop(id); |
soulx | 0:2433ddae2772 | 522 | wait_ms(500); |
soulx | 0:2433ddae2772 | 523 | pc.printf("motor[1] read position\n"); |
soulx | 0:2433ddae2772 | 524 | max_pos_LU = position_LU.read_u16(); |
soulx | 0:2433ddae2772 | 525 | pc.printf("max_pos_LU= %d\n",max_pos_LU); |
soulx | 0:2433ddae2772 | 526 | |
soulx | 0:2433ddae2772 | 527 | pc.printf("motor[1] run down\n"); |
soulx | 5:f07cbb5a86c3 | 528 | do { |
soulx | 0:2433ddae2772 | 529 | motor_set(id,2); |
soulx | 5:f07cbb5a86c3 | 530 | } while(sw_LU_D==0) ; |
soulx | 0:2433ddae2772 | 531 | pc.printf("motor[1] stop down\n"); |
soulx | 0:2433ddae2772 | 532 | do { |
soulx | 0:2433ddae2772 | 533 | motor_set(id,1); |
soulx | 5:f07cbb5a86c3 | 534 | } while(sw_LU_D); |
soulx | 0:2433ddae2772 | 535 | motor_stop(id); |
soulx | 0:2433ddae2772 | 536 | wait_ms(500); |
soulx | 0:2433ddae2772 | 537 | pc.printf("motor[1] read position\n"); |
soulx | 0:2433ddae2772 | 538 | min_pos_LU = position_LU.read_u16(); |
soulx | 0:2433ddae2772 | 539 | pc.printf("min_pos_LU= %d\n",min_pos_LU); |
soulx | 0:2433ddae2772 | 540 | break; |
soulx | 0:2433ddae2772 | 541 | |
soulx | 0:2433ddae2772 | 542 | case 2: |
soulx | 0:2433ddae2772 | 543 | |
soulx | 0:2433ddae2772 | 544 | pc.printf("motor[2] run up\n"); |
soulx | 7:6a99be179329 | 545 | do { |
soulx | 0:2433ddae2772 | 546 | motor_set(id,1); |
soulx | 7:6a99be179329 | 547 | } while(sw_LL_U==0) ; |
soulx | 0:2433ddae2772 | 548 | motor_stop(id); |
soulx | 0:2433ddae2772 | 549 | wait_ms(500); |
soulx | 0:2433ddae2772 | 550 | pc.printf("motor[2] stop up\n"); |
soulx | 0:2433ddae2772 | 551 | do { |
soulx | 0:2433ddae2772 | 552 | motor_set(id,2); |
soulx | 5:f07cbb5a86c3 | 553 | } while(sw_LL_U ); |
soulx | 0:2433ddae2772 | 554 | motor_stop(id); |
soulx | 0:2433ddae2772 | 555 | wait_ms(500); |
soulx | 0:2433ddae2772 | 556 | max_pos_LL = position_LL.read_u16(); |
soulx | 0:2433ddae2772 | 557 | pc.printf("max_pos_LL= %d\n",max_pos_LL); |
soulx | 0:2433ddae2772 | 558 | pc.printf("motor[2] run down\n"); |
soulx | 7:6a99be179329 | 559 | do { |
soulx | 0:2433ddae2772 | 560 | motor_set(id,2); |
soulx | 7:6a99be179329 | 561 | } while(sw_LL_D==0) ; |
soulx | 0:2433ddae2772 | 562 | motor_stop(id); |
soulx | 0:2433ddae2772 | 563 | wait_ms(500); |
soulx | 0:2433ddae2772 | 564 | pc.printf("motor[2] stop down\n"); |
soulx | 0:2433ddae2772 | 565 | do { |
soulx | 0:2433ddae2772 | 566 | motor_set(id,1); |
soulx | 5:f07cbb5a86c3 | 567 | } while(sw_LL_D); |
soulx | 0:2433ddae2772 | 568 | motor_stop(id); |
soulx | 0:2433ddae2772 | 569 | wait_ms(500); |
soulx | 0:2433ddae2772 | 570 | min_pos_LL = position_LL.read_u16(); |
soulx | 0:2433ddae2772 | 571 | pc.printf("min_pos_LL= %d\n",min_pos_LL); |
soulx | 0:2433ddae2772 | 572 | break; |
soulx | 0:2433ddae2772 | 573 | |
soulx | 0:2433ddae2772 | 574 | case 3: |
soulx | 5:f07cbb5a86c3 | 575 | // uint8_t count=1000; |
soulx | 0:2433ddae2772 | 576 | pc.printf("motor[3] run up\n"); |
soulx | 5:f07cbb5a86c3 | 577 | do { |
soulx | 0:2433ddae2772 | 578 | motor_set(id,1); |
soulx | 7:6a99be179329 | 579 | } while(sw_RU_U==0); |
soulx | 0:2433ddae2772 | 580 | motor_stop(id); |
soulx | 0:2433ddae2772 | 581 | wait_ms(500); |
soulx | 0:2433ddae2772 | 582 | pc.printf("motor[3] stop up\n"); |
soulx | 1:768d359e9d96 | 583 | |
soulx | 0:2433ddae2772 | 584 | do { |
soulx | 0:2433ddae2772 | 585 | motor_set(id,2); |
soulx | 5:f07cbb5a86c3 | 586 | } while(sw_RU_U); |
soulx | 0:2433ddae2772 | 587 | motor_stop(id); |
soulx | 0:2433ddae2772 | 588 | wait_ms(500); |
soulx | 0:2433ddae2772 | 589 | max_pos_RU = position_RU.read_u16(); |
soulx | 0:2433ddae2772 | 590 | pc.printf("max_pos_RU= %d\n",max_pos_RU); |
soulx | 0:2433ddae2772 | 591 | |
soulx | 0:2433ddae2772 | 592 | pc.printf("motor[3] run down\n"); |
soulx | 1:768d359e9d96 | 593 | |
soulx | 7:6a99be179329 | 594 | do { |
soulx | 0:2433ddae2772 | 595 | motor_set(id,2); |
soulx | 7:6a99be179329 | 596 | } while(sw_RU_D==0); |
soulx | 0:2433ddae2772 | 597 | motor_stop(id); |
soulx | 0:2433ddae2772 | 598 | wait_ms(500); |
soulx | 0:2433ddae2772 | 599 | pc.printf("motor[3] stop down\n"); |
soulx | 8:8d30ce9a9463 | 600 | |
soulx | 0:2433ddae2772 | 601 | do { |
soulx | 0:2433ddae2772 | 602 | motor_set(id,1); |
soulx | 5:f07cbb5a86c3 | 603 | } while(sw_RU_D); |
soulx | 0:2433ddae2772 | 604 | motor_stop(id); |
soulx | 0:2433ddae2772 | 605 | wait_ms(500); |
soulx | 0:2433ddae2772 | 606 | min_pos_RU = position_RU.read_u16(); |
soulx | 0:2433ddae2772 | 607 | pc.printf("min_pos_RU= %d\n",min_pos_RU); |
soulx | 0:2433ddae2772 | 608 | break; |
soulx | 0:2433ddae2772 | 609 | |
soulx | 0:2433ddae2772 | 610 | case 4: |
soulx | 0:2433ddae2772 | 611 | |
soulx | 0:2433ddae2772 | 612 | pc.printf("motor[4] run up\n"); |
soulx | 5:f07cbb5a86c3 | 613 | do { |
soulx | 0:2433ddae2772 | 614 | motor_set(id,1); |
soulx | 7:6a99be179329 | 615 | } while(sw_RL_U==0); |
soulx | 0:2433ddae2772 | 616 | motor_stop(id); |
soulx | 0:2433ddae2772 | 617 | wait_ms(500); |
soulx | 0:2433ddae2772 | 618 | pc.printf("motor[4] stop up\n"); |
soulx | 0:2433ddae2772 | 619 | do { |
soulx | 0:2433ddae2772 | 620 | motor_set(id,2); |
soulx | 5:f07cbb5a86c3 | 621 | } while(sw_RL_U); |
soulx | 0:2433ddae2772 | 622 | motor_stop(id); |
soulx | 0:2433ddae2772 | 623 | wait_ms(500); |
soulx | 0:2433ddae2772 | 624 | max_pos_RL = position_RL.read_u16(); |
soulx | 0:2433ddae2772 | 625 | pc.printf("max_pos_RL= %d\n",max_pos_RL); |
soulx | 0:2433ddae2772 | 626 | pc.printf("motor[4] run down\n"); |
soulx | 7:6a99be179329 | 627 | do { |
soulx | 0:2433ddae2772 | 628 | motor_set(id,2); |
soulx | 7:6a99be179329 | 629 | } while(sw_RL_D==0); |
soulx | 0:2433ddae2772 | 630 | motor_stop(id); |
soulx | 0:2433ddae2772 | 631 | wait_ms(500); |
soulx | 0:2433ddae2772 | 632 | pc.printf("motor[4] stop down\n"); |
soulx | 0:2433ddae2772 | 633 | do { |
soulx | 0:2433ddae2772 | 634 | motor_set(id,1); |
soulx | 5:f07cbb5a86c3 | 635 | } while(sw_RL_D); |
soulx | 0:2433ddae2772 | 636 | motor_stop(id); |
soulx | 0:2433ddae2772 | 637 | wait_ms(500); |
soulx | 0:2433ddae2772 | 638 | min_pos_RL = position_RL.read_u16(); |
soulx | 0:2433ddae2772 | 639 | pc.printf("min_pos_RL= %d\n",min_pos_RL); |
soulx | 0:2433ddae2772 | 640 | break; |
soulx | 0:2433ddae2772 | 641 | } |
soulx | 0:2433ddae2772 | 642 | |
soulx | 1:768d359e9d96 | 643 | } |
soulx | 8:8d30ce9a9463 | 644 | |
soulx | 8:8d30ce9a9463 | 645 | */ |
soulx | 8:8d30ce9a9463 | 646 | |
soulx | 8:8d30ce9a9463 | 647 | |
soulx | 5:f07cbb5a86c3 | 648 | /* |
soulx | 1:768d359e9d96 | 649 | uint16_t convert(uint16_t data) |
soulx | 1:768d359e9d96 | 650 | { |
soulx | 1:768d359e9d96 | 651 | uint16_t ans=0; |
soulx | 1:768d359e9d96 | 652 | |
soulx | 1:768d359e9d96 | 653 | //ans = |
soulx | 1:768d359e9d96 | 654 | |
soulx | 1:768d359e9d96 | 655 | return |
soulx | 1:768d359e9d96 | 656 | } |
soulx | 1:768d359e9d96 | 657 | |
soulx | 1:768d359e9d96 | 658 | uint16_t scale(uint16_t data) |
soulx | 1:768d359e9d96 | 659 | { |
soulx | 5:f07cbb5a86c3 | 660 | |
soulx | 5:f07cbb5a86c3 | 661 | } |
soulx | 6:8d80c84e0c09 | 662 | */ |
soulx | 6:8d80c84e0c09 | 663 | |
soulx | 6:8d80c84e0c09 | 664 | void getCommand() |
soulx | 6:8d80c84e0c09 | 665 | { |
soulx | 6:8d80c84e0c09 | 666 | static uint8_t count =0; |
soulx | 6:8d80c84e0c09 | 667 | |
soulx | 6:8d80c84e0c09 | 668 | ANDANTE_PROTOCOL_PACKET packet; |
soulx | 6:8d80c84e0c09 | 669 | |
soulx | 6:8d80c84e0c09 | 670 | uint8_t status = pan_a.receiveCommunicatePacket(&packet); |
soulx | 6:8d80c84e0c09 | 671 | myled=0; |
soulx | 7:6a99be179329 | 672 | |
soulx | 6:8d80c84e0c09 | 673 | |
soulx | 6:8d80c84e0c09 | 674 | |
soulx | 6:8d80c84e0c09 | 675 | if(status == ANDANTE_ERRBIT_NONE) { |
soulx | 6:8d80c84e0c09 | 676 | if(count >2 && count <10) { |
soulx | 6:8d80c84e0c09 | 677 | count--; |
soulx | 6:8d80c84e0c09 | 678 | } else { |
soulx | 6:8d80c84e0c09 | 679 | count=0; |
soulx | 7:6a99be179329 | 680 | |
soulx | 6:8d80c84e0c09 | 681 | } |
soulx | 6:8d80c84e0c09 | 682 | pan_a.sendCommunicatePacket(&packet); |
soulx | 6:8d80c84e0c09 | 683 | //update command |
soulx | 6:8d80c84e0c09 | 684 | //setControl(&packet); |
soulx | 6:8d80c84e0c09 | 685 | |
soulx | 6:8d80c84e0c09 | 686 | } else if(status == ANDANTE_ERRBIT_READ_TIMEOUT) { |
soulx | 6:8d80c84e0c09 | 687 | count++; |
soulx | 6:8d80c84e0c09 | 688 | } |
soulx | 6:8d80c84e0c09 | 689 | |
soulx | 6:8d80c84e0c09 | 690 | if(count >= TIMEOUT_RESPONE_COMMAND) { |
soulx | 6:8d80c84e0c09 | 691 | //stop robot |
soulx | 6:8d80c84e0c09 | 692 | count++; |
soulx | 6:8d80c84e0c09 | 693 | myled=1; |
soulx | 7:6a99be179329 | 694 | // setSpeedControl(0.0); |
soulx | 6:8d80c84e0c09 | 695 | } |
soulx | 6:8d80c84e0c09 | 696 | } |