ラジコン用プログラムです。
Dependencies: mbed ATP3012 a HMC US015_2 getGPS
Function.h@1:6ff5fbdf32bb, 2021-12-15 (annotated)
- Committer:
- ushiroji
- Date:
- Wed Dec 15 06:54:35 2021 +0000
- Revision:
- 1:6ff5fbdf32bb
- Parent:
- 0:149a860cbcb9
first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ushiroji | 0:149a860cbcb9 | 1 | #include "mbed.h" |
ushiroji | 0:149a860cbcb9 | 2 | #include "getGPS.h" |
ushiroji | 0:149a860cbcb9 | 3 | #include "us015.h" |
ushiroji | 0:149a860cbcb9 | 4 | #include "HMC6352.h" |
ushiroji | 0:149a860cbcb9 | 5 | #include "TB6612.h" |
ushiroji | 0:149a860cbcb9 | 6 | #include "ATP3011.h" |
ushiroji | 0:149a860cbcb9 | 7 | |
ushiroji | 0:149a860cbcb9 | 8 | US015 hs(D2, D3); |
ushiroji | 0:149a860cbcb9 | 9 | DigitalOut Ultra(D2); |
ushiroji | 0:149a860cbcb9 | 10 | GPS gps(D1, D0); |
ushiroji | 0:149a860cbcb9 | 11 | HMC6352 compass(D4, D5); |
ushiroji | 0:149a860cbcb9 | 12 | ATP3011 talk(D4,D5); // I2C sda scl |
ushiroji | 0:149a860cbcb9 | 13 | TB6612 motor_a(D10,D6,D7); //モータA制御用(pwma,ain1,ain2) |
ushiroji | 0:149a860cbcb9 | 14 | TB6612 motor_b(D11,D8,D9); //モータB制御用(pwmb,bin1,bin2) |
ushiroji | 0:149a860cbcb9 | 15 | Serial xbee(A7, A2); |
ushiroji | 0:149a860cbcb9 | 16 | |
ushiroji | 0:149a860cbcb9 | 17 | double GPS_x, GPS_y; // 現在地の座標 |
ushiroji | 0:149a860cbcb9 | 18 | double next_CP_x, next_CP_y; |
ushiroji | 0:149a860cbcb9 | 19 | double CPs_x[100]; // = []; //CPリスト(x座標) |
ushiroji | 0:149a860cbcb9 | 20 | double CPs_y[100]; // = []; // CPリスト(y座標) |
ushiroji | 0:149a860cbcb9 | 21 | double theta; |
ushiroji | 0:149a860cbcb9 | 22 | double delta; |
ushiroji | 0:149a860cbcb9 | 23 | |
ushiroji | 0:149a860cbcb9 | 24 | int FrontGet() |
ushiroji | 0:149a860cbcb9 | 25 | { |
ushiroji | 0:149a860cbcb9 | 26 | Ultra = 1; //超音波on |
ushiroji | 0:149a860cbcb9 | 27 | hs.TrigerOut(); |
ushiroji | 0:149a860cbcb9 | 28 | int distance; |
ushiroji | 0:149a860cbcb9 | 29 | distance = hs.GetDistance(); |
ushiroji | 0:149a860cbcb9 | 30 | xbee.printf("distance=%d\r\n", distance); //距離出力 |
ushiroji | 0:149a860cbcb9 | 31 | Ultra=0;//超音波off |
ushiroji | 0:149a860cbcb9 | 32 | |
ushiroji | 0:149a860cbcb9 | 33 | if(distance < 1500) { |
ushiroji | 0:149a860cbcb9 | 34 | return 1; |
ushiroji | 0:149a860cbcb9 | 35 | } |
ushiroji | 0:149a860cbcb9 | 36 | else { |
ushiroji | 0:149a860cbcb9 | 37 | return 0; |
ushiroji | 0:149a860cbcb9 | 38 | } |
ushiroji | 0:149a860cbcb9 | 39 | } |
ushiroji | 0:149a860cbcb9 | 40 | |
ushiroji | 0:149a860cbcb9 | 41 | |
ushiroji | 0:149a860cbcb9 | 42 | void catchGPS() |
ushiroji | 0:149a860cbcb9 | 43 | { |
ushiroji | 0:149a860cbcb9 | 44 | xbee.printf("GPS Start\r\n"); |
ushiroji | 0:149a860cbcb9 | 45 | /* 1秒ごとに現在地を取得してターミナル出力 */ |
ushiroji | 0:149a860cbcb9 | 46 | while(1) { |
ushiroji | 0:149a860cbcb9 | 47 | if(gps.getgps()) { //現在地取得 |
ushiroji | 0:149a860cbcb9 | 48 | xbee.printf("%lf %lf\r\n", gps.latitude, gps.longitude);//緯度と経度を出力 |
ushiroji | 0:149a860cbcb9 | 49 | GPS_x = gps.latitude; |
ushiroji | 0:149a860cbcb9 | 50 | GPS_y = gps.longitude; |
ushiroji | 0:149a860cbcb9 | 51 | break; |
ushiroji | 0:149a860cbcb9 | 52 | } |
ushiroji | 0:149a860cbcb9 | 53 | else { |
ushiroji | 0:149a860cbcb9 | 54 | xbee.printf("No data\r\n");//データ取得に失敗した場合 |
ushiroji | 0:149a860cbcb9 | 55 | wait(1); |
ushiroji | 0:149a860cbcb9 | 56 | } |
ushiroji | 0:149a860cbcb9 | 57 | } |
ushiroji | 0:149a860cbcb9 | 58 | return; |
ushiroji | 0:149a860cbcb9 | 59 | } |
ushiroji | 0:149a860cbcb9 | 60 | |
ushiroji | 0:149a860cbcb9 | 61 | |
ushiroji | 0:149a860cbcb9 | 62 | void Move(char input_data, float motor_speed) { |
ushiroji | 0:149a860cbcb9 | 63 | switch (input_data) { |
ushiroji | 0:149a860cbcb9 | 64 | case '1': // 停止 |
ushiroji | 0:149a860cbcb9 | 65 | motor_a = 0; |
ushiroji | 0:149a860cbcb9 | 66 | motor_b = 0; |
ushiroji | 0:149a860cbcb9 | 67 | break; |
ushiroji | 0:149a860cbcb9 | 68 | case '2': // 前進 |
ushiroji | 0:149a860cbcb9 | 69 | motor_a = motor_speed; |
ushiroji | 0:149a860cbcb9 | 70 | motor_b = motor_speed; |
ushiroji | 0:149a860cbcb9 | 71 | break; |
ushiroji | 0:149a860cbcb9 | 72 | case '3': // 後退 |
ushiroji | 0:149a860cbcb9 | 73 | motor_a = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 74 | motor_b = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 75 | break; |
ushiroji | 0:149a860cbcb9 | 76 | case '4': // 時計回りに回転 |
ushiroji | 0:149a860cbcb9 | 77 | motor_a = motor_speed; |
ushiroji | 0:149a860cbcb9 | 78 | motor_b = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 79 | break; |
ushiroji | 0:149a860cbcb9 | 80 | case '5': // 反時計回りに回転 |
ushiroji | 0:149a860cbcb9 | 81 | motor_a = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 82 | motor_b = motor_speed; |
ushiroji | 0:149a860cbcb9 | 83 | break; |
ushiroji | 0:149a860cbcb9 | 84 | case '6': // Aのみ正転 |
ushiroji | 0:149a860cbcb9 | 85 | motor_a = motor_speed; |
ushiroji | 0:149a860cbcb9 | 86 | break; |
ushiroji | 0:149a860cbcb9 | 87 | case '7': // Bのみ正転 |
ushiroji | 0:149a860cbcb9 | 88 | motor_b = motor_speed; |
ushiroji | 0:149a860cbcb9 | 89 | break; |
ushiroji | 0:149a860cbcb9 | 90 | case '8': // Aのみ逆転 |
ushiroji | 0:149a860cbcb9 | 91 | motor_a = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 92 | break; |
ushiroji | 0:149a860cbcb9 | 93 | case '9': // Bのみ逆転 |
ushiroji | 0:149a860cbcb9 | 94 | motor_b = -motor_speed; |
ushiroji | 0:149a860cbcb9 | 95 | break; |
ushiroji | 0:149a860cbcb9 | 96 | } |
ushiroji | 0:149a860cbcb9 | 97 | } |
ushiroji | 0:149a860cbcb9 | 98 | |
ushiroji | 0:149a860cbcb9 | 99 | double AngleGet() |
ushiroji | 0:149a860cbcb9 | 100 | { |
ushiroji | 0:149a860cbcb9 | 101 | double angle = 0; |
ushiroji | 0:149a860cbcb9 | 102 | compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); |
ushiroji | 0:149a860cbcb9 | 103 | angle = compass.sample() / 10; |
ushiroji | 0:149a860cbcb9 | 104 | |
ushiroji | 0:149a860cbcb9 | 105 | double theta; |
ushiroji | 0:149a860cbcb9 | 106 | double delta; |
ushiroji | 0:149a860cbcb9 | 107 | |
ushiroji | 0:149a860cbcb9 | 108 | xbee.printf("gps.latitude=%f, gps.longitude=%f\r\n", gps.latitude, gps.longitude); |
ushiroji | 0:149a860cbcb9 | 109 | theta = atan2(next_CP_y - gps.longitude , next_CP_x - gps.latitude) * 180 / 3.14159265359; |
ushiroji | 0:149a860cbcb9 | 110 | printf("theta=%f\r\n", theta); |
ushiroji | 0:149a860cbcb9 | 111 | if(theta >= 0) { |
ushiroji | 0:149a860cbcb9 | 112 | delta = angle - theta; |
ushiroji | 0:149a860cbcb9 | 113 | } |
ushiroji | 0:149a860cbcb9 | 114 | else { |
ushiroji | 0:149a860cbcb9 | 115 | theta = theta + 360; |
ushiroji | 0:149a860cbcb9 | 116 | delta = angle - theta; |
ushiroji | 0:149a860cbcb9 | 117 | } |
ushiroji | 0:149a860cbcb9 | 118 | if(delta<0){ |
ushiroji | 0:149a860cbcb9 | 119 | delta+=360; |
ushiroji | 0:149a860cbcb9 | 120 | } |
ushiroji | 0:149a860cbcb9 | 121 | printf("%f-%f=%f\r\n", angle, theta, delta); |
ushiroji | 0:149a860cbcb9 | 122 | wait(2); |
ushiroji | 0:149a860cbcb9 | 123 | return delta; |
ushiroji | 0:149a860cbcb9 | 124 | } |
ushiroji | 0:149a860cbcb9 | 125 | |
ushiroji | 0:149a860cbcb9 | 126 | void Calibration() |
ushiroji | 0:149a860cbcb9 | 127 | { |
ushiroji | 0:149a860cbcb9 | 128 | xbee.printf("calibration start\r\n"); |
ushiroji | 0:149a860cbcb9 | 129 | compass.setCalibrationMode(0x43); |
ushiroji | 0:149a860cbcb9 | 130 | Move('4', 0.15); |
ushiroji | 0:149a860cbcb9 | 131 | xbee.printf("mortor mode:4 speed:0.15\n\r"); |
ushiroji | 0:149a860cbcb9 | 132 | wait(60); |
ushiroji | 0:149a860cbcb9 | 133 | Move('1', 0); |
ushiroji | 0:149a860cbcb9 | 134 | xbee.printf("mortor mode:1 speed:0\n\r"); |
ushiroji | 0:149a860cbcb9 | 135 | compass.setCalibrationMode(0x45); |
ushiroji | 0:149a860cbcb9 | 136 | xbee.printf("calibration end\r\n"); |
ushiroji | 0:149a860cbcb9 | 137 | while(1) { |
ushiroji | 0:149a860cbcb9 | 138 | if(gps.getgps()) { //現在地取得 |
ushiroji | 0:149a860cbcb9 | 139 | xbee.printf("lati=%lf\nlong=%lf\r\n", gps.latitude, gps.longitude);//緯度と経度を出力 |
ushiroji | 0:149a860cbcb9 | 140 | GPS_x = gps.latitude; |
ushiroji | 0:149a860cbcb9 | 141 | GPS_y = gps.longitude; |
ushiroji | 0:149a860cbcb9 | 142 | break; |
ushiroji | 0:149a860cbcb9 | 143 | } |
ushiroji | 0:149a860cbcb9 | 144 | else { |
ushiroji | 0:149a860cbcb9 | 145 | xbee.printf("No data\r\n");//データ取得に失敗した場合 |
ushiroji | 0:149a860cbcb9 | 146 | wait(1); |
ushiroji | 0:149a860cbcb9 | 147 | } |
ushiroji | 0:149a860cbcb9 | 148 | } |
ushiroji | 0:149a860cbcb9 | 149 | |
ushiroji | 0:149a860cbcb9 | 150 | return; |
ushiroji | 0:149a860cbcb9 | 151 | } |
ushiroji | 0:149a860cbcb9 | 152 | |
ushiroji | 0:149a860cbcb9 | 153 | /*地上局から新情報を送るときはflag=がでてきたらスペースか.を入力 |
ushiroji | 0:149a860cbcb9 | 154 | 3秒後ぐらいにmessage=が出てくるので、そしたら新情報を入力*/ |
ushiroji | 0:149a860cbcb9 | 155 | void speak() |
ushiroji | 0:149a860cbcb9 | 156 | { |
ushiroji | 0:149a860cbcb9 | 157 | int timeout_ms=500; |
ushiroji | 0:149a860cbcb9 | 158 | char mess[100]; |
ushiroji | 0:149a860cbcb9 | 159 | if(talk.IsActive(timeout_ms)==true){ |
ushiroji | 0:149a860cbcb9 | 160 | xbee.printf("Active\n\rflag="); |
ushiroji | 0:149a860cbcb9 | 161 | wait(3); |
ushiroji | 0:149a860cbcb9 | 162 | if(xbee.readable()){ |
ushiroji | 0:149a860cbcb9 | 163 | xbee.printf("\n\rmessage="); |
ushiroji | 0:149a860cbcb9 | 164 | int i=0; |
ushiroji | 0:149a860cbcb9 | 165 | do{ |
ushiroji | 0:149a860cbcb9 | 166 | mess[i++]= xbee.getc(); |
ushiroji | 0:149a860cbcb9 | 167 | } |
ushiroji | 0:149a860cbcb9 | 168 | while(mess[i-1]!= 0x0d && i<99); |
ushiroji | 0:149a860cbcb9 | 169 | talk.Synthe(mess); |
ushiroji | 0:149a860cbcb9 | 170 | } |
ushiroji | 0:149a860cbcb9 | 171 | else{ |
ushiroji | 0:149a860cbcb9 | 172 | xbee.printf("preset_message speak\r\n"); |
ushiroji | 0:149a860cbcb9 | 173 | talk.Synthe("purissetommese-ji,,konnichiwa."); |
ushiroji | 0:149a860cbcb9 | 174 | } |
ushiroji | 0:149a860cbcb9 | 175 | } |
ushiroji | 0:149a860cbcb9 | 176 | else{ |
ushiroji | 0:149a860cbcb9 | 177 | xbee.printf("\r\nNot Active\n"); |
ushiroji | 0:149a860cbcb9 | 178 | } |
ushiroji | 0:149a860cbcb9 | 179 | } |