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.
main.cpp
00001 // Robot 00002 // Red 00003 00004 #include "mbed.h" 00005 00006 /* 00007 --- XBeeアドレス --- 00008 平面アンテナ 00009 Red 00010 Zigbee Router API : 0013A200 407ABE67 00011 Zigbee Router API : 0013A200 407ABCBB 00012 Blue 00013 Zigbee Coordinator API : 0013A200 407C04BD 00014 Zigbee Router API : 0013A200 407C04F9 00015 */ 00016 00017 Serial xbee(p28, p27); 00018 Serial pc(USBTX, USBRX); 00019 00020 InterruptIn raserSensor1(p9); 00021 InterruptIn raserSensor2(p10); 00022 InterruptIn raserSensor3(p29); 00023 00024 DigitalOut FR1(p11); 00025 DigitalOut FR2(p12); 00026 DigitalOut FL1(p13); 00027 DigitalOut FL2(p14); 00028 DigitalOut BR1(p15); 00029 DigitalOut BR2(p16); 00030 DigitalOut BL1(p17); 00031 DigitalOut BL2(p18); 00032 DigitalOut CR1(p5); 00033 DigitalOut CR2(p6); 00034 DigitalOut CL1(p7); 00035 DigitalOut CL2(p8); 00036 00037 PwmOut FR(p25); 00038 PwmOut FL(p22); 00039 PwmOut BR(p24); 00040 PwmOut BL(p23); 00041 PwmOut CR(p26); 00042 PwmOut CL(p21); 00043 00044 DigitalOut Raser1(p20); 00045 DigitalOut Raser2(p30); 00046 00047 DigitalOut led1(LED1); 00048 DigitalOut led2(LED2); 00049 DigitalOut led3(LED3); 00050 DigitalOut led4(LED4); 00051 00052 00053 /*--- グローバル変数宣言---*/ 00054 uint8_t MyRemoteAddress[] = {0x00,0x13,0xa2,0x00,0x40,0x7a,0xbe,0x67}; // 敵コントローラのアドレス 00055 // uint8_t EnRemoteAddress[] = {0x00,0x13,0xa2,0x00,0x40,0x7c,0x04,0xf9}; // 敵コントローラのアドレス 00056 00057 00058 00059 00060 /*--- モーター制御用関数 ---*/ 00061 void FRmove (float vec) {FR = abs(vec); FR1 = (unsigned char)(abs)(vec+0.9); FR2 = (unsigned char)(abs)(vec-0.9);} 00062 void FLmove (float vec) {FL = abs(vec); FL1 = (unsigned char)(abs)(vec+0.9); FL2 = (unsigned char)(abs)(vec-0.9);} 00063 void CRmove (float vec) {CR = abs(vec); CR1 = (unsigned char)(abs)(vec+0.9); CR2 = (unsigned char)(abs)(vec-0.9);} 00064 void CLmove (float vec) {CL = abs(vec); CL1 = (unsigned char)(abs)(vec-0.9); CL2 = (unsigned char)(abs)(vec+0.9);} 00065 void BRmove (float vec) {BR = abs(vec); BR1 = (unsigned char)(abs)(vec+0.9); BR2 = (unsigned char)(abs)(vec-0.9);} 00066 void BLmove (float vec) {BL = abs(vec); BL1 = (unsigned char)(abs)(vec+0.9); BL2 = (unsigned char)(abs)(vec-0.9);} 00067 00068 00069 /*--- 進行方向制御用変数 ---*/ 00070 void FFmove() { 00071 FLmove(+1.00); FRmove(+1.00); 00072 CLmove(+0.61); CRmove(+0.71); 00073 BLmove(-1.00); BRmove(-1.00); 00074 } 00075 void FRmove() { 00076 FLmove(-0.60); FRmove(+0.60); 00077 CLmove(+1.00); CRmove(+1.00); 00078 BLmove(-1.00); BRmove(+1.00); 00079 } 00080 void FLmove() { 00081 FLmove(+0.60); FRmove(-0.60); 00082 CLmove(+1.00); CRmove(+1.00); 00083 BLmove(+1.00); BRmove(-1.00); 00084 } 00085 void Lmove() { 00086 FLmove(+0.70); FRmove(-0.70); 00087 CLmove(+0.00); CRmove(+0.00); 00088 BLmove(+1.00); BRmove(-1.00); 00089 } 00090 void Rmove() { 00091 FLmove(-0.60); FRmove(+0.60); 00092 CLmove(+0.00); CRmove(+0.00); 00093 BLmove(-1.00); BRmove(+1.00); 00094 } 00095 void BBmove() { 00096 FLmove(-1.00); FRmove(-1.00); 00097 CLmove(-0.61); CRmove(-0.71); 00098 BLmove(+1.00); BRmove(+1.00); 00099 } 00100 void BRmove() { 00101 FLmove(-0.60); FRmove(+0.60); 00102 CLmove(-1.00); CRmove(-1.00); 00103 BLmove(-1.00); BRmove(+1.00); 00104 } 00105 void BLmove() { 00106 FLmove(+0.70); FRmove(-0.70); 00107 CLmove(-1.00); CRmove(-1.00); 00108 BLmove(+1.00); BRmove(-1.00); 00109 } 00110 00111 void Lturn() { 00112 FLmove(+1.00); FRmove(-1.00); 00113 CLmove(-1.00); CRmove(+1.00); 00114 BLmove(+0.50); BRmove(-0.50); 00115 } 00116 void Rturn() { 00117 FLmove(-1.00); FRmove(+1.00); 00118 CLmove(+1.00); CRmove(-1.00); 00119 BLmove(-0.50); BRmove(+0.50); 00120 } 00121 00122 void Stop() { 00123 FR1 = 1; FR2 = 1; 00124 FL1 = 1; FL2 = 1; 00125 CR1 = 1; CR2 = 1; 00126 CL1 = 1; CL2 = 1; 00127 BR1 = 1; BR2 = 1; 00128 BL1 = 1; BL2 = 1; 00129 } 00130 00131 00132 /*--- データフレーム用クラス ---*/ 00133 class xbeeAPI { 00134 public: 00135 uint8_t address[8]; 00136 uint8_t direction; 00137 uint8_t turning; 00138 void reset() { 00139 for (int i=0; i<8; i++) address[i]=0; 00140 direction = 0; 00141 turning = 0; 00142 } 00143 }; 00144 00145 /*--- 送信用関数 ---*/ 00146 void xbeeTx(uint8_t *address, uint8_t data) { 00147 int i=0; 00148 00149 xbee.putc(0x7E); 00150 xbee.putc(0x00); // フレーム長 00151 xbee.putc(0x0F); 00152 xbee.putc(0x10); // フレームタイプ 00153 xbee.putc(0x01); // フレームID 00154 for (int i=0; i<8; i++) 00155 xbee.putc(address[i]); // 62bit宛先アドレス 00156 xbee.putc(0xFF); // 16bit宛先ネットワークアドレス 00157 xbee.putc(0xFE); 00158 xbee.putc(0x00); // ブロードキャスト半径 00159 xbee.putc(0x00); // オプション 00160 xbee.putc(data); // RFデータ 00161 // チェックサムの計算 00162 uint8_t checksum = 0xFF - (0x10+0x01+0xFF+0xFE+data); 00163 for (i=0; i<8; i++) checksum -= address[i]; 00164 xbee.putc(checksum); // チェックサム 00165 00166 // 送信ステータスの確認 00167 if (xbee.readable()) { 00168 if (xbee.getc() == 0x7E) { 00169 for (i=0; i<8; i++) xbee.getc(); 00170 if (xbee.getc() == 0x00) 00171 pc.printf("Send success!\n"); 00172 else 00173 pc.printf("Send missed.\n"); 00174 for (i=9; i<10; i++) xbee.getc(); 00175 } 00176 } 00177 } 00178 00179 00180 /*--- 受信用関数 ---*/ 00181 xbeeAPI xbeeRx() { 00182 xbeeAPI returnApi; // 返り値用の変数 00183 returnApi.reset(); 00184 if (xbee.readable() > 0) { 00185 pc.printf("readable\n"); 00186 if (xbee.getc() == 0x7E) { 00187 pc.printf("startBit\n"); 00188 int i=0; 00189 uint8_t buffer[18]; 00190 00191 buffer[0] = 0x7E; 00192 for (i=1; i<18; i++) { 00193 buffer[i] = xbee.getc(); 00194 } 00195 // 送信元アドレスを抽出 00196 for (i=0; i<8; i++) { 00197 returnApi.address[i] = buffer[i+4]; 00198 } 00199 // 受信データを抽出 00200 returnApi.direction = buffer[15]; 00201 returnApi.turning = buffer[16]; 00202 00203 // データをPCに表示 00204 for (i=0; i<8; i++) pc.printf("%x", returnApi.address[i]); 00205 pc.printf("\n"); 00206 pc.printf("%x\n", returnApi.direction); 00207 pc.printf("%x\n", returnApi.turning); 00208 } 00209 } 00210 return returnApi; 00211 } 00212 00213 00214 /*--- レーザー発射 ---*/ 00215 void Raser() { 00216 Raser1 = 1; 00217 Raser2 = 1; 00218 wait(1); 00219 Raser1 = 0; 00220 Raser2 = 0; 00221 } 00222 00223 00224 /*--- レーザー被弾時割り込み ---*/ 00225 // 注)タクトスイッチに、チャタリング防止のためのコンデンサフィルタを付ける 00226 void damage() { 00227 // 見方コントローラに連絡 00228 led1 = 1; 00229 xbeeTx(MyRemoteAddress, 'D'); 00230 wait(1); 00231 led1 = 0; 00232 } 00233 00234 00235 00236 00237 00238 00239 int main() { 00240 00241 /*--- 変数宣言 ---*/ 00242 int i, j, k; 00243 xbeeAPI rxData; // コントローラからのデータ受信用変数 00244 00245 /*--- 初期設定 ---*/ 00246 // ビットレートの設定 00247 xbee.baud(9600); 00248 pc.baud(9600); 00249 // 割り込みの設定 00250 raserSensor1.rise(&damage); 00251 raserSensor2.rise(&damage); 00252 raserSensor3.rise(&damage); 00253 // 初期値の設定 00254 Raser1=0; Raser2=0; 00255 led1=0; led2=0; led3=0; led4=0; 00256 00257 00258 00259 /*--- 繰り返し処理 ---*/ 00260 while(1) { 00261 // 受信 00262 rxData = xbeeRx(); 00263 00264 // チェック 00265 if (rxData.address[7] != MyRemoteAddress[7]) continue; 00266 // 命令確認 & 実行 00267 switch (rxData.direction) { 00268 case 'w': FFmove(); break; 00269 case 'q': FLmove(); break; 00270 case 'e': FRmove(); break; 00271 00272 case 'a': Lmove(); break; 00273 case 'd': Rmove(); break; 00274 00275 case 'x': BBmove(); break; 00276 case 'z': BLmove(); break; 00277 case 'c': BRmove(); break; 00278 00279 case 'o': Lturn(); break; 00280 case 'p': Rturn(); break; 00281 00282 00283 case 's': Stop(); break; 00284 00285 case 'r': 00286 Raser(); // レーザー発射! 00287 rxData.direction = 0x00; 00288 break; 00289 } 00290 switch (rxData.turning) { 00291 case 0: 00292 00293 } 00294 00295 00296 // wait(0.5); 00297 00298 } 00299 00300 00301 return 0; 00302 } 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316
Generated on Thu Jul 28 2022 11:00:47 by
1.7.2