角度調整用のプログラムです。 (モータードライバー+磁気センサー)

Dependencies:   ATP3012 mbed a HMC US015_2 getGPS

Committer:
ushiroji
Date:
Wed Dec 01 08:14:01 2021 +0000
Revision:
0:99f4fe3e21c6
Child:
1:f70beec382b6
First version

Who changed what in which revision?

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