right and left move at the same time

Dependencies:   mbed robot

Committer:
shimizuta
Date:
Wed May 08 08:45:48 2019 +0000
Revision:
11:0522b336eb82
Parent:
10:2973cea54efd
Child:
12:e6fd3a3201f0
if it was not convergence, led[0] is on

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eri 0:411ab20ce87d 1 #include "mbed.h"
eri 0:411ab20ce87d 2 #include "pin.h"
shimizuta 9:ac95473a5d86 3 #include "debug.h"
shimizuta 9:ac95473a5d86 4 #include "robot.h"
shimizuta 9:ac95473a5d86 5 enum WALKMODE {
yuto17320508 7:c5c60192eb02 6 BACK,
yuto17320508 7:c5c60192eb02 7 STOP,
yuto17320508 7:c5c60192eb02 8 FORWARD,
yuto17320508 7:c5c60192eb02 9 };
shimizuta 9:ac95473a5d86 10 enum EVENT {
yuto17320508 7:c5c60192eb02 11 NORMAL,
yuto17320508 7:c5c60192eb02 12 GEREGE,
shimizuta 9:ac95473a5d86 13 GOAL,
yuto17320508 7:c5c60192eb02 14 };
kageyuta 1:34371ffd3dc0 15
yuto17320508 2:db2bc2ae4d20 16 float accel_max = 0.01; //これグローバルにしたのはごめん。set関数多すぎてめんどくなった。
yuto17320508 8:43c31d2afb9e 17 int hand_mode=NORMAL;
yuto17320508 2:db2bc2ae4d20 18
yuto17320508 2:db2bc2ae4d20 19 void setup();
yuto17320508 7:c5c60192eb02 20 void forward();
yuto17320508 7:c5c60192eb02 21 void stop();
yuto17320508 7:c5c60192eb02 22 void back();
yuto17320508 7:c5c60192eb02 23 void can_receive(int &mode, float &duty);
yuto17320508 2:db2bc2ae4d20 24 void reset();
eri 0:411ab20ce87d 25
yuto17320508 7:c5c60192eb02 26
yuto17320508 2:db2bc2ae4d20 27 //PIDcontroller, Motor, AirCylinderはOneLegのメンバクラスとして扱う
yuto17320508 2:db2bc2ae4d20 28 //しかし変更を多々行うためポインタ渡しにしてある
yuto17320508 2:db2bc2ae4d20 29 //文が長くなると困るため, PID係数の変更は直接PIDコントローラを介して行う
yuto17320508 2:db2bc2ae4d20 30 PIDcontroller pid_ro(0.01, 0.000, 0.000);
yuto17320508 2:db2bc2ae4d20 31 PIDcontroller pid_ri(0.01, 0.000, 0.000); //Kp.Ki,Kd
yuto17320508 2:db2bc2ae4d20 32 Motor motor_ro(&motor_ro_f, &motor_ro_b),
yuto17320508 7:c5c60192eb02 33 motor_ri(&motor_ri_f, &motor_ri_b); //forward,backのピンを代入
yuto17320508 2:db2bc2ae4d20 34 OneLeg leg_ro, leg_ri;
yuto17320508 2:db2bc2ae4d20 35 Robot robot;
shimizuta 11:0522b336eb82 36 DigitalOut led[] = {LED1,LED2,LED3,LED4};
shimizuta 11:0522b336eb82 37
eri 0:411ab20ce87d 38
yuto17320508 7:c5c60192eb02 39 int step_num_r = 0;
kageyuta 1:34371ffd3dc0 40 int main()
kageyuta 1:34371ffd3dc0 41 {
yuto17320508 2:db2bc2ae4d20 42 printf("standby ok\n\r");
eri 0:411ab20ce87d 43 setup();
yuto17320508 2:db2bc2ae4d20 44 pid_ro.setTolerance(10);
yuto17320508 2:db2bc2ae4d20 45 pid_ri.setTolerance(10);
yuto17320508 7:c5c60192eb02 46
yuto17320508 2:db2bc2ae4d20 47 motor_ro.setEncoder(&ec_ro);
yuto17320508 2:db2bc2ae4d20 48 motor_ro.setResolution(1000);
yuto17320508 2:db2bc2ae4d20 49 motor_ri.setEncoder(&ec_ri);
yuto17320508 2:db2bc2ae4d20 50 motor_ri.setResolution(1000);
yuto17320508 7:c5c60192eb02 51
yuto17320508 2:db2bc2ae4d20 52 leg_ro.setMotor(&motor_ro);
yuto17320508 2:db2bc2ae4d20 53 leg_ro.setPIDcontroller(&pid_ro);
yuto17320508 2:db2bc2ae4d20 54 leg_ri.setMotor(&motor_ri);
yuto17320508 2:db2bc2ae4d20 55 leg_ri.setPIDcontroller(&pid_ri);
yuto17320508 7:c5c60192eb02 56
yuto17320508 2:db2bc2ae4d20 57 robot.setLeg(&leg_ro, &leg_ri);
yuto17320508 2:db2bc2ae4d20 58 robot.setTickerTime(0.01); //モータ出力間隔 0.01
yuto17320508 7:c5c60192eb02 59
yuto17320508 6:2bc2950f32d8 60 motor_ro.setDutyLimit(0.5);
yuto17320508 6:2bc2950f32d8 61 motor_ri.setDutyLimit(0.5);
yuto17320508 7:c5c60192eb02 62
kageyuta 1:34371ffd3dc0 63 reset();
yuto17320508 10:2973cea54efd 64 for(int i=0; i<5; ++i)
yuto17320508 10:2973cea54efd 65 servo.set_degree(0,0);
yuto17320508 2:db2bc2ae4d20 66 bus_out = 1;
yuto17320508 2:db2bc2ae4d20 67 printf("start\n\r");
yuto17320508 7:c5c60192eb02 68 int mode = -1;
yuto17320508 7:c5c60192eb02 69 air_hand = 1;
shimizuta 9:ac95473a5d86 70
shimizuta 9:ac95473a5d86 71 while(1) {
yuto17320508 7:c5c60192eb02 72 float duty = 0.0;
yuto17320508 7:c5c60192eb02 73 can_receive(mode,duty);
yuto17320508 7:c5c60192eb02 74 printf("mode:%d duty:%.3f\n\r", mode, duty);
yuto17320508 7:c5c60192eb02 75 motor_ro.setDutyLimit(duty);
yuto17320508 7:c5c60192eb02 76 motor_ri.setDutyLimit(duty);
shimizuta 9:ac95473a5d86 77 if(hand_mode == GEREGE) {
yuto17320508 7:c5c60192eb02 78 air_hand = 0;
shimizuta 11:0522b336eb82 79
shimizuta 9:ac95473a5d86 80 } else if(hand_mode == GOAL) {
shimizuta 9:ac95473a5d86 81 for(int i=0; i<10; ++i)
shimizuta 9:ac95473a5d86 82 servo.set_degree(0,(7200 - 3500) * 270.0/(11500 - 3500));
yuto17320508 7:c5c60192eb02 83 }
yuto17320508 7:c5c60192eb02 84 bus_out = 0;
yuto17320508 7:c5c60192eb02 85 //printf("target is %d\n\r",target_ro);
yuto17320508 7:c5c60192eb02 86 if(mode == FORWARD) forward();
yuto17320508 7:c5c60192eb02 87 else if(mode == STOP) stop();
yuto17320508 7:c5c60192eb02 88 else if(mode == BACK) back();
yuto17320508 2:db2bc2ae4d20 89 bus_out = 1;
eri 0:411ab20ce87d 90 }
yuto17320508 7:c5c60192eb02 91
eri 0:411ab20ce87d 92 }
shimizuta 11:0522b336eb82 93 void Run(int d_step_r)
yuto17320508 4:db1640bd0e89 94 {
shimizuta 11:0522b336eb82 95 int target_step = step_num_r + d_step_r;
shimizuta 11:0522b336eb82 96 leg_ro.setTargetPose(180+target_step*180);
shimizuta 11:0522b336eb82 97 leg_ri.setTargetPose(target_step*180);
shimizuta 11:0522b336eb82 98 led[0] = 1;
yuto17320508 7:c5c60192eb02 99 robot.run();
shimizuta 11:0522b336eb82 100 led[0] = 0;
shimizuta 11:0522b336eb82 101 step_num_r = target_step;
yuto17320508 7:c5c60192eb02 102 motor_ro_f.write(0);
yuto17320508 7:c5c60192eb02 103 motor_ro_b.write(0);
yuto17320508 7:c5c60192eb02 104 motor_ri_f.write(0);
yuto17320508 7:c5c60192eb02 105 motor_ri_b.write(0);
shimizuta 11:0522b336eb82 106
shimizuta 11:0522b336eb82 107 }
shimizuta 11:0522b336eb82 108
shimizuta 11:0522b336eb82 109 void forward()
shimizuta 11:0522b336eb82 110 {
shimizuta 11:0522b336eb82 111 Run(1);
yuto17320508 7:c5c60192eb02 112 }
yuto17320508 7:c5c60192eb02 113 void stop()
yuto17320508 7:c5c60192eb02 114 {
shimizuta 11:0522b336eb82 115 Run(0);
yuto17320508 4:db1640bd0e89 116 }
yuto17320508 7:c5c60192eb02 117
yuto17320508 7:c5c60192eb02 118 void back()
yuto17320508 4:db1640bd0e89 119 {
shimizuta 11:0522b336eb82 120 Run(-1);
yuto17320508 4:db1640bd0e89 121 }
eri 0:411ab20ce87d 122 //////////////////////////////////////////////
kageyuta 1:34371ffd3dc0 123 void setup()
kageyuta 1:34371ffd3dc0 124 {
eri 0:411ab20ce87d 125 can.frequency(1000000);
eri 0:411ab20ce87d 126 motor_ro_f.period_us(100);
eri 0:411ab20ce87d 127 motor_ro_b.period_us(100);
eri 0:411ab20ce87d 128 motor_ri_f.period_us(100);
eri 0:411ab20ce87d 129 motor_ri_b.period_us(100);
shimizuta 9:ac95473a5d86 130 air_hand = 1;//ゲレゲを離す手の形にしておく
kageyuta 1:34371ffd3dc0 131 switch_ro.mode(PullUp);
kageyuta 1:34371ffd3dc0 132 switch_ri.mode(PullUp);
eri 0:411ab20ce87d 133 servo.init();
yuto17320508 7:c5c60192eb02 134 wait(0.01);
shimizuta 9:ac95473a5d86 135 for(int i=0; i<5; ++i)
yuto17320508 7:c5c60192eb02 136 servo.set_degree(0,0);
yuto17320508 7:c5c60192eb02 137 //(7200 - 3500) * 270.0/(11500 - 3500)
eri 0:411ab20ce87d 138 }
eri 0:411ab20ce87d 139
kageyuta 1:34371ffd3dc0 140 void reset()
kageyuta 1:34371ffd3dc0 141 {
kageyuta 1:34371ffd3dc0 142 while(switch_ro.read()) {
kageyuta 3:29999b02e940 143 motor_ro.output(0.3);
kageyuta 1:34371ffd3dc0 144 }
yuto17320508 7:c5c60192eb02 145 ec_ro.reset();
yuto17320508 7:c5c60192eb02 146 motor_ro.output(0.0);
yuto17320508 7:c5c60192eb02 147 printf("ro OK\n\r");
kageyuta 3:29999b02e940 148 while(switch_ri.read()) {
kageyuta 3:29999b02e940 149 motor_ri.output(0.3);
kageyuta 3:29999b02e940 150 }
yuto17320508 7:c5c60192eb02 151
yuto17320508 7:c5c60192eb02 152 ec_ri.reset();
yuto17320508 7:c5c60192eb02 153 motor_ri.output(0.0);
yuto17320508 7:c5c60192eb02 154 printf("ri OK\n\r");
eri 0:411ab20ce87d 155 }
eri 0:411ab20ce87d 156
yuto17320508 7:c5c60192eb02 157 void can_receive(int &mode, float &duty)
kageyuta 1:34371ffd3dc0 158 {
eri 0:411ab20ce87d 159 CANMessage msg;
yuto17320508 7:c5c60192eb02 160 while(1) {
yuto17320508 7:c5c60192eb02 161 //printf("roop\r\n");
eri 0:411ab20ce87d 162 if(can.read(msg)) {
eri 0:411ab20ce87d 163 if(msg.id==0) {
yuto17320508 7:c5c60192eb02 164 mode= msg.data[0]*0.1;
yuto17320508 7:c5c60192eb02 165 duty = (msg.data[0]%10)*0.1;
yuto17320508 7:c5c60192eb02 166 hand_mode= msg.data[1];
eri 0:411ab20ce87d 167 break;
eri 0:411ab20ce87d 168 }
kageyuta 1:34371ffd3dc0 169 led2=1;
eri 0:411ab20ce87d 170 } else led2=0;
eri 0:411ab20ce87d 171 }
yuto17320508 7:c5c60192eb02 172 }