![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DRのPS3での操作用(アームの開閉をコントローラで制御可能)
Dependencies: mbed CalPID motorout ros_lib_melodic MotorController_AbsEC
main.cpp@6:e6b303ea6439, 2021-07-19 (annotated)
- Committer:
- ayu13
- Date:
- Mon Jul 19 13:20:31 2021 +0000
- Revision:
- 6:e6b303ea6439
- Parent:
- 5:4fba3e91741f
- Child:
- 7:038ea29c5a97
Deg_Controll_OK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ayu13 | 0:83b2c6a67cce | 1 | ////c = center arm |
ayu13 | 0:83b2c6a67cce | 2 | #include "mbed.h" |
ayu13 | 0:83b2c6a67cce | 3 | #include "AMT21.h" |
ayu13 | 0:83b2c6a67cce | 4 | #include "CalPID.h" |
ayu13 | 0:83b2c6a67cce | 5 | #include "KondoServo.h" |
ayu13 | 0:83b2c6a67cce | 6 | #include "MotorController.h" |
ayu13 | 0:83b2c6a67cce | 7 | #include <stdlib.h> |
ayu13 | 0:83b2c6a67cce | 8 | #include <ros.h> |
ayu13 | 0:83b2c6a67cce | 9 | #include <std_msgs/String.h> |
ayu13 | 0:83b2c6a67cce | 10 | #include <geometry_msgs/Point.h> |
ayu13 | 0:83b2c6a67cce | 11 | |
ayu13 | 0:83b2c6a67cce | 12 | ///////////////// 宣言部分 ////////////////////// |
ayu13 | 0:83b2c6a67cce | 13 | #ifndef M_PI |
ayu13 | 0:83b2c6a67cce | 14 | #define M_PI 3.14159265359f |
ayu13 | 0:83b2c6a67cce | 15 | #endif |
ayu13 | 1:c0a3e4589a8f | 16 | #define DELTA_T 0.01 //制御周期 |
ayu13 | 1:c0a3e4589a8f | 17 | #define DUTY_MAX 0.07 //duty比の最大値 |
ayu13 | 6:e6b303ea6439 | 18 | #define OMEGA_MAX 0.25 //速度制御を利用した角度制御での角速度の最大値 |
ayu13 | 0:83b2c6a67cce | 19 | #define NUM_DATA 270 |
ayu13 | 0:83b2c6a67cce | 20 | |
ayu13 | 5:4fba3e91741f | 21 | int state = 0; |
ayu13 | 5:4fba3e91741f | 22 | |
ayu13 | 0:83b2c6a67cce | 23 | ////角度制御(アブソリュートエンコーダ)///// |
ayu13 | 0:83b2c6a67cce | 24 | DigitalOut myled1(LED1); |
ayu13 | 0:83b2c6a67cce | 25 | Timer timer; |
ayu13 | 0:83b2c6a67cce | 26 | CalPID speed_pid(0.08,0,0.0008,DELTA_T,DUTY_MAX); //速度制御のPD計算 |
ayu13 | 3:78b608ba0286 | 27 | CalPID angle_duty_pid(0,0,0,DELTA_T,DUTY_MAX); //角度制御(duty式)のPD計算 |
ayu13 | 6:e6b303ea6439 | 28 | CalPID angle_omega_pid(5.0,0,0.0032,DELTA_T,OMEGA_MAX);//角度制御(速度式)のPD計算 |
ayu13 | 0:83b2c6a67cce | 29 | Amt21 amt(p9,p10,p8); //Amt21 (PinName tx,PinName rx,PinName mode):serial_(tx,rx),rs485_mode(mode) |
ayu13 | 0:83b2c6a67cce | 30 | MotorController motor(p26,p25,DELTA_T,amt,speed_pid,angle_duty_pid,angle_omega_pid); //MotorController(PinName motor_p_, PinName motor_n_,double dt, Amt21 &ec, CalPID &sc_, CalPID &ac_duty, CalPID &ac_omega) |
ayu13 | 5:4fba3e91741f | 31 | double radToDeg(double x); |
ayu13 | 5:4fba3e91741f | 32 | double degToRad(double x); |
ayu13 | 0:83b2c6a67cce | 33 | double target_speed=0; |
ayu13 | 0:83b2c6a67cce | 34 | double target_rad=0; |
ayu13 | 0:83b2c6a67cce | 35 | double target_deg = 0; |
ayu13 | 5:4fba3e91741f | 36 | double right_deg = -90; |
ayu13 | 5:4fba3e91741f | 37 | double left_deg = 90; |
ayu13 | 0:83b2c6a67cce | 38 | |
ayu13 | 0:83b2c6a67cce | 39 | ////先端アーム(サーボ)///// |
ayu13 | 0:83b2c6a67cce | 40 | DigitalIn ts_c1(p6, PullUp); |
ayu13 | 0:83b2c6a67cce | 41 | DigitalIn ts_c2(p7, PullUp); |
ayu13 | 0:83b2c6a67cce | 42 | KondoServo servo_c(p13, p14); |
ayu13 | 0:83b2c6a67cce | 43 | double servoDeg(double servo_value); |
ayu13 | 5:4fba3e91741f | 44 | void touchGrab(); |
ayu13 | 0:83b2c6a67cce | 45 | int id_c1 = 0; |
ayu13 | 6:e6b303ea6439 | 46 | float deg_c_grab = 10600; |
ayu13 | 6:e6b303ea6439 | 47 | float deg_c_open = 8300; |
ayu13 | 4:3abefa13c2e3 | 48 | int hand = 0; //タッチセンサがONになった状態:1,OFF:0 |
ayu13 | 0:83b2c6a67cce | 49 | |
ayu13 | 0:83b2c6a67cce | 50 | ////データ記録まわり///// |
ayu13 | 0:83b2c6a67cce | 51 | int angle_count=0; |
ayu13 | 0:83b2c6a67cce | 52 | int omega_count=0; |
ayu13 | 4:3abefa13c2e3 | 53 | double now_deg=0; |
ayu13 | 0:83b2c6a67cce | 54 | double now_count=0; |
ayu13 | 0:83b2c6a67cce | 55 | |
ayu13 | 2:b728a6564520 | 56 | ///////////////// ROS ///////////////// |
ayu13 | 2:b728a6564520 | 57 | DigitalOut myled2(LED2); |
ayu13 | 2:b728a6564520 | 58 | DigitalOut myled3(LED3); |
ayu13 | 2:b728a6564520 | 59 | DigitalOut myled4(LED4); |
ayu13 | 2:b728a6564520 | 60 | float arm_num=0,arm_deg=0,arm_rad=0; |
ayu13 | 2:b728a6564520 | 61 | void cArmCallback(const geometry_msgs::Point &arm_state) |
ayu13 | 2:b728a6564520 | 62 | { |
ayu13 | 2:b728a6564520 | 63 | arm_num = arm_state.x; //右回転=1、左回転=2 |
ayu13 | 2:b728a6564520 | 64 | arm_deg = arm_state.y; //目標角度 |
ayu13 | 2:b728a6564520 | 65 | } |
ayu13 | 2:b728a6564520 | 66 | ros::NodeHandle nh; |
ayu13 | 2:b728a6564520 | 67 | ros::Subscriber<geometry_msgs::Point> sub("/arm",&cArmCallback); |
ayu13 | 2:b728a6564520 | 68 | |
ayu13 | 6:e6b303ea6439 | 69 | DigitalIn toggle1(p16, PullUp); //right |
ayu13 | 6:e6b303ea6439 | 70 | DigitalIn toggle2(p20, PullUp); //left |
ayu13 | 6:e6b303ea6439 | 71 | |
ayu13 | 0:83b2c6a67cce | 72 | int main() |
ayu13 | 0:83b2c6a67cce | 73 | { |
ayu13 | 5:4fba3e91741f | 74 | myled1 = 0; //目標角度以上離れている:点灯 |
ayu13 | 5:4fba3e91741f | 75 | myled2 = 0; //通信で欲しい値が送られているとき:点灯 |
ayu13 | 5:4fba3e91741f | 76 | myled3 = 0; // |
ayu13 | 5:4fba3e91741f | 77 | myled4 = 0; //サーボを掴む→点灯.離す→消灯 |
ayu13 | 5:4fba3e91741f | 78 | |
ayu13 | 0:83b2c6a67cce | 79 | ///////// ROS ///////// |
ayu13 | 1:c0a3e4589a8f | 80 | nh.getHardware()->setBaud(921600); |
ayu13 | 0:83b2c6a67cce | 81 | nh.initNode(); |
ayu13 | 0:83b2c6a67cce | 82 | nh.subscribe(sub); |
ayu13 | 0:83b2c6a67cce | 83 | |
ayu13 | 0:83b2c6a67cce | 84 | timer.reset(); |
ayu13 | 0:83b2c6a67cce | 85 | timer.start(); |
ayu13 | 0:83b2c6a67cce | 86 | |
ayu13 | 0:83b2c6a67cce | 87 | motor.setEquation(0.0523,0.0148,0.0459,-0.0341); //Excel"omega_controll"の図より |
ayu13 | 0:83b2c6a67cce | 88 | |
ayu13 | 3:78b608ba0286 | 89 | // target_rad = -(M_PI/6); //目標角度をπ/6(rad)に(単位に注意) ////////////////////////// |
ayu13 | 3:78b608ba0286 | 90 | // target_deg = radToDeg(target_rad); |
ayu13 | 0:83b2c6a67cce | 91 | |
ayu13 | 0:83b2c6a67cce | 92 | while (1) { |
ayu13 | 0:83b2c6a67cce | 93 | //// ROS //// |
ayu13 | 0:83b2c6a67cce | 94 | nh.spinOnce(); |
ayu13 | 4:3abefa13c2e3 | 95 | |
ayu13 | 5:4fba3e91741f | 96 | if(timer.read()>DELTA_T) { |
ayu13 | 5:4fba3e91741f | 97 | if(arm_num==1&&arm_deg==30) { //動作確認用 |
ayu13 | 5:4fba3e91741f | 98 | myled2 = 1; |
ayu13 | 0:83b2c6a67cce | 99 | } |
ayu13 | 5:4fba3e91741f | 100 | arm_rad = degToRad(arm_deg); |
ayu13 | 6:e6b303ea6439 | 101 | // target_rad = -arm_rad; |
ayu13 | 6:e6b303ea6439 | 102 | // target_deg = -arm_deg; |
ayu13 | 6:e6b303ea6439 | 103 | arm_deg = -15; |
ayu13 | 5:4fba3e91741f | 104 | arm_rad = degToRad(arm_deg); |
ayu13 | 0:83b2c6a67cce | 105 | |
ayu13 | 5:4fba3e91741f | 106 | if(state == 0) { |
ayu13 | 6:e6b303ea6439 | 107 | ///トグルスイッチ1////1 |
ayu13 | 6:e6b303ea6439 | 108 | if(toggle1 == 0) { //トグルスイッチ1(mbed側)ONで初start |
ayu13 | 6:e6b303ea6439 | 109 | wait_us(50); |
ayu13 | 6:e6b303ea6439 | 110 | if(toggle1 == 0) { |
ayu13 | 6:e6b303ea6439 | 111 | state ++; |
ayu13 | 6:e6b303ea6439 | 112 | } |
ayu13 | 6:e6b303ea6439 | 113 | } |
ayu13 | 6:e6b303ea6439 | 114 | } |
ayu13 | 6:e6b303ea6439 | 115 | if(state == 1) { |
ayu13 | 6:e6b303ea6439 | 116 | target_deg = -90; |
ayu13 | 5:4fba3e91741f | 117 | target_rad = degToRad(target_deg); |
ayu13 | 6:e6b303ea6439 | 118 | servo_c.set_degree(0,servoDeg(deg_c_open)); //servo_c離す |
ayu13 | 5:4fba3e91741f | 119 | state++; |
ayu13 | 6:e6b303ea6439 | 120 | } else if (state == 2) { |
ayu13 | 5:4fba3e91741f | 121 | myled3 = 1; |
ayu13 | 5:4fba3e91741f | 122 | if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す |
ayu13 | 5:4fba3e91741f | 123 | myled1 = 1; //LED1 点灯 |
ayu13 | 5:4fba3e91741f | 124 | motor.AcOmega(target_rad); //速度式角度制御 |
ayu13 | 5:4fba3e91741f | 125 | } else { //目標角度に達したら停止 |
ayu13 | 5:4fba3e91741f | 126 | myled1=0; //LED1 消灯 |
ayu13 | 5:4fba3e91741f | 127 | motor.stop(); |
ayu13 | 5:4fba3e91741f | 128 | } |
ayu13 | 5:4fba3e91741f | 129 | touchGrab(); //タッチセンサ |
ayu13 | 6:e6b303ea6439 | 130 | } else if (state == 3) { |
ayu13 | 5:4fba3e91741f | 131 | myled3 = 0; |
ayu13 | 5:4fba3e91741f | 132 | target_rad = arm_rad; |
ayu13 | 5:4fba3e91741f | 133 | target_deg = arm_deg; |
ayu13 | 5:4fba3e91741f | 134 | state++; |
ayu13 | 6:e6b303ea6439 | 135 | } else if (state == 4) { |
ayu13 | 5:4fba3e91741f | 136 | if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す |
ayu13 | 5:4fba3e91741f | 137 | myled1 = 1; //LED1 点灯 |
ayu13 | 3:78b608ba0286 | 138 | motor.AcOmega(target_rad); //速度式角度制御 |
ayu13 | 5:4fba3e91741f | 139 | } else { //目標角度に達したら停止 |
ayu13 | 5:4fba3e91741f | 140 | myled1=0; //LED1 消灯 |
ayu13 | 5:4fba3e91741f | 141 | motor.stop(); |
ayu13 | 5:4fba3e91741f | 142 | //wait(100); //table停止用時間 |
ayu13 | 5:4fba3e91741f | 143 | servo_c.set_degree(0,servoDeg(deg_c_open)); //servo_c離す |
ayu13 | 5:4fba3e91741f | 144 | myled4 = 0; |
ayu13 | 6:e6b303ea6439 | 145 | // state = 0; |
ayu13 | 3:78b608ba0286 | 146 | } |
ayu13 | 0:83b2c6a67cce | 147 | } |
ayu13 | 5:4fba3e91741f | 148 | |
ayu13 | 5:4fba3e91741f | 149 | ////タッチセンサ//// |
ayu13 | 5:4fba3e91741f | 150 | // touchGrab(); |
ayu13 | 5:4fba3e91741f | 151 | |
ayu13 | 5:4fba3e91741f | 152 | ////アブソリュートエンコーダ//// |
ayu13 | 5:4fba3e91741f | 153 | |
ayu13 | 5:4fba3e91741f | 154 | // if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す |
ayu13 | 5:4fba3e91741f | 155 | // myled1 = 1; //LED1 点灯 |
ayu13 | 5:4fba3e91741f | 156 | //// if(hand == 1) { //タッチセンサが反応していたとき(ハンドルを掴んでいたとき) |
ayu13 | 5:4fba3e91741f | 157 | // motor.AcOmega(target_rad); //速度式角度制御 |
ayu13 | 5:4fba3e91741f | 158 | //// } else if(hand == 0) { |
ayu13 | 5:4fba3e91741f | 159 | //// myled4=0; //LED4 消灯 |
ayu13 | 5:4fba3e91741f | 160 | //// } |
ayu13 | 5:4fba3e91741f | 161 | // } else if(fabs(now_deg-target_deg) < 0.5 && state == 3) { //目標角度に達したら停止 |
ayu13 | 5:4fba3e91741f | 162 | // myled1=0; //LED1 消灯 |
ayu13 | 5:4fba3e91741f | 163 | // motor.stop(); |
ayu13 | 5:4fba3e91741f | 164 | // hand = 0; |
ayu13 | 5:4fba3e91741f | 165 | // //wait(100); //table停止用時間 |
ayu13 | 5:4fba3e91741f | 166 | // servo_c.set_degree(0,servoDeg(deg_c_open)); //servo_c離す |
ayu13 | 5:4fba3e91741f | 167 | // } |
ayu13 | 0:83b2c6a67cce | 168 | amt.rewriteCount(); |
ayu13 | 0:83b2c6a67cce | 169 | amt.calOmega(); |
ayu13 | 0:83b2c6a67cce | 170 | amt.getOmega(); |
ayu13 | 4:3abefa13c2e3 | 171 | now_deg = amt.getDeg(); |
ayu13 | 0:83b2c6a67cce | 172 | now_count=amt.getCount(); |
ayu13 | 4:3abefa13c2e3 | 173 | // motor.AcOmega(target_rad); |
ayu13 | 0:83b2c6a67cce | 174 | |
ayu13 | 0:83b2c6a67cce | 175 | timer.reset(); |
ayu13 | 0:83b2c6a67cce | 176 | } |
ayu13 | 0:83b2c6a67cce | 177 | wait_us(10); |
ayu13 | 0:83b2c6a67cce | 178 | } |
ayu13 | 0:83b2c6a67cce | 179 | } |
ayu13 | 0:83b2c6a67cce | 180 | |
ayu13 | 0:83b2c6a67cce | 181 | ///////////////// 関数宣言部分 ////////////////////// |
ayu13 | 0:83b2c6a67cce | 182 | double servoDeg(double servo_value) |
ayu13 | 0:83b2c6a67cce | 183 | { |
ayu13 | 0:83b2c6a67cce | 184 | return (servo_value - 3500)*270/(11500 - 3500); |
ayu13 | 0:83b2c6a67cce | 185 | } |
ayu13 | 0:83b2c6a67cce | 186 | double radToDeg(double x) |
ayu13 | 0:83b2c6a67cce | 187 | { |
ayu13 | 0:83b2c6a67cce | 188 | return x*180/M_PI; |
ayu13 | 2:b728a6564520 | 189 | } |
ayu13 | 2:b728a6564520 | 190 | double degToRad(double x) |
ayu13 | 2:b728a6564520 | 191 | { |
ayu13 | 2:b728a6564520 | 192 | return x*M_PI/180; |
ayu13 | 5:4fba3e91741f | 193 | } |
ayu13 | 5:4fba3e91741f | 194 | void touchGrab() |
ayu13 | 5:4fba3e91741f | 195 | { |
ayu13 | 5:4fba3e91741f | 196 | if(ts_c1 == 0) { //タッチセンサ反応したらハンドルを掴む |
ayu13 | 5:4fba3e91741f | 197 | wait_us(50); |
ayu13 | 5:4fba3e91741f | 198 | if(ts_c1 == 0) { |
ayu13 | 5:4fba3e91741f | 199 | servo_c.set_degree(0,servoDeg(deg_c_grab)); //servo_c掴む |
ayu13 | 5:4fba3e91741f | 200 | state++; |
ayu13 | 5:4fba3e91741f | 201 | // hand = 1; |
ayu13 | 5:4fba3e91741f | 202 | myled4=1; //LED4 点灯 |
ayu13 | 5:4fba3e91741f | 203 | } |
ayu13 | 5:4fba3e91741f | 204 | } |
ayu13 | 0:83b2c6a67cce | 205 | } |