ROBOSTEP_5期 / Mbed 2 deprecated George_Master_Param

Dependencies:   mbed robot

Committer:
yuto17320508
Date:
Thu May 02 04:52:47 2019 +0000
Revision:
12:9a5de6adae9c
Parent:
11:a6fadff7cc78
Child:
13:29e71a2fd11e
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 10:a335588b9ef0 278 void turn(float turn_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 4:81f01f93e502 306 pid_lo.setTolerance(10);
yuto17320508 4:81f01f93e502 307 pid_li.setTolerance(10);
yuto17320508 4:81f01f93e502 308 motor_lo.setEncoder(&ec_lo);
yuto17320508 4:81f01f93e502 309 motor_lo.setResolution(1000);
yuto17320508 4:81f01f93e502 310 motor_li.setEncoder(&ec_li);
yuto17320508 7:cff25545558f 311 motor_li.setResolution(600);
yuto17320508 4:81f01f93e502 312 leg_lo.setMotor(&motor_lo);
yuto17320508 4:81f01f93e502 313 leg_lo.setPIDcontroller(&pid_lo);
yuto17320508 4:81f01f93e502 314 leg_li.setMotor(&motor_li);
yuto17320508 4:81f01f93e502 315 leg_li.setPIDcontroller(&pid_li);
yuto17320508 4:81f01f93e502 316 robot.setLeg(&leg_lo, &leg_li);
yuto17320508 4:81f01f93e502 317 robot.setTickerTime(0.01); //モータ出力間隔 0.01
kageyuta 11:a6fadff7cc78 318 motor_lo.setDutyLimit(0.5);//0.5
yuto17320508 9:e248986c8423 319 motor_li.setDutyLimit(0.5);
yuto17320508 12:9a5de6adae9c 320 printf("reset standby\n\r");
kageyuta 2:55c616d2e0fe 321 reset();
yuto17320508 4:81f01f93e502 322 printf("bus standby\n\r");
yuto17320508 5:63462d696256 323 while(1) {
yuto17320508 5:63462d696256 324 if(bus_in.read() == 1) break;
yuto17320508 3:7a608fbd3bcd 325 }
yuto17320508 4:81f01f93e502 326 printf("bus is %d\n\r", bus_in.read());
yuto17320508 10:a335588b9ef0 327
yuto17320508 12:9a5de6adae9c 328 wait(0.5);
yuto17320508 12:9a5de6adae9c 329
yuto17320508 12:9a5de6adae9c 330 straight();
yuto17320508 12:9a5de6adae9c 331
yuto17320508 12:9a5de6adae9c 332 wait_gerege();
yuto17320508 12:9a5de6adae9c 333
yuto17320508 12:9a5de6adae9c 334 hand_mode = GEREGE;
yuto17320508 12:9a5de6adae9c 335
yuto17320508 10:a335588b9ef0 336 set_gyro();
yuto17320508 4:81f01f93e502 337 //Sample
yuto17320508 10:a335588b9ef0 338 //スタート直進
yuto17320508 10:a335588b9ef0 339 printf("dist:%.3f\r\n", get_dist_forward());
kageyuta 11:a6fadff7cc78 340 while(get_dist_back() < 300)
yuto17320508 4:81f01f93e502 341 {
kageyuta 11:a6fadff7cc78 342 straightAndInfinity(0, 5);
kageyuta 11:a6fadff7cc78 343 //wait(0.01);
yuto17320508 12:9a5de6adae9c 344 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 3:7a608fbd3bcd 345 }
yuto17320508 12:9a5de6adae9c 346 //printf("back dist:%.3f\r\n", get_dist_back());
yuto17320508 10:a335588b9ef0 347 //段差前旋回
yuto17320508 12:9a5de6adae9c 348 motor_lo.setDutyLimit(0.4);//0.5
yuto17320508 12:9a5de6adae9c 349 motor_li.setDutyLimit(0.4);
yuto17320508 10:a335588b9ef0 350 turn(45.0);
yuto17320508 10:a335588b9ef0 351 //段差乗り越え
yuto17320508 12:9a5de6adae9c 352 for(int i= 0;i<5;++i) straight();
yuto17320508 12:9a5de6adae9c 353 while(get_dist_back() > 40) straight();
yuto17320508 10:a335588b9ef0 354 //段差後旋回
yuto17320508 12:9a5de6adae9c 355 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 10:a335588b9ef0 356 turn(45.0);
yuto17320508 12:9a5de6adae9c 357 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 10:a335588b9ef0 358 //前進
yuto17320508 12:9a5de6adae9c 359 motor_lo.setDutyLimit(0.5);//0.5
yuto17320508 12:9a5de6adae9c 360 motor_li.setDutyLimit(0.5);
yuto17320508 12:9a5de6adae9c 361 while(get_dist_forward() > 60)
yuto17320508 12:9a5de6adae9c 362 {
yuto17320508 12:9a5de6adae9c 363 straightAndInfinity(90.0, 5.0);
yuto17320508 12:9a5de6adae9c 364 printf("angle:%.3f backdist:%.2f\r\n", degree0, get_dist_back());
yuto17320508 12:9a5de6adae9c 365 }
yuto17320508 10:a335588b9ef0 366 //ロープ前旋回
yuto17320508 10:a335588b9ef0 367 turn(-90);
yuto17320508 10:a335588b9ef0 368 //ロープ
yuto17320508 12:9a5de6adae9c 369 while(get_dist_forward() > 60)
kageyuta 11:a6fadff7cc78 370 {
kageyuta 11:a6fadff7cc78 371 straightAndInfinity(0, 5);
yuto17320508 12:9a5de6adae9c 372 //printf("forward:%.3f back:%.3f\r\n", get_dist_forward(),get_dist_back());
kageyuta 11:a6fadff7cc78 373 }
yuto17320508 12:9a5de6adae9c 374
yuto17320508 12:9a5de6adae9c 375 turn(-90);
yuto17320508 12:9a5de6adae9c 376
yuto17320508 12:9a5de6adae9c 377 hand_mode = GOAL;
yuto17320508 12:9a5de6adae9c 378
yuto17320508 12:9a5de6adae9c 379 straight();
yuto17320508 10:a335588b9ef0 380 }
yuto17320508 10:a335588b9ef0 381 void turn(float turn_degree)//turn_degreeを超えるまで旋回し続ける関数
yuto17320508 10:a335588b9ef0 382 {
yuto17320508 10:a335588b9ef0 383 float target_degree = degree0 + turn_degree;
yuto17320508 10:a335588b9ef0 384 if(target_degree - degree0 > 0)
yuto17320508 10:a335588b9ef0 385 {
yuto17320508 10:a335588b9ef0 386 while(target_degree - degree0 > 0)
yuto17320508 12:9a5de6adae9c 387 turnLeft();
yuto17320508 10:a335588b9ef0 388 }
yuto17320508 10:a335588b9ef0 389 else
yuto17320508 10:a335588b9ef0 390 {
yuto17320508 10:a335588b9ef0 391 while(target_degree - degree0 < 0)
yuto17320508 12:9a5de6adae9c 392 turnRight();
yuto17320508 10:a335588b9ef0 393 }
yuto17320508 10:a335588b9ef0 394 }
yuto17320508 10:a335588b9ef0 395 void straightAndInfinity(float target_degree, float tolerance_degree)
yuto17320508 10:a335588b9ef0 396 {
yuto17320508 12:9a5de6adae9c 397 if(target_degree - degree0 > tolerance_degree) curveLeft();
yuto17320508 12:9a5de6adae9c 398 else if(degree0 - target_degree > tolerance_degree) curveRight();
yuto17320508 12:9a5de6adae9c 399 else straight();
yuto17320508 5:63462d696256 400 }
yuto17320508 12:9a5de6adae9c 401 void straight()
yuto17320508 5:63462d696256 402 {
yuto17320508 12:9a5de6adae9c 403 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 5:63462d696256 404 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 5:63462d696256 405 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 5:63462d696256 406 robot.run();
yuto17320508 4:81f01f93e502 407 motor_lo_f.write(0);
yuto17320508 4:81f01f93e502 408 motor_lo_b.write(0);
yuto17320508 4:81f01f93e502 409 motor_li_f.write(0);
yuto17320508 4:81f01f93e502 410 motor_li_b.write(0);
yuto17320508 5:63462d696256 411 while(1) {
yuto17320508 5:63462d696256 412 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 413 }
yuto17320508 7:cff25545558f 414 step_num_l++;
yuto17320508 7:cff25545558f 415 step_num_r++;
yuto17320508 5:63462d696256 416 }
yuto17320508 12:9a5de6adae9c 417 void turnLeft()
yuto17320508 5:63462d696256 418 {
yuto17320508 12:9a5de6adae9c 419 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 5:63462d696256 420 leg_lo.setTargetPose(360+(step_num_l-2)*180);
yuto17320508 5:63462d696256 421 leg_li.setTargetPose(180+(step_num_l-2)*180);
yuto17320508 5:63462d696256 422 robot.run();
yuto17320508 5:63462d696256 423 motor_lo_f.write(0);
yuto17320508 5:63462d696256 424 motor_lo_b.write(0);
yuto17320508 5:63462d696256 425 motor_li_f.write(0);
yuto17320508 5:63462d696256 426 motor_li_b.write(0);
yuto17320508 5:63462d696256 427 while(1) {
yuto17320508 5:63462d696256 428 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 429 }
yuto17320508 5:63462d696256 430 step_num_r++;
yuto17320508 5:63462d696256 431 step_num_l--;
yuto17320508 5:63462d696256 432 }
yuto17320508 12:9a5de6adae9c 433 void curveLeft()
yuto17320508 10:a335588b9ef0 434 {
yuto17320508 12:9a5de6adae9c 435 can_send(FORWARD, motor_lo.getDutyLimit());
yuto17320508 10:a335588b9ef0 436 leg_lo.setTargetPose(360+(step_num_l-1)*180);
yuto17320508 10:a335588b9ef0 437 leg_li.setTargetPose(180+(step_num_l-1)*180);
yuto17320508 10:a335588b9ef0 438 robot.run();
yuto17320508 10:a335588b9ef0 439 motor_lo_f.write(0);
yuto17320508 10:a335588b9ef0 440 motor_lo_b.write(0);
yuto17320508 10:a335588b9ef0 441 motor_li_f.write(0);
yuto17320508 10:a335588b9ef0 442 motor_li_b.write(0);
yuto17320508 10:a335588b9ef0 443 while(1) {
yuto17320508 10:a335588b9ef0 444 if(bus_in.read() == 1) break;
yuto17320508 10:a335588b9ef0 445 }
yuto17320508 10:a335588b9ef0 446 step_num_r++;
yuto17320508 10:a335588b9ef0 447 }
yuto17320508 12:9a5de6adae9c 448 void turnRight()
yuto17320508 5:63462d696256 449 {
yuto17320508 12:9a5de6adae9c 450 can_send(BACK, motor_lo.getDutyLimit());
yuto17320508 8:ded0354412ae 451 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 8:ded0354412ae 452 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 5:63462d696256 453 robot.run();
yuto17320508 5:63462d696256 454 motor_lo_f.write(0);
yuto17320508 5:63462d696256 455 motor_lo_b.write(0);
yuto17320508 5:63462d696256 456 motor_li_f.write(0);
yuto17320508 5:63462d696256 457 motor_li_b.write(0);
yuto17320508 5:63462d696256 458 while(1) {
yuto17320508 5:63462d696256 459 if(bus_in.read() == 1) break;
yuto17320508 5:63462d696256 460 }
yuto17320508 8:ded0354412ae 461 step_num_r--;
yuto17320508 8:ded0354412ae 462 step_num_l++;
eri 0:c1476d342c13 463 }
yuto17320508 12:9a5de6adae9c 464 void curveRight()
yuto17320508 10:a335588b9ef0 465 {
yuto17320508 12:9a5de6adae9c 466 can_send(STOP, motor_lo.getDutyLimit());
yuto17320508 10:a335588b9ef0 467 leg_lo.setTargetPose(360+step_num_l*180);
yuto17320508 10:a335588b9ef0 468 leg_li.setTargetPose(180+step_num_l*180);
yuto17320508 10:a335588b9ef0 469 robot.run();
yuto17320508 10:a335588b9ef0 470 motor_lo_f.write(0);
yuto17320508 10:a335588b9ef0 471 motor_lo_b.write(0);
yuto17320508 10:a335588b9ef0 472 motor_li_f.write(0);
yuto17320508 10:a335588b9ef0 473 motor_li_b.write(0);
yuto17320508 10:a335588b9ef0 474 while(1) {
yuto17320508 10:a335588b9ef0 475 if(bus_in.read() == 1) break;
yuto17320508 10:a335588b9ef0 476 }
yuto17320508 10:a335588b9ef0 477 step_num_l++;
yuto17320508 10:a335588b9ef0 478 }
eri 0:c1476d342c13 479
eri 0:c1476d342c13 480
kageyuta 1:86c4c38abe40 481 void setup()
kageyuta 1:86c4c38abe40 482 {
eri 0:c1476d342c13 483 can1.frequency(1000000);
eri 0:c1476d342c13 484 motor_lo_f.period_us(100);
eri 0:c1476d342c13 485 motor_lo_b.period_us(100);
eri 0:c1476d342c13 486 motor_li_f.period_us(100);
eri 0:c1476d342c13 487 motor_li_b.period_us(100);
kageyuta 1:86c4c38abe40 488
eri 0:c1476d342c13 489 hand.mode(PullUp);
kageyuta 2:55c616d2e0fe 490 switch_lo.mode(PullUp);
kageyuta 2:55c616d2e0fe 491 switch_li.mode(PullUp);
eri 0:c1476d342c13 492 switch4.mode(PullUp);
kageyuta 1:86c4c38abe40 493
yuto17320508 10:a335588b9ef0 494 }
eri 0:c1476d342c13 495
yuto17320508 10:a335588b9ef0 496 void set_gyro()
yuto17320508 10:a335588b9ef0 497 {
eri 0:c1476d342c13 498 device.baud(115200);
eri 0:c1476d342c13 499 device.format(8,Serial::None,1);
eri 0:c1476d342c13 500 device.attach(dev_rx, Serial::RxIrq);
eri 0:c1476d342c13 501 wait(0.05);
eri 0:c1476d342c13 502 theta0=degree0;
eri 0:c1476d342c13 503 check_gyro();
eri 0:c1476d342c13 504 }
eri 0:c1476d342c13 505
eri 0:c1476d342c13 506
eri 0:c1476d342c13 507 //////////////////////////////////////can
yuto17320508 12:9a5de6adae9c 508 void can_send(int mode, float duty)
eri 0:c1476d342c13 509 {
yuto17320508 12:9a5de6adae9c 510 char data[2]= {0};
yuto17320508 12:9a5de6adae9c 511 int send=mode * 10 + (int)(duty * 10.0);
yuto17320508 12:9a5de6adae9c 512 //printf("duty is %.3f\n\r",duty);
yuto17320508 12:9a5de6adae9c 513 data[0]=send & 0b11111111;
yuto17320508 12:9a5de6adae9c 514 data[1]=hand_mode;
kageyuta 1:86c4c38abe40 515
yuto17320508 12:9a5de6adae9c 516 if(can1.write(CANMessage(0,data,2)))led4=1;
eri 0:c1476d342c13 517 else led4=0;
kageyuta 1:86c4c38abe40 518 }
kageyuta 2:55c616d2e0fe 519 void reset()
kageyuta 2:55c616d2e0fe 520 {
kageyuta 2:55c616d2e0fe 521 while(switch_lo.read()) {
kageyuta 6:fe9fa8c2e6f9 522 motor_lo.output(0.3);
kageyuta 2:55c616d2e0fe 523 }
yuto17320508 5:63462d696256 524 ec_lo.reset();
yuto17320508 5:63462d696256 525 motor_lo.output(0.0);
kageyuta 2:55c616d2e0fe 526 while(switch_li.read()) {
kageyuta 6:fe9fa8c2e6f9 527 motor_li.output(0.3);
kageyuta 2:55c616d2e0fe 528 }
yuto17320508 5:63462d696256 529 ec_li.reset();
yuto17320508 5:63462d696256 530 motor_li.output(0.0);
kageyuta 1:86c4c38abe40 531 }
yuto17320508 10:a335588b9ef0 532 double get_dist_back()
yuto17320508 10:a335588b9ef0 533 {
yuto17320508 10:a335588b9ef0 534 sensor_back.start();
yuto17320508 10:a335588b9ef0 535 //wait_ms(50); //sensor.start()で信号を送ったあと反射波が帰ってきてから距離データが更新されるため、少し待つ必要がある。
yuto17320508 10:a335588b9ef0 536 //何ループも回す場合は不要。また、時間は適当だから調整が必要。
yuto17320508 10:a335588b9ef0 537 return sensor_back.get_dist_cm();
yuto17320508 10:a335588b9ef0 538 }
yuto17320508 10:a335588b9ef0 539 double get_dist_forward()
yuto17320508 10:a335588b9ef0 540 {
kageyuta 11:a6fadff7cc78 541 sensor_forward.start();
kageyuta 11:a6fadff7cc78 542 return sensor_forward.get_dist_cm();
yuto17320508 10:a335588b9ef0 543 }
yuto17320508 12:9a5de6adae9c 544
yuto17320508 12:9a5de6adae9c 545 void wait_gerege()
yuto17320508 12:9a5de6adae9c 546 {
yuto17320508 12:9a5de6adae9c 547 int i = 0;
yuto17320508 12:9a5de6adae9c 548 while(i!=100)
yuto17320508 12:9a5de6adae9c 549 {
yuto17320508 12:9a5de6adae9c 550 if(hand.read()==0)i++;
yuto17320508 12:9a5de6adae9c 551 }
yuto17320508 12:9a5de6adae9c 552 }