中継機能つけた受け取りオムニ

Dependencies:   mbed MultiSerial

Committer:
bousiya03
Date:
Tue Oct 07 09:51:52 2014 +0000
Revision:
28:4da223ae4392
Parent:
27:68f76ea992bd
Child:
29:7a8efb8a9af8
???????????; ??????????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hatter 0:fddd018033fb 1 /*
Hatter 0:fddd018033fb 2 * 4WD_OMNI_simple by Tomoki Hokida
Hatter 13:8a9d3855ab96 3 * Simple Type
Hatter 0:fddd018033fb 4 *
Hatter 0:fddd018033fb 5 * motor pins piar
Hatter 13:8a9d3855ab96 6 * motor(p13,p14):motor(p15,p16)
Hatter 13:8a9d3855ab96 7 * motor(p17,p18):motor(p19,p20)
Hatter 13:8a9d3855ab96 8 *
Hatter 13:8a9d3855ab96 9 * Serial Pins
Hatter 15:1c1ae4077900 10 * packet.arm[0]:arm
Hatter 13:8a9d3855ab96 11 * packet.leg[1]:omni
Hatter 0:fddd018033fb 12 * reset 0
Hatter 0:fddd018033fb 13 */
Hatter 0:fddd018033fb 14
Hatter 0:fddd018033fb 15 #include "mbed.h"
Hatter 6:26e5a520472d 16 #include "MultiSerial.h"
Hatter 0:fddd018033fb 17
Hatter 14:731c52eb333b 18 #define DATA_NUM 2 //DATA_NUM[byte]通信 この値はmemcpy使ってるんでxbee_packet構造体のメモリ数といい感じに合わせるようにしてください
Hatter 13:8a9d3855ab96 19 #define XBEE_KEY 0xAA //keycode
Hatter 9:f05f881226a3 20 #define ARM_KEY 0xAA
Hatter 3:1f1498403eec 21
bousiya03 26:9a7e9dd2746f 22 #define PWM 0.5
bousiya03 25:a6e41d2efd0c 23 #define PWM_SLOW 0.1
Hatter 15:1c1ae4077900 24
Hatter 12:6e2ee1c83ac1 25
bousiya03 25:a6e41d2efd0c 26 #define R_R 0x4
bousiya03 25:a6e41d2efd0c 27 #define R_L 0x1
bousiya03 25:a6e41d2efd0c 28 #define R_U 0x2
bousiya03 25:a6e41d2efd0c 29 #define R_D 0x8
Hatter 15:1c1ae4077900 30
bousiya03 25:a6e41d2efd0c 31 #define L_R 0x10
bousiya03 25:a6e41d2efd0c 32 #define L_L 0x40
bousiya03 25:a6e41d2efd0c 33 #define L_U 0x20
bousiya03 25:a6e41d2efd0c 34 #define L_D 0x80
Hatter 15:1c1ae4077900 35
Hatter 15:1c1ae4077900 36 #define TIME 0
Hatter 13:8a9d3855ab96 37
Hatter 9:f05f881226a3 38 BusOut check(LED1,LED2,LED3,LED4);
Hatter 13:8a9d3855ab96 39 BusOut motors(p13,p14,p15,p16,p17,p18,p19,p20);
Hatter 12:6e2ee1c83ac1 40
Hatter 15:1c1ae4077900 41 PwmOut pwm[4]= {p21,p22,p23,p24};
Hatter 0:fddd018033fb 42
Hatter 9:f05f881226a3 43 Serial pc(USBTX,USBRX);
Hatter 0:fddd018033fb 44
Hatter 16:4b502070bea8 45 MultiSerial xbee(p28,p27);
Hatter 16:4b502070bea8 46 MultiSerial armMbed(p9,p10);
Hatter 0:fddd018033fb 47
Hatter 13:8a9d3855ab96 48 /* data structure */
Hatter 15:1c1ae4077900 49 typedef struct {
Hatter 6:26e5a520472d 50
Hatter 6:26e5a520472d 51 uint8_t arm[1];
Hatter 6:26e5a520472d 52 uint8_t leg;
Hatter 4:5da566692b79 53
Hatter 15:1c1ae4077900 54 } xbee_packet;
Hatter 6:26e5a520472d 55
Hatter 6:26e5a520472d 56 xbee_packet packet;
Hatter 6:26e5a520472d 57
Hatter 15:1c1ae4077900 58 uint8_t get_data[DATA_NUM]= {0};
bousiya03 27:68f76ea992bd 59 uint8_t check_data[2]={0};
Hatter 13:8a9d3855ab96 60
bousiya03 27:68f76ea992bd 61
bousiya03 27:68f76ea992bd 62 /*start display LED*/
bousiya03 27:68f76ea992bd 63 void display_LED(int kind){
bousiya03 27:68f76ea992bd 64
bousiya03 27:68f76ea992bd 65 switch(kind){
bousiya03 28:4da223ae4392 66
bousiya03 28:4da223ae4392 67 case 0: //err check
bousiya03 27:68f76ea992bd 68 check = 0xA;
bousiya03 27:68f76ea992bd 69 wait(0.5);
bousiya03 27:68f76ea992bd 70 check != 0xF;
bousiya03 27:68f76ea992bd 71 wait(0.5);
bousiya03 27:68f76ea992bd 72
bousiya03 27:68f76ea992bd 73 break;
bousiya03 27:68f76ea992bd 74
bousiya03 27:68f76ea992bd 75 case 1:
bousiya03 28:4da223ae4392 76 //
bousiya03 27:68f76ea992bd 77 check = 0xF;
bousiya03 28:4da223ae4392 78 wait(0.5);
bousiya03 28:4da223ae4392 79 check = 0;
bousiya03 27:68f76ea992bd 80 break;
bousiya03 27:68f76ea992bd 81 }
bousiya03 27:68f76ea992bd 82 }
bousiya03 27:68f76ea992bd 83
bousiya03 27:68f76ea992bd 84
bousiya03 27:68f76ea992bd 85 /*Safety Mode*/
bousiya03 27:68f76ea992bd 86 void safety_mode()
bousiya03 27:68f76ea992bd 87 {
bousiya03 27:68f76ea992bd 88 xbee.stop_read();
bousiya03 27:68f76ea992bd 89 armMbed.stop_write();
bousiya03 27:68f76ea992bd 90
bousiya03 27:68f76ea992bd 91 for(;;) {
bousiya03 27:68f76ea992bd 92
bousiya03 27:68f76ea992bd 93 motors = 0;
bousiya03 27:68f76ea992bd 94 display_LED(0);
bousiya03 27:68f76ea992bd 95
bousiya03 27:68f76ea992bd 96 if(xbee.readable_check()==1) {
bousiya03 27:68f76ea992bd 97
bousiya03 27:68f76ea992bd 98 NVIC_SystemReset();
bousiya03 27:68f76ea992bd 99
bousiya03 27:68f76ea992bd 100 //xbee.start_read();
bousiya03 27:68f76ea992bd 101 //arm_mbed.start_write();
bousiya03 27:68f76ea992bd 102 return;
bousiya03 27:68f76ea992bd 103 }
bousiya03 27:68f76ea992bd 104 }
bousiya03 27:68f76ea992bd 105 }
bousiya03 27:68f76ea992bd 106
Hatter 18:9cbd70819d2d 107
Hatter 0:fddd018033fb 108 int main()
Hatter 15:1c1ae4077900 109 {
Hatter 13:8a9d3855ab96 110
Hatter 13:8a9d3855ab96 111 /* PWM init */
Hatter 12:6e2ee1c83ac1 112 pwm[0] = PWM;
Hatter 12:6e2ee1c83ac1 113 pwm[1] = PWM;
Hatter 12:6e2ee1c83ac1 114 pwm[2] = PWM;
Hatter 12:6e2ee1c83ac1 115 pwm[3] = PWM;
Hatter 12:6e2ee1c83ac1 116
Hatter 10:5b67d18f30a9 117 xbee_packet *pt_packet=&packet;
Hatter 12:6e2ee1c83ac1 118
Hatter 17:3c82070177e5 119 //connect_check();
bousiya03 27:68f76ea992bd 120 int radio_check=0;
Hatter 20:a322730ea5d7 121
Hatter 16:4b502070bea8 122 xbee.start_read();
Hatter 13:8a9d3855ab96 123 xbee.read_data(get_data,XBEE_KEY);
Hatter 22:bfeb1c6e2539 124
Hatter 20:a322730ea5d7 125 armMbed.start_write();
Hatter 20:a322730ea5d7 126 armMbed.write_data(pt_packet->arm,ARM_KEY);
Hatter 20:a322730ea5d7 127
bousiya03 28:4da223ae4392 128 display_LED(1);
bousiya03 27:68f76ea992bd 129
bousiya03 28:4da223ae4392 130 int counter=0;
bousiya03 27:68f76ea992bd 131
Hatter 15:1c1ae4077900 132 for(;;) {
Hatter 15:1c1ae4077900 133
Hatter 23:54fddee73c88 134 wait_ms(0.5);
Hatter 12:6e2ee1c83ac1 135
Hatter 13:8a9d3855ab96 136 //memcpy(&packet, get_data, DATA_NUM);
Hatter 10:5b67d18f30a9 137 packet.arm[0] = get_data[1];
Hatter 15:1c1ae4077900 138 packet.leg = get_data[0];
Hatter 12:6e2ee1c83ac1 139
Hatter 23:54fddee73c88 140 //check = get_data[1];
bousiya03 28:4da223ae4392 141 counter++;
bousiya03 28:4da223ae4392 142 check = counter/100;
bousiya03 28:4da223ae4392 143 if(counter>=15*100){counter=0;}
bousiya03 28:4da223ae4392 144
bousiya03 27:68f76ea992bd 145
bousiya03 27:68f76ea992bd 146 if(xbee.readable_check() == 0) {
bousiya03 27:68f76ea992bd 147
bousiya03 27:68f76ea992bd 148 radio_check++;
bousiya03 27:68f76ea992bd 149
bousiya03 27:68f76ea992bd 150 if(radio_check>=500){
bousiya03 27:68f76ea992bd 151 safety_mode();
bousiya03 27:68f76ea992bd 152 }
bousiya03 27:68f76ea992bd 153
bousiya03 27:68f76ea992bd 154 } else {
bousiya03 27:68f76ea992bd 155
bousiya03 27:68f76ea992bd 156 radio_check = 0;
bousiya03 27:68f76ea992bd 157 }
Hatter 19:1305ce40f4e2 158
Hatter 13:8a9d3855ab96 159 /* Stop */
Hatter 22:bfeb1c6e2539 160 if(packet.leg==0x0) {
bousiya03 25:a6e41d2efd0c 161
bousiya03 25:a6e41d2efd0c 162
bousiya03 25:a6e41d2efd0c 163 motors = 0;
bousiya03 25:a6e41d2efd0c 164
bousiya03 25:a6e41d2efd0c 165 pwm[0] = 0;
bousiya03 25:a6e41d2efd0c 166 pwm[1] = 0;
bousiya03 25:a6e41d2efd0c 167 pwm[2] = 0;
bousiya03 25:a6e41d2efd0c 168 pwm[3] = 0;
Hatter 22:bfeb1c6e2539 169
Hatter 15:1c1ae4077900 170
Hatter 23:54fddee73c88 171 }else{
bousiya03 25:a6e41d2efd0c 172
bousiya03 27:68f76ea992bd 173 pwm[0] = PWM;
bousiya03 27:68f76ea992bd 174 pwm[1] = PWM;
bousiya03 27:68f76ea992bd 175 pwm[2] = PWM;
bousiya03 27:68f76ea992bd 176 pwm[3] = PWM;
Hatter 22:bfeb1c6e2539 177 }
Hatter 15:1c1ae4077900 178
Hatter 13:8a9d3855ab96 179 /* Direction of movement */
bousiya03 25:a6e41d2efd0c 180 if(packet.leg&R_D) motors = 0x55; //01010101 R 接続に気をつけること
bousiya03 25:a6e41d2efd0c 181 if(packet.leg&R_U) motors = 0xAA; //10101010 L 多分モータードライバの信号線をそれぞれ逆にすればOKかな
bousiya03 25:a6e41d2efd0c 182 if(packet.leg&R_R) motors = 0x5A; //01011010 U
bousiya03 25:a6e41d2efd0c 183 if(packet.leg&R_L) motors = 0xA5; //10100101 D
Hatter 13:8a9d3855ab96 184
Hatter 13:8a9d3855ab96 185 /* Turn circling */
bousiya03 25:a6e41d2efd0c 186 if(packet.leg&L_L) motors = 0x66; //01100110
bousiya03 25:a6e41d2efd0c 187 if(packet.leg&L_R) motors = 0x99; //10011001
bousiya03 25:a6e41d2efd0c 188 //if(packet.leg&L_U)
bousiya03 25:a6e41d2efd0c 189 //if(packet.leg&L_D)
Hatter 6:26e5a520472d 190 }
Hatter 0:fddd018033fb 191 }