12/16用テスト

Dependencies:   ATP3012 mbed a HMC US015_2 getGPS

Committer:
miyajitakenari
Date:
Thu Dec 09 11:18:58 2021 +0000
Revision:
12:740d999b1fea
Parent:
11:d00bfdcb7481
Child:
14:3e7d563538e5
Child:
16:7ff8e5bbae88
a

Who changed what in which revision?

UserRevisionLine numberNew 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 xbee(A7, A2);
miyajitakenari 0:79033ee3c961 16
miyajitakenari 0:79033ee3c961 17 double GPS_x, GPS_y; // 現在地の座標
miyajitakenari 0:79033ee3c961 18 double next_CP_x, next_CP_y;
miyajitakenari 0:79033ee3c961 19 double CPs_x[100]; // = []; //CPリスト(x座標)
miyajitakenari 0:79033ee3c961 20 double CPs_y[100]; // = []; // CPリスト(y座標)
miyajitakenari 0:79033ee3c961 21 double theta;
miyajitakenari 0:79033ee3c961 22 double delta;
miyajitakenari 0:79033ee3c961 23
miyajitakenari 0:79033ee3c961 24 int FrontGet()
miyajitakenari 0:79033ee3c961 25 {
miyajitakenari 0:79033ee3c961 26 Ultra = 1; //超音波on
miyajitakenari 0:79033ee3c961 27 hs.TrigerOut();
miyajitakenari 0:79033ee3c961 28 wait(1);
miyajitakenari 0:79033ee3c961 29 int distance;
miyajitakenari 6:326208aabe68 30 distance = hs.GetDistance();
miyajitakenari 6:326208aabe68 31 xbee.printf("distance=%d\r\n", distance); //距離出力
miyajitakenari 0:79033ee3c961 32 Ultra=0;//超音波off
miyajitakenari 0:79033ee3c961 33
miyajitakenari 11:d00bfdcb7481 34 if(distance < 1500) {
miyajitakenari 0:79033ee3c961 35 return 1;
miyajitakenari 0:79033ee3c961 36 }
miyajitakenari 0:79033ee3c961 37 else {
miyajitakenari 0:79033ee3c961 38 return 0;
miyajitakenari 0:79033ee3c961 39 }
miyajitakenari 0:79033ee3c961 40 }
miyajitakenari 0:79033ee3c961 41
miyajitakenari 0:79033ee3c961 42
miyajitakenari 0:79033ee3c961 43 void catchGPS()
miyajitakenari 0:79033ee3c961 44 {
miyajitakenari 0:79033ee3c961 45 xbee.printf("GPS Start\r\n");
miyajitakenari 0:79033ee3c961 46 /* 1秒ごとに現在地を取得してターミナル出力 */
miyajitakenari 0:79033ee3c961 47 while(1) {
miyajitakenari 0:79033ee3c961 48 if(gps.getgps()) { //現在地取得
miyajitakenari 6:326208aabe68 49 xbee.printf("%lf %lf\r\n", gps.latitude, gps.longitude);//緯度と経度を出力
miyajitakenari 4:975b0d9bd51b 50 GPS_x = gps.latitude;
miyajitakenari 4:975b0d9bd51b 51 GPS_y = gps.longitude;
miyajitakenari 0:79033ee3c961 52 break;
miyajitakenari 0:79033ee3c961 53 }
miyajitakenari 0:79033ee3c961 54 else {
miyajitakenari 6:326208aabe68 55 xbee.printf("No data\r\n");//データ取得に失敗した場合
miyajitakenari 0:79033ee3c961 56 wait(1);
miyajitakenari 0:79033ee3c961 57 }
miyajitakenari 0:79033ee3c961 58 }
miyajitakenari 0:79033ee3c961 59 return;
miyajitakenari 0:79033ee3c961 60 }
miyajitakenari 0:79033ee3c961 61
miyajitakenari 0:79033ee3c961 62
miyajitakenari 0:79033ee3c961 63 void Move(char input_data, float motor_speed) {
miyajitakenari 0:79033ee3c961 64 switch (input_data) {
miyajitakenari 0:79033ee3c961 65 case '1': // 停止
miyajitakenari 0:79033ee3c961 66 motor_a = 0;
miyajitakenari 0:79033ee3c961 67 motor_b = 0;
miyajitakenari 0:79033ee3c961 68 break;
miyajitakenari 0:79033ee3c961 69 case '2': // 前進
miyajitakenari 0:79033ee3c961 70 motor_a = motor_speed;
miyajitakenari 0:79033ee3c961 71 motor_b = motor_speed;
miyajitakenari 0:79033ee3c961 72 break;
miyajitakenari 0:79033ee3c961 73 case '3': // 後退
miyajitakenari 0:79033ee3c961 74 motor_a = -motor_speed;
miyajitakenari 0:79033ee3c961 75 motor_b = -motor_speed;
miyajitakenari 0:79033ee3c961 76 break;
miyajitakenari 0:79033ee3c961 77 case '4': // 時計回りに回転
miyajitakenari 0:79033ee3c961 78 motor_a = motor_speed;
miyajitakenari 0:79033ee3c961 79 motor_b = -motor_speed;
miyajitakenari 0:79033ee3c961 80 break;
miyajitakenari 0:79033ee3c961 81 case '5': // 反時計回りに回転
miyajitakenari 0:79033ee3c961 82 motor_a = -motor_speed;
miyajitakenari 0:79033ee3c961 83 motor_b = motor_speed;
miyajitakenari 0:79033ee3c961 84 break;
miyajitakenari 0:79033ee3c961 85 case '6': // Aのみ正転
miyajitakenari 0:79033ee3c961 86 motor_a = motor_speed;
miyajitakenari 0:79033ee3c961 87 break;
miyajitakenari 0:79033ee3c961 88 case '7': // Bのみ正転
miyajitakenari 0:79033ee3c961 89 motor_b = motor_speed;
miyajitakenari 0:79033ee3c961 90 break;
miyajitakenari 0:79033ee3c961 91 case '8': // Aのみ逆転
miyajitakenari 0:79033ee3c961 92 motor_a = -motor_speed;
miyajitakenari 0:79033ee3c961 93 break;
miyajitakenari 0:79033ee3c961 94 case '9': // Bのみ逆転
miyajitakenari 0:79033ee3c961 95 motor_b = -motor_speed;
miyajitakenari 0:79033ee3c961 96 break;
miyajitakenari 0:79033ee3c961 97 }
miyajitakenari 0:79033ee3c961 98 }
miyajitakenari 0:79033ee3c961 99
miyajitakenari 0:79033ee3c961 100 double AngleGet()
miyajitakenari 0:79033ee3c961 101 {
miyajitakenari 0:79033ee3c961 102 double angle = 0;
miyajitakenari 0:79033ee3c961 103 compass.setOpMode(HMC6352_CONTINUOUS, 1, 20);
miyajitakenari 0:79033ee3c961 104 angle = compass.sample() / 10;
miyajitakenari 0:79033ee3c961 105
miyajitakenari 0:79033ee3c961 106 double theta;
miyajitakenari 0:79033ee3c961 107 double delta;
miyajitakenari 0:79033ee3c961 108
miyajitakenari 0:79033ee3c961 109 xbee.printf("gps.latitude=%f, gps.longitude=%f\r\n", gps.latitude, gps.longitude);
miyajitakenari 3:ec2b7587be78 110 theta = atan2(next_CP_y - gps.longitude , next_CP_x - gps.latitude) * 180 / 3.14159265359;
miyajitakenari 0:79033ee3c961 111 printf("theta=%f\r\n", theta);
miyajitakenari 0:79033ee3c961 112 if(theta >= 0) {
miyajitakenari 0:79033ee3c961 113 delta = angle - theta;
miyajitakenari 0:79033ee3c961 114 }
miyajitakenari 0:79033ee3c961 115 else {
miyajitakenari 0:79033ee3c961 116 theta = theta + 360;
miyajitakenari 0:79033ee3c961 117 delta = angle - theta;
miyajitakenari 12:740d999b1fea 118 }
miyajitakenari 12:740d999b1fea 119 if(delta<0){
miyajitakenari 12:740d999b1fea 120 delta+=360;
miyajitakenari 12:740d999b1fea 121 }
miyajitakenari 0:79033ee3c961 122 printf("%f-%f=%f\r\n", angle, theta, delta);
miyajitakenari 0:79033ee3c961 123 wait(2);
miyajitakenari 0:79033ee3c961 124 return delta;
miyajitakenari 0:79033ee3c961 125 }
miyajitakenari 0:79033ee3c961 126
miyajitakenari 0:79033ee3c961 127 void Calibration()
miyajitakenari 0:79033ee3c961 128 {
miyajitakenari 0:79033ee3c961 129 xbee.printf("calibration start\r\n");
miyajitakenari 0:79033ee3c961 130 compass.setCalibrationMode(0x43);
miyajitakenari 10:e25e06011fd2 131 Move('4', 0.15);
miyajitakenari 11:d00bfdcb7481 132 xbee.printf("mortor mode:4 speed:0.15\n\r");
miyajitakenari 3:ec2b7587be78 133 wait(60);
miyajitakenari 3:ec2b7587be78 134 Move('1', 0);
miyajitakenari 5:cc7917e8c442 135 xbee.printf("mortor mode:1 speed:0\n\r");
miyajitakenari 0:79033ee3c961 136 compass.setCalibrationMode(0x45);
miyajitakenari 0:79033ee3c961 137 xbee.printf("calibration end\r\n");
miyajitakenari 0:79033ee3c961 138 while(1) {
miyajitakenari 0:79033ee3c961 139 if(gps.getgps()) { //現在地取得
miyajitakenari 6:326208aabe68 140 xbee.printf("lati=%lf\nlong=%lf\r\n", gps.latitude, gps.longitude);//緯度と経度を出力
miyajitakenari 4:975b0d9bd51b 141 GPS_x = gps.latitude;
miyajitakenari 4:975b0d9bd51b 142 GPS_y = gps.longitude;
miyajitakenari 0:79033ee3c961 143 break;
miyajitakenari 0:79033ee3c961 144 }
miyajitakenari 0:79033ee3c961 145 else {
miyajitakenari 6:326208aabe68 146 xbee.printf("No data\r\n");//データ取得に失敗した場合
miyajitakenari 0:79033ee3c961 147 wait(1);
miyajitakenari 0:79033ee3c961 148 }
miyajitakenari 0:79033ee3c961 149 }
miyajitakenari 0:79033ee3c961 150
miyajitakenari 0:79033ee3c961 151 return;
miyajitakenari 0:79033ee3c961 152 }
miyajitakenari 0:79033ee3c961 153
miyajitakenari 0:79033ee3c961 154 /*地上局から新情報を送るときはflag=がでてきたらスペースか.を入力
miyajitakenari 0:79033ee3c961 155 3秒後ぐらいにmessage=が出てくるので、そしたら新情報を入力*/
miyajitakenari 0:79033ee3c961 156 void speak()
miyajitakenari 0:79033ee3c961 157 {
miyajitakenari 0:79033ee3c961 158 int timeout_ms=500;
miyajitakenari 0:79033ee3c961 159 char mess[100];
miyajitakenari 0:79033ee3c961 160 if(talk.IsActive(timeout_ms)==true){
miyajitakenari 0:79033ee3c961 161 xbee.printf("Active\n\rflag=");
miyajitakenari 0:79033ee3c961 162 wait(3);
miyajitakenari 0:79033ee3c961 163 if(xbee.readable()){
miyajitakenari 0:79033ee3c961 164 xbee.printf("\n\rmessage=");
miyajitakenari 0:79033ee3c961 165 int i=0;
miyajitakenari 0:79033ee3c961 166 do{
miyajitakenari 0:79033ee3c961 167 mess[i++]= xbee.getc();
miyajitakenari 0:79033ee3c961 168 }
miyajitakenari 0:79033ee3c961 169 while(mess[i-1]!= 0x0d && i<99);
miyajitakenari 0:79033ee3c961 170 talk.Synthe(mess);
miyajitakenari 0:79033ee3c961 171 }
miyajitakenari 0:79033ee3c961 172 else{
miyajitakenari 0:79033ee3c961 173 xbee.printf("preset_message speak\r\n");
miyajitakenari 0:79033ee3c961 174 talk.Synthe("purissetommese-ji,,konnichiwa.");
miyajitakenari 0:79033ee3c961 175 }
miyajitakenari 0:79033ee3c961 176 }
miyajitakenari 0:79033ee3c961 177 else{
miyajitakenari 0:79033ee3c961 178 xbee.printf("\r\nNot Active\n");
miyajitakenari 0:79033ee3c961 179 }
miyajitakenari 0:79033ee3c961 180 }