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