ROBOSTEP_5期 / Mbed 2 deprecated George_Master_BOTHMOVE

Dependencies:   mbed robot

Committer:
yuto17320508
Date:
Thu May 02 07:20:17 2019 +0000
Revision:
13:29e71a2fd11e
Parent:
12:9a5de6adae9c
Child:
14:1a3a673d85e3
l

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eri 0:c1476d342c13 1 #include "mbed.h"
eri 0:c1476d342c13 2 #include "pin.h"
eri 0:c1476d342c13 3 #include "microinfinity.h"
eri 0:c1476d342c13 4
yuto17320508 4:81f01f93e502 5 //#define DEBUG_ON
yuto17320508 4:81f01f93e502 6
yuto17320508 4:81f01f93e502 7 #ifdef DEBUG_ON
yuto17320508 4:81f01f93e502 8 #define DEBUG(...) printf("" __VA_ARGS__);
yuto17320508 4:81f01f93e502 9 #else
yuto17320508 4:81f01f93e502 10 #define DEBUG(...)
yuto17320508 4:81f01f93e502 11 #endif
yuto17320508 4:81f01f93e502 12
yuto17320508 4:81f01f93e502 13 #define Pi 3.14159265359 //円周率π
yuto17320508 4:81f01f93e502 14
yuto17320508 12:9a5de6adae9c 15 enum WalkMode
yuto17320508 12:9a5de6adae9c 16 {
yuto17320508 12:9a5de6adae9c 17 BACK,
yuto17320508 12:9a5de6adae9c 18 STOP,
yuto17320508 12:9a5de6adae9c 19 FORWARD,
yuto17320508 12:9a5de6adae9c 20 };
yuto17320508 12:9a5de6adae9c 21 enum EVENT
yuto17320508 12:9a5de6adae9c 22 {
yuto17320508 12:9a5de6adae9c 23 NORMAL,
yuto17320508 12:9a5de6adae9c 24 GEREGE,
yuto17320508 12:9a5de6adae9c 25 GOAL,
yuto17320508 12:9a5de6adae9c 26 };
yuto17320508 4:81f01f93e502 27 float accel_max = 0.01; //これグローバルにしたのはごめん。set関数多すぎてめんどくなった。
yuto17320508 4:81f01f93e502 28
yuto17320508 4:81f01f93e502 29 class PIDcontroller //distanceをvalueに置き換えました
yuto17320508 4:81f01f93e502 30 {
yuto17320508 4:81f01f93e502 31 float Kp_, Ki_, Kd_, tolerance_, time_delta_;
yuto17320508 4:81f01f93e502 32 float pile_, value_old_, target_;
yuto17320508 4:81f01f93e502 33
yuto17320508 5:63462d696256 34 public:
yuto17320508 4:81f01f93e502 35 bool IsConvergence_; //収束したかどうか
yuto17320508 4:81f01f93e502 36 PIDcontroller(float Kp, float Ki, float Kd); //初期設定で係数を入力
yuto17320508 5:63462d696256 37 void setCoefficients(float Kp, float Ki, float Kd)
yuto17320508 5:63462d696256 38 {
yuto17320508 5:63462d696256 39 Kp_ = Kp, Ki_ = Ki, Kd_ = Kd;
yuto17320508 5:63462d696256 40 }; //係数を変更するときに使う
yuto17320508 5:63462d696256 41 void setTimeDelta(float delta)
yuto17320508 5:63462d696256 42 {
yuto17320508 5:63462d696256 43 time_delta_ = delta;
yuto17320508 5:63462d696256 44 };
yuto17320508 4:81f01f93e502 45 void setTarget(float target); //目標位置の設定
yuto17320508 5:63462d696256 46 void setTolerance(float tolerance)
yuto17320508 5:63462d696256 47 {
yuto17320508 5:63462d696256 48 tolerance_ = tolerance;
yuto17320508 5:63462d696256 49 }; //許容誤差の設定
yuto17320508 4:81f01f93e502 50 float calc(float nowVal); //現在位置と目標を比較してPID補正
yuto17320508 5:63462d696256 51 bool knowConvergence()
yuto17320508 5:63462d696256 52 {
yuto17320508 5:63462d696256 53 return IsConvergence_;
yuto17320508 5:63462d696256 54 }; //収束したかどうかを外部に伝える
yuto17320508 4:81f01f93e502 55 };
yuto17320508 4:81f01f93e502 56
yuto17320508 4:81f01f93e502 57 class Motor //PIDコントローラ、エンコーダを含むモータのクラス
yuto17320508 4:81f01f93e502 58 {
yuto17320508 4:81f01f93e502 59 PwmOut *pin_forward_, *pin_back_;
yuto17320508 4:81f01f93e502 60 Ec *ec_; //対応するエンコーダ
yuto17320508 4:81f01f93e502 61 float duty_, pre_duty_, duty_limit_; //dutyと現在のモータ位置
yuto17320508 4:81f01f93e502 62 int resolution_;
yuto17320508 5:63462d696256 63 public:
yuto17320508 4:81f01f93e502 64 Motor(PwmOut *forward, PwmOut *back); //ピンをポインタ渡し
yuto17320508 5:63462d696256 65 void setDutyLimit(float limit)
yuto17320508 5:63462d696256 66 {
yuto17320508 5:63462d696256 67 duty_limit_ = limit;
yuto17320508 5:63462d696256 68 };
yuto17320508 12:9a5de6adae9c 69 float getDutyLimit(){return duty_limit_;};
yuto17320508 4:81f01f93e502 70 float getPosi(); //ポジをエンコーダから取得
yuto17320508 4:81f01f93e502 71 void calcDuty(PIDcontroller *pid); //Duty比を計算
yuto17320508 5:63462d696256 72 void setEncoder(Ec *ec)
yuto17320508 5:63462d696256 73 {
yuto17320508 5:63462d696256 74 ec_ = ec;
yuto17320508 5:63462d696256 75 }; //エンコーダを設定
yuto17320508 5:63462d696256 76 void setResolution(int reso)
yuto17320508 5:63462d696256 77 {
yuto17320508 5:63462d696256 78 resolution_ = reso;
yuto17320508 5:63462d696256 79 };
yuto17320508 4:81f01f93e502 80 void output(); //出力するだけ
yuto17320508 4:81f01f93e502 81 void output(float duty);
yuto17320508 4:81f01f93e502 82 };
yuto17320508 4:81f01f93e502 83
yuto17320508 4:81f01f93e502 84 class OneLeg //足の挙動を制御する
yuto17320508 4:81f01f93e502 85 {
yuto17320508 4:81f01f93e502 86 Motor *motor_;
yuto17320508 4:81f01f93e502 87 float target_pose_;
yuto17320508 4:81f01f93e502 88
yuto17320508 5:63462d696256 89 public:
yuto17320508 4:81f01f93e502 90 PIDcontroller *pid_;
yuto17320508 5:63462d696256 91 OneLeg() {};
yuto17320508 5:63462d696256 92 void setMotor(Motor *motor)
yuto17320508 5:63462d696256 93 {
yuto17320508 5:63462d696256 94 motor_ = motor;
yuto17320508 5:63462d696256 95 };
yuto17320508 5:63462d696256 96 void setPIDcontroller(PIDcontroller *pid)
yuto17320508 5:63462d696256 97 {
yuto17320508 5:63462d696256 98 pid_ = pid;
yuto17320508 5:63462d696256 99 };
yuto17320508 4:81f01f93e502 100 void setTargetPose(float target_pose);
yuto17320508 4:81f01f93e502 101 void actMotor();//モータ出力
yuto17320508 4:81f01f93e502 102 };
yuto17320508 4:81f01f93e502 103
yuto17320508 4:81f01f93e502 104 class Robot
yuto17320508 4:81f01f93e502 105 {
yuto17320508 4:81f01f93e502 106 float ticker_time_, air_wait_time_;
yuto17320508 4:81f01f93e502 107 OneLeg *Leg1_, *Leg2_;
yuto17320508 4:81f01f93e502 108 Timer timer;
yuto17320508 4:81f01f93e502 109
yuto17320508 5:63462d696256 110 public:
yuto17320508 5:63462d696256 111 Robot()
yuto17320508 5:63462d696256 112 {
yuto17320508 5:63462d696256 113 timer.reset();
yuto17320508 5:63462d696256 114 timer.start();
yuto17320508 5:63462d696256 115 };
yuto17320508 4:81f01f93e502 116 void setLeg(OneLeg *Leg1_, OneLeg *Leg2_);
yuto17320508 4:81f01f93e502 117 void setTickerTime(float ticker_time);
yuto17320508 4:81f01f93e502 118 void run();//ここがメインで走る記述
yuto17320508 4:81f01f93e502 119 };
yuto17320508 4:81f01f93e502 120
yuto17320508 4:81f01f93e502 121
yuto17320508 4:81f01f93e502 122
yuto17320508 4:81f01f93e502 123 PIDcontroller::PIDcontroller(float Kp, float Ki, float Kd)
yuto17320508 4:81f01f93e502 124 {
yuto17320508 4:81f01f93e502 125 Kp_ = Kp, Ki_ = Ki, Kd_ = Kd;
yuto17320508 4:81f01f93e502 126 DEBUG("set Kp:%.3f KI:%.3f Kd:%.3f \n\r", Kp_, Ki_, Kd_);
yuto17320508 4:81f01f93e502 127 IsConvergence_=true;
yuto17320508 4:81f01f93e502 128 }
yuto17320508 4:81f01f93e502 129 void PIDcontroller::setTarget(float target)
yuto17320508 4:81f01f93e502 130 {
yuto17320508 5:63462d696256 131 if (IsConvergence_) { //収束時のみ変更可能
yuto17320508 4:81f01f93e502 132 target_ = target;
yuto17320508 4:81f01f93e502 133 DEBUG("set Target: %.3f\n\r", target_);
yuto17320508 4:81f01f93e502 134 IsConvergence_ = false;
yuto17320508 5:63462d696256 135 } else {
yuto17320508 4:81f01f93e502 136 DEBUG("error: setTarget permission denied!\n");
yuto17320508 4:81f01f93e502 137 }
yuto17320508 4:81f01f93e502 138 }
yuto17320508 4:81f01f93e502 139 float PIDcontroller::calc(float nowVal)
yuto17320508 4:81f01f93e502 140 {
yuto17320508 4:81f01f93e502 141 float out = 0;
yuto17320508 4:81f01f93e502 142 //PID計算ここで行う
yuto17320508 4:81f01f93e502 143 float deviation = target_ - nowVal; //目標との差分
yuto17320508 4:81f01f93e502 144 pile_ += deviation; //積分用に和を取る
yuto17320508 4:81f01f93e502 145 out = deviation * Kp_ - (nowVal - value_old_) / time_delta_ * Kd_ + pile_ * Ki_ * time_delta_;
yuto17320508 4:81f01f93e502 146 value_old_ = nowVal; //今のデータを保存
yuto17320508 4:81f01f93e502 147 //
yuto17320508 5:63462d696256 148 if (fabs(deviation) < tolerance_) { //収束した場合
yuto17320508 4:81f01f93e502 149 DEBUG("complete !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\r");
yuto17320508 4:81f01f93e502 150 out = 0;
yuto17320508 4:81f01f93e502 151 pile_ = 0;
yuto17320508 4:81f01f93e502 152 value_old_ = 0;
yuto17320508 4:81f01f93e502 153 IsConvergence_ = true;
yuto17320508 4:81f01f93e502 154 }
yuto17320508 4:81f01f93e502 155 return out;
yuto17320508 4:81f01f93e502 156 }
yuto17320508 4:81f01f93e502 157
yuto17320508 4:81f01f93e502 158 Motor::Motor(PwmOut *forward, PwmOut *back)
yuto17320508 4:81f01f93e502 159 {
yuto17320508 4:81f01f93e502 160 pin_forward_ = forward;
yuto17320508 4:81f01f93e502 161 pin_back_ = back;
yuto17320508 4:81f01f93e502 162 }
yuto17320508 4:81f01f93e502 163 float Motor::getPosi()
yuto17320508 4:81f01f93e502 164 {
yuto17320508 4:81f01f93e502 165 float posi = 2.0*180*((float)(ec_->getCount)()/(float)resolution_);
yuto17320508 5:63462d696256 166
yuto17320508 4:81f01f93e502 167 //DEBUG("value :%d :%d\n\r", (ec_->getCount)(),resolution_);
yuto17320508 4:81f01f93e502 168 DEBUG("posi is %.4f\n\r",posi);
yuto17320508 4:81f01f93e502 169 return posi;
yuto17320508 4:81f01f93e502 170 }
yuto17320508 4:81f01f93e502 171 void Motor::calcDuty(PIDcontroller *pid)
yuto17320508 5:63462d696256 172 {
yuto17320508 4:81f01f93e502 173 duty_ = pid->calc(getPosi());
yuto17320508 4:81f01f93e502 174 DEBUG("duty is %.4f\n\r",duty_);
yuto17320508 4:81f01f93e502 175 }
yuto17320508 4:81f01f93e502 176 void Motor::output()
yuto17320508 4:81f01f93e502 177 {
yuto17320508 4:81f01f93e502 178 //DEBUG("dutyOut %.3f\n\r",duty_);
yuto17320508 4:81f01f93e502 179 //加速度が一定値を超えたら変更加える
yuto17320508 5:63462d696256 180 if (duty_ > 0) {
yuto17320508 12:9a5de6adae9c 181 //if (duty_ - pre_duty_ > accel_max) duty_ = pre_duty_ + accel_max;
yuto17320508 4:81f01f93e502 182 double output_duty=min(fabs(duty_), duty_limit_);
yuto17320508 4:81f01f93e502 183 pin_forward_->write(output_duty);
yuto17320508 4:81f01f93e502 184 pin_back_->write(0);
yuto17320508 4:81f01f93e502 185 DEBUG("forward %.3f\n\r",pin_forward_->read());
yuto17320508 5:63462d696256 186 } else {
yuto17320508 12:9a5de6adae9c 187 //if (pre_duty_ - duty_ > accel_max)
yuto17320508 12:9a5de6adae9c 188 // duty_ = pre_duty_ - accel_max;
yuto17320508 4:81f01f93e502 189 double output_duty=min(fabs(duty_), duty_limit_);
yuto17320508 4:81f01f93e502 190 pin_forward_->write(0);
yuto17320508 4:81f01f93e502 191 pin_back_->write(output_duty);
yuto17320508 4:81f01f93e502 192 DEBUG("back %.3f\n\r",pin_back_->read());
yuto17320508 4:81f01f93e502 193 }
yuto17320508 4:81f01f93e502 194 pre_duty_ = duty_;
yuto17320508 4:81f01f93e502 195 }
yuto17320508 4:81f01f93e502 196 void Motor::output(float duty)
yuto17320508 4:81f01f93e502 197 {
yuto17320508 4:81f01f93e502 198 duty_ = duty;
yuto17320508 4:81f01f93e502 199 //DEBUG("dutyOut %.3f\n\r",duty_);
yuto17320508 4:81f01f93e502 200 //加速度が一定値を超えたら変更加える
yuto17320508 5:63462d696256 201 if (duty_ > 0) {
yuto17320508 4:81f01f93e502 202 //if (duty_ - pre_duty_ > accel_max) duty_ = pre_duty_ + accel_max;
yuto17320508 4:81f01f93e502 203 double output_duty=min(fabs(duty_), duty_limit_);
yuto17320508 4:81f01f93e502 204 pin_forward_->write(output_duty);
yuto17320508 4:81f01f93e502 205 pin_back_->write(0);
yuto17320508 4:81f01f93e502 206 DEBUG("forward %.3f\n\r",pin_forward_->read());
yuto17320508 5:63462d696256 207 } else {
yuto17320508 4:81f01f93e502 208 //if (pre_duty_ - duty_ > accel_max)
yuto17320508 4:81f01f93e502 209 // duty_ = pre_duty_ - accel_max;
yuto17320508 4:81f01f93e502 210 double output_duty=min(fabs(duty_), duty_limit_);
yuto17320508 4:81f01f93e502 211 pin_forward_->write(0);
yuto17320508 4:81f01f93e502 212 pin_back_->write(output_duty);
yuto17320508 4:81f01f93e502 213 DEBUG("back %.3f\n\r",pin_back_->read());
yuto17320508 4:81f01f93e502 214 }
yuto17320508 4:81f01f93e502 215 pre_duty_ = duty_;
yuto17320508 4:81f01f93e502 216 }
yuto17320508 4:81f01f93e502 217
yuto17320508 4:81f01f93e502 218 void OneLeg::setTargetPose(float target_pose)
yuto17320508 4:81f01f93e502 219 {
yuto17320508 4:81f01f93e502 220 target_pose_ = target_pose;
yuto17320508 4:81f01f93e502 221 //PIDにtargetを送る
yuto17320508 4:81f01f93e502 222 pid_->setTarget(target_pose_);
yuto17320508 4:81f01f93e502 223 }
yuto17320508 4:81f01f93e502 224 void OneLeg::actMotor()
yuto17320508 4:81f01f93e502 225 {
yuto17320508 4:81f01f93e502 226 motor_->calcDuty(pid_);
yuto17320508 4:81f01f93e502 227 motor_->output();
yuto17320508 4:81f01f93e502 228 }
yuto17320508 4:81f01f93e502 229
yuto17320508 4:81f01f93e502 230
yuto17320508 4:81f01f93e502 231
yuto17320508 4:81f01f93e502 232 void Robot::setLeg(OneLeg *Leg1, OneLeg *Leg2)
yuto17320508 4:81f01f93e502 233 {
yuto17320508 4:81f01f93e502 234 Leg1_ = Leg1;
yuto17320508 4:81f01f93e502 235 Leg2_ = Leg2;
yuto17320508 4:81f01f93e502 236 }
yuto17320508 4:81f01f93e502 237 void Robot::setTickerTime(float ticker_time)
yuto17320508 4:81f01f93e502 238 {
yuto17320508 4:81f01f93e502 239 ticker_time_ = ticker_time;
yuto17320508 4:81f01f93e502 240 Leg1_->pid_->setTimeDelta(ticker_time_);
yuto17320508 4:81f01f93e502 241 Leg2_->pid_->setTimeDelta(ticker_time_);
yuto17320508 4:81f01f93e502 242 }
yuto17320508 4:81f01f93e502 243 void Robot::run()
yuto17320508 4:81f01f93e502 244 {
yuto17320508 5:63462d696256 245 while (!Leg1_->pid_->IsConvergence_ || !Leg2_->pid_->IsConvergence_) { //片方が収束していない時*/
yuto17320508 4:81f01f93e502 246 //ticker_time毎にモータに出力する
yuto17320508 4:81f01f93e502 247 float time_s = timer.read();
yuto17320508 4:81f01f93e502 248 Leg1_->actMotor();
yuto17320508 4:81f01f93e502 249 Leg2_->actMotor();
yuto17320508 4:81f01f93e502 250 float rest_time_s = ticker_time_ - (timer.read() - time_s);
yuto17320508 4:81f01f93e502 251 //ticker_timeまで待機
yuto17320508 5:63462d696256 252 if (rest_time_s > 0) {
yuto17320508 4:81f01f93e502 253 wait(rest_time_s);
yuto17320508 4:81f01f93e502 254 DEBUG("start:%.3f last: %.3f restTime: %.3f\n\r",time_s, timer.read(),rest_time_s);
yuto17320508 4:81f01f93e502 255 }
yuto17320508 5:63462d696256 256
yuto17320508 4:81f01f93e502 257 else //時間が足りない場合警告
yuto17320508 4:81f01f93e502 258 printf("error: restTime not enough\n\r");
yuto17320508 4:81f01f93e502 259 DEBUG("loop end\n\r")
yuto17320508 4:81f01f93e502 260 }
yuto17320508 4:81f01f93e502 261
yuto17320508 4:81f01f93e502 262 }
yuto17320508 4:81f01f93e502 263
eri 0:c1476d342c13 264
eri 0:c1476d342c13 265
eri 0:c1476d342c13 266 ////////////関数
yuto17320508 4:81f01f93e502 267 void reset();
eri 0:c1476d342c13 268 void setup();
yuto17320508 10:a335588b9ef0 269 void set_gyro();
yuto17320508 10:a335588b9ef0 270 double get_dist_forward();
yuto17320508 10:a335588b9ef0 271 double get_dist_back();
yuto17320508 12:9a5de6adae9c 272 void can_send(int mode, float duty);
yuto17320508 12:9a5de6adae9c 273 void straight();
yuto17320508 12:9a5de6adae9c 274 void turnLeft();
yuto17320508 12:9a5de6adae9c 275 void curveLeft();
yuto17320508 12:9a5de6adae9c 276 void turnRight();
yuto17320508 12:9a5de6adae9c 277 void curveRight();
yuto17320508 13:29e71a2fd11e 278 void turn(float target_degree);//回転角, 収束許容誤差
yuto17320508 10:a335588b9ef0 279 void straightAndInfinity(float target_degree, float tolerance_degree);//角度を補正するのと前進
eri 0:c1476d342c13 280
yuto17320508 12:9a5de6adae9c 281 void wait_gerege();
yuto17320508 12:9a5de6adae9c 282
eri 0:c1476d342c13 283 ////////////定数
eri 0:c1476d342c13 284
eri 0:c1476d342c13 285 ////////////変数
yuto17320508 12:9a5de6adae9c 286 bool hand_mode=NORMAL;
kageyuta 1:86c4c38abe40 287
yuto17320508 4:81f01f93e502 288 //PIDcontroller, Motor, AirCylinderはOneLegのメンバクラスとして扱う
yuto17320508 4:81f01f93e502 289 //しかし変更を多々行うためポインタ渡しにしてある
yuto17320508 4:81f01f93e502 290 //文が長くなると困るため, PID係数の変更は直接PIDコントローラを介して行う
yuto17320508 4:81f01f93e502 291 PIDcontroller pid_lo(0.01, 0.000, 0.000);
yuto17320508 4:81f01f93e502 292 PIDcontroller pid_li(0.01, 0.000, 0.000); //Kp.Ki,Kd
yuto17320508 4:81f01f93e502 293 Motor motor_lo(&motor_lo_f, &motor_lo_b),
yuto17320508 5:63462d696256 294 motor_li(&motor_li_f, &motor_li_b); //forward,backのピンを代入
yuto17320508 4:81f01f93e502 295 OneLeg leg_lo, leg_li;
yuto17320508 4:81f01f93e502 296 Robot robot;
eri 0:c1476d342c13 297
yuto17320508 10:a335588b9ef0 298 int step_num_l, step_num_r;
yuto17320508 10:a335588b9ef0 299
yuto17320508 10:a335588b9ef0 300 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
yuto17320508 10:a335588b9ef0 301 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
yuto17320508 10:a335588b9ef0 302 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kageyuta 1:86c4c38abe40 303 int main()
kageyuta 1:86c4c38abe40 304 {
eri 0:c1476d342c13 305 setup();
yuto17320508 13:29e71a2fd11e 306
yuto17320508 4:81f01f93e502 307 pid_lo.setTolerance(10);
yuto17320508 4:81f01f93e502 308 pid_li.setTolerance(10);
yuto17320508 4:81f01f93e502 309 motor_lo.setEncoder(&ec_lo);
yuto17320508 4:81f01f93e502 310 motor_lo.setResolution(1000);
yuto17320508 4:81f01f93e502 311 motor_li.setEncoder(&ec_li);
yuto17320508 7:cff25545558f 312 motor_li.setResolution(600);
yuto17320508 4:81f01f93e502 313 leg_lo.setMotor(&motor_lo);
yuto17320508 4:81f01f93e502 314 leg_lo.setPIDcontroller(&pid_lo);
yuto17320508 4:81f01f93e502 315 leg_li.setMotor(&motor_li);
yuto17320508 4:81f01f93e502 316 leg_li.setPIDcontroller(&pid_li);
yuto17320508 4:81f01f93e502 317 robot.setLeg(&leg_lo, &leg_li);
yuto17320508 4:81f01f93e502 318 robot.setTickerTime(0.01); //モータ出力間隔 0.01
kageyuta 11:a6fadff7cc78 319 motor_lo.setDutyLimit(0.5);//0.5
yuto17320508 9:e248986c8423 320 motor_li.setDutyLimit(0.5);
yuto17320508 12:9a5de6adae9c 321 printf("reset standby\n\r");
kageyuta 2:55c616d2e0fe 322 reset();
yuto17320508 4:81f01f93e502 323 printf("bus standby\n\r");
yuto17320508 5:63462d696256 324 while(1) {
yuto17320508 5:63462d696256 325 if(bus_in.read() == 1) break;
yuto17320508 3:7a608fbd3bcd 326 }
yuto17320508 4:81f01f93e502 327 printf("bus is %d\n\r", bus_in.read());
yuto17320508 10:a335588b9ef0 328
yuto17320508 12:9a5de6adae9c 329 wait(0.5);
yuto17320508 12:9a5de6adae9c 330
yuto17320508 12:9a5de6adae9c 331 straight();
yuto17320508 12:9a5de6adae9c 332
yuto17320508 12:9a5de6adae9c 333 wait_gerege();
yuto17320508 12:9a5de6adae9c 334
yuto17320508 12:9a5de6adae9c 335 hand_mode = GEREGE;
yuto17320508 12:9a5de6adae9c 336
yuto17320508 10:a335588b9ef0 337 set_gyro();
yuto17320508 4:81f01f93e502 338 //Sample
yuto17320508 10:a335588b9ef0 339 //スタート直進
yuto17320508 10:a335588b9ef0 340 printf("dist:%.3f\r\n", get_dist_forward());
yuto17320508 13:29e71a2fd11e 341 /*
yuto17320508 13:29e71a2fd11e 342 for(int i=0;i<3;++i){
yuto17320508 13:29e71a2fd11e 343 while(get_dist_back() < 280)
yuto17320508 13:29e71a2fd11e 344 {
yuto17320508 13:29e71a2fd11e 345 straightAndInfinity(0, 5);
yuto17320508 13:29e71a2fd11e 346 //wait(0.01);
yuto17320508 13:29e71a2fd11e 347 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 13:29e71a2fd11e 348 }
yuto17320508 3:7a608fbd3bcd 349 }
yuto17320508 12:9a5de6adae9c 350 //printf("back dist:%.3f\r\n", get_dist_back());
yuto17320508 10:a335588b9ef0 351 //段差前旋回
yuto17320508 12:9a5de6adae9c 352 motor_lo.setDutyLimit(0.4);//0.5
yuto17320508 12:9a5de6adae9c 353 motor_li.setDutyLimit(0.4);
yuto17320508 13:29e71a2fd11e 354 turn(40.0);
yuto17320508 10:a335588b9ef0 355 //段差乗り越え
yuto17320508 12:9a5de6adae9c 356 for(int i= 0;i<5;++i) straight();
yuto17320508 12:9a5de6adae9c 357 while(get_dist_back() > 40) straight();
yuto17320508 10:a335588b9ef0 358 //段差後旋回
yuto17320508 12:9a5de6adae9c 359 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 13:29e71a2fd11e 360 turn(90.0);
yuto17320508 12:9a5de6adae9c 361 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 10:a335588b9ef0 362 //前進
yuto17320508 12:9a5de6adae9c 363 motor_lo.setDutyLimit(0.5);//0.5
yuto17320508 12:9a5de6adae9c 364 motor_li.setDutyLimit(0.5);
yuto17320508 13:29e71a2fd11e 365 for(int i=0;i<3;++i)
yuto17320508 12:9a5de6adae9c 366 {
yuto17320508 13:29e71a2fd11e 367 while(get_dist_forward() > 65)
yuto17320508 13:29e71a2fd11e 368 {
yuto17320508 13:29e71a2fd11e 369 straightAndInfinity(90.0, 5.0);
yuto17320508 13:29e71a2fd11e 370 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 13:29e71a2fd11e 371 }
yuto17320508 12:9a5de6adae9c 372 }
yuto17320508 10:a335588b9ef0 373 //ロープ前旋回
yuto17320508 13:29e71a2fd11e 374 printf("before roop deg:%.3f\n\r",degree0);
yuto17320508 13:29e71a2fd11e 375 turn(0);
yuto17320508 10:a335588b9ef0 376 //ロープ
yuto17320508 13:29e71a2fd11e 377 while(mode4.read() == 0)
yuto17320508 13:29e71a2fd11e 378 {
yuto17320508 13:29e71a2fd11e 379
yuto17320508 13:29e71a2fd11e 380 straightAndInfinity(0, 5);
yuto17320508 13:29e71a2fd11e 381 }
yuto17320508 13:29e71a2fd11e 382 */
yuto17320508 13:29e71a2fd11e 383 printf("go to uhai deg:%.3f\n\r",degree0);
yuto17320508 13:29e71a2fd11e 384 for(int i=0;i<3;++i)
kageyuta 11:a6fadff7cc78 385 {
yuto17320508 13:29e71a2fd11e 386 while(get_dist_forward() > 65)
yuto17320508 13:29e71a2fd11e 387 {
yuto17320508 13:29e71a2fd11e 388 straightAndInfinity(0, 5);
yuto17320508 13:29e71a2fd11e 389 //printf("forward:%.3f back:%.3f\r\n", get_dist_forward(),get_dist_back());
yuto17320508 13:29e71a2fd11e 390 }
yuto17320508 13:29e71a2fd11e 391 }
yuto17320508 13:29e71a2fd11e 392 turn(-90);
yuto17320508 13:29e71a2fd11e 393
yuto17320508 13:29e71a2fd11e 394
yuto17320508 13:29e71a2fd11e 395 motor_lo.setDutyLimit(0.3);//0.5
yuto17320508 13:29e71a2fd11e 396 motor_li.setDutyLimit(0.3);
yuto17320508 13:29e71a2fd11e 397
yuto17320508 13:29e71a2fd11e 398 for(int i=0;i<15;++i)straightAndInfinity(-90, 5);
yuto17320508 13:29e71a2fd11e 399 printf("wall standby");
yuto17320508 13:29e71a2fd11e 400 for(int i=0;i<3;++i)
yuto17320508 13:29e71a2fd11e 401 {
yuto17320508 13:29e71a2fd11e 402 while(get_dist_forward() > 60)
yuto17320508 13:29e71a2fd11e 403 {
yuto17320508 13:29e71a2fd11e 404 straightAndInfinity(-90, 5);
yuto17320508 13:29e71a2fd11e 405 printf("forward:%.3f back:%.3f\r\n", get_dist_forward(),get_dist_back());
yuto17320508 13:29e71a2fd11e 406 }
kageyuta 11:a6fadff7cc78 407 }
yuto17320508 12:9a5de6adae9c 408
yuto17320508 12:9a5de6adae9c 409 hand_mode = GOAL;
yuto17320508 12:9a5de6adae9c 410 straight();
yuto17320508 13:29e71a2fd11e 411 printf("uhai!!!!!!!!!!!!!!!");
yuto17320508 10:a335588b9ef0 412 }
yuto17320508 13:29e71a2fd11e 413 void turn(float target_degree)//turn_degreeを超えるまで旋回し続ける関数
yuto17320508 10:a335588b9ef0 414 {
yuto17320508 10:a335588b9ef0 415 if(target_degree - degree0 > 0)
yuto17320508 10:a335588b9ef0 416 {
yuto17320508 10:a335588b9ef0 417 while(target_degree - degree0 > 0)
yuto17320508 12:9a5de6adae9c 418 turnLeft();
yuto17320508 10:a335588b9ef0 419 }
yuto17320508 10:a335588b9ef0 420 else
yuto17320508 10:a335588b9ef0 421 {
yuto17320508 10:a335588b9ef0 422 while(target_degree - degree0 < 0)
yuto17320508 12:9a5de6adae9c 423 turnRight();
yuto17320508 10:a335588b9ef0 424 }
yuto17320508 13:29e71a2fd11e 425 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 10:a335588b9ef0 426 }
yuto17320508 10:a335588b9ef0 427 void straightAndInfinity(float target_degree, float tolerance_degree)
yuto17320508 10:a335588b9ef0 428 {
yuto17320508 12:9a5de6adae9c 429 if(target_degree - degree0 > tolerance_degree) curveLeft();
yuto17320508 12:9a5de6adae9c 430 else if(degree0 - target_degree > tolerance_degree) curveRight();
yuto17320508 12:9a5de6adae9c 431 else straight();
yuto17320508 5:63462d696256 432 }
yuto17320508 12:9a5de6adae9c 433 void straight()
yuto17320508 5:63462d696256 434 {
yuto17320508 12:9a5de6adae9c 435 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 5:63462d696256 436 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 5:63462d696256 437 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 5:63462d696256 438 robot.run();
yuto17320508 4:81f01f93e502 439 motor_lo_f.write(0);
yuto17320508 4:81f01f93e502 440 motor_lo_b.write(0);
yuto17320508 4:81f01f93e502 441 motor_li_f.write(0);
yuto17320508 4:81f01f93e502 442 motor_li_b.write(0);
yuto17320508 5:63462d696256 443 while(1) {
yuto17320508 5:63462d696256 444 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 445 }
yuto17320508 7:cff25545558f 446 step_num_l++;
yuto17320508 7:cff25545558f 447 step_num_r++;
yuto17320508 5:63462d696256 448 }
yuto17320508 12:9a5de6adae9c 449 void turnLeft()
yuto17320508 5:63462d696256 450 {
yuto17320508 12:9a5de6adae9c 451 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 5:63462d696256 452 leg_lo.setTargetPose(360+(step_num_l-2)*180);
yuto17320508 5:63462d696256 453 leg_li.setTargetPose(180+(step_num_l-2)*180);
yuto17320508 5:63462d696256 454 robot.run();
yuto17320508 5:63462d696256 455 motor_lo_f.write(0);
yuto17320508 5:63462d696256 456 motor_lo_b.write(0);
yuto17320508 5:63462d696256 457 motor_li_f.write(0);
yuto17320508 5:63462d696256 458 motor_li_b.write(0);
yuto17320508 5:63462d696256 459 while(1) {
yuto17320508 5:63462d696256 460 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 461 }
yuto17320508 5:63462d696256 462 step_num_r++;
yuto17320508 5:63462d696256 463 step_num_l--;
yuto17320508 5:63462d696256 464 }
yuto17320508 12:9a5de6adae9c 465 void curveLeft()
yuto17320508 10:a335588b9ef0 466 {
yuto17320508 12:9a5de6adae9c 467 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 10:a335588b9ef0 468 leg_lo.setTargetPose(360+(step_num_l-1)*180);
yuto17320508 10:a335588b9ef0 469 leg_li.setTargetPose(180+(step_num_l-1)*180);
yuto17320508 10:a335588b9ef0 470 robot.run();
yuto17320508 10:a335588b9ef0 471 motor_lo_f.write(0);
yuto17320508 10:a335588b9ef0 472 motor_lo_b.write(0);
yuto17320508 10:a335588b9ef0 473 motor_li_f.write(0);
yuto17320508 10:a335588b9ef0 474 motor_li_b.write(0);
yuto17320508 10:a335588b9ef0 475 while(1) {
yuto17320508 10:a335588b9ef0 476 if(bus_in.read() == 1) break;
yuto17320508 10:a335588b9ef0 477 }
yuto17320508 10:a335588b9ef0 478 step_num_r++;
yuto17320508 10:a335588b9ef0 479 }
yuto17320508 12:9a5de6adae9c 480 void turnRight()
yuto17320508 5:63462d696256 481 {
yuto17320508 12:9a5de6adae9c 482 can_send(BACK, motor_lo.getDutyLimit());
yuto17320508 8:ded0354412ae 483 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 8:ded0354412ae 484 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 5:63462d696256 485 robot.run();
yuto17320508 5:63462d696256 486 motor_lo_f.write(0);
yuto17320508 5:63462d696256 487 motor_lo_b.write(0);
yuto17320508 5:63462d696256 488 motor_li_f.write(0);
yuto17320508 5:63462d696256 489 motor_li_b.write(0);
yuto17320508 5:63462d696256 490 while(1) {
yuto17320508 5:63462d696256 491 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 492 }
yuto17320508 8:ded0354412ae 493 step_num_r--;
yuto17320508 8:ded0354412ae 494 step_num_l++;
eri 0:c1476d342c13 495 }
yuto17320508 12:9a5de6adae9c 496 void curveRight()
yuto17320508 10:a335588b9ef0 497 {
yuto17320508 12:9a5de6adae9c 498 can_send(STOP, motor_lo.getDutyLimit());
yuto17320508 10:a335588b9ef0 499 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 10:a335588b9ef0 500 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 10:a335588b9ef0 501 robot.run();
yuto17320508 10:a335588b9ef0 502 motor_lo_f.write(0);
yuto17320508 10:a335588b9ef0 503 motor_lo_b.write(0);
yuto17320508 10:a335588b9ef0 504 motor_li_f.write(0);
yuto17320508 10:a335588b9ef0 505 motor_li_b.write(0);
yuto17320508 10:a335588b9ef0 506 while(1) {
yuto17320508 10:a335588b9ef0 507 if(bus_in.read() == 1) break;
yuto17320508 10:a335588b9ef0 508 }
yuto17320508 10:a335588b9ef0 509 step_num_l++;
yuto17320508 10:a335588b9ef0 510 }
eri 0:c1476d342c13 511
eri 0:c1476d342c13 512
kageyuta 1:86c4c38abe40 513 void setup()
kageyuta 1:86c4c38abe40 514 {
eri 0:c1476d342c13 515 can1.frequency(1000000);
eri 0:c1476d342c13 516 motor_lo_f.period_us(100);
eri 0:c1476d342c13 517 motor_lo_b.period_us(100);
eri 0:c1476d342c13 518 motor_li_f.period_us(100);
eri 0:c1476d342c13 519 motor_li_b.period_us(100);
kageyuta 1:86c4c38abe40 520
eri 0:c1476d342c13 521 hand.mode(PullUp);
kageyuta 2:55c616d2e0fe 522 switch_lo.mode(PullUp);
kageyuta 2:55c616d2e0fe 523 switch_li.mode(PullUp);
yuto17320508 13:29e71a2fd11e 524 mode4.mode(PullUp);
kageyuta 1:86c4c38abe40 525
yuto17320508 10:a335588b9ef0 526 }
eri 0:c1476d342c13 527
yuto17320508 10:a335588b9ef0 528 void set_gyro()
yuto17320508 10:a335588b9ef0 529 {
eri 0:c1476d342c13 530 device.baud(115200);
eri 0:c1476d342c13 531 device.format(8,Serial::None,1);
eri 0:c1476d342c13 532 device.attach(dev_rx, Serial::RxIrq);
eri 0:c1476d342c13 533 wait(0.05);
eri 0:c1476d342c13 534 theta0=degree0;
eri 0:c1476d342c13 535 check_gyro();
eri 0:c1476d342c13 536 }
eri 0:c1476d342c13 537
eri 0:c1476d342c13 538
eri 0:c1476d342c13 539 //////////////////////////////////////can
yuto17320508 12:9a5de6adae9c 540 void can_send(int mode, float duty)
eri 0:c1476d342c13 541 {
yuto17320508 12:9a5de6adae9c 542 char data[2]= {0};
yuto17320508 12:9a5de6adae9c 543 int send=mode * 10 + (int)(duty * 10.0);
yuto17320508 12:9a5de6adae9c 544 //printf("duty is %.3f\n\r",duty);
yuto17320508 12:9a5de6adae9c 545 data[0]=send & 0b11111111;
yuto17320508 12:9a5de6adae9c 546 data[1]=hand_mode;
kageyuta 1:86c4c38abe40 547
yuto17320508 12:9a5de6adae9c 548 if(can1.write(CANMessage(0,data,2)))led4=1;
eri 0:c1476d342c13 549 else led4=0;
kageyuta 1:86c4c38abe40 550 }
kageyuta 2:55c616d2e0fe 551 void reset()
kageyuta 2:55c616d2e0fe 552 {
kageyuta 2:55c616d2e0fe 553 while(switch_lo.read()) {
kageyuta 6:fe9fa8c2e6f9 554 motor_lo.output(0.3);
kageyuta 2:55c616d2e0fe 555 }
yuto17320508 5:63462d696256 556 ec_lo.reset();
yuto17320508 5:63462d696256 557 motor_lo.output(0.0);
kageyuta 2:55c616d2e0fe 558 while(switch_li.read()) {
kageyuta 6:fe9fa8c2e6f9 559 motor_li.output(0.3);
kageyuta 2:55c616d2e0fe 560 }
yuto17320508 5:63462d696256 561 ec_li.reset();
yuto17320508 5:63462d696256 562 motor_li.output(0.0);
kageyuta 1:86c4c38abe40 563 }
yuto17320508 10:a335588b9ef0 564 double get_dist_back()
yuto17320508 10:a335588b9ef0 565 {
yuto17320508 10:a335588b9ef0 566 sensor_back.start();
yuto17320508 10:a335588b9ef0 567 //wait_ms(50); //sensor.start()で信号を送ったあと反射波が帰ってきてから距離データが更新されるため、少し待つ必要がある。
yuto17320508 10:a335588b9ef0 568 //何ループも回す場合は不要。また、時間は適当だから調整が必要。
yuto17320508 10:a335588b9ef0 569 return sensor_back.get_dist_cm();
yuto17320508 10:a335588b9ef0 570 }
yuto17320508 10:a335588b9ef0 571 double get_dist_forward()
yuto17320508 10:a335588b9ef0 572 {
kageyuta 11:a6fadff7cc78 573 sensor_forward.start();
kageyuta 11:a6fadff7cc78 574 return sensor_forward.get_dist_cm();
yuto17320508 10:a335588b9ef0 575 }
yuto17320508 12:9a5de6adae9c 576
yuto17320508 12:9a5de6adae9c 577 void wait_gerege()
yuto17320508 12:9a5de6adae9c 578 {
yuto17320508 12:9a5de6adae9c 579 int i = 0;
yuto17320508 12:9a5de6adae9c 580 while(i!=100)
yuto17320508 12:9a5de6adae9c 581 {
yuto17320508 12:9a5de6adae9c 582 if(hand.read()==0)i++;
yuto17320508 12:9a5de6adae9c 583 }
yuto17320508 12:9a5de6adae9c 584 }