DRのPS3での操作用(アームの開閉をコントローラで制御可能)

Dependencies:   mbed CalPID motorout ros_lib_melodic MotorController_AbsEC

Committer:
ayu13
Date:
Sat Jul 10 09:19:49 2021 +0000
Revision:
5:4fba3e91741f
Parent:
4:3abefa13c2e3
Child:
6:e6b303ea6439
LED_Operation check OK

Who changed what in which revision?

UserRevisionLine numberNew 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 1:c0a3e4589a8f 18 #define OMEGA_MAX 0.6 //速度制御を利用した角度制御での角速度の最大値
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 0:83b2c6a67cce 28 CalPID angle_omega_pid(5.5,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 1:c0a3e4589a8f 46 float deg_c_grab = 10000;
ayu13 1:c0a3e4589a8f 47 float deg_c_open = 7500;
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 0:83b2c6a67cce 69 int main()
ayu13 0:83b2c6a67cce 70 {
ayu13 5:4fba3e91741f 71 myled1 = 0; //目標角度以上離れている:点灯
ayu13 5:4fba3e91741f 72 myled2 = 0; //通信で欲しい値が送られているとき:点灯
ayu13 5:4fba3e91741f 73 myled3 = 0; //
ayu13 5:4fba3e91741f 74 myled4 = 0; //サーボを掴む→点灯.離す→消灯
ayu13 5:4fba3e91741f 75
ayu13 0:83b2c6a67cce 76 ///////// ROS /////////
ayu13 1:c0a3e4589a8f 77 nh.getHardware()->setBaud(921600);
ayu13 0:83b2c6a67cce 78 nh.initNode();
ayu13 0:83b2c6a67cce 79 nh.subscribe(sub);
ayu13 0:83b2c6a67cce 80
ayu13 0:83b2c6a67cce 81 timer.reset();
ayu13 0:83b2c6a67cce 82 timer.start();
ayu13 0:83b2c6a67cce 83
ayu13 0:83b2c6a67cce 84 motor.setEquation(0.0523,0.0148,0.0459,-0.0341); //Excel"omega_controll"の図より
ayu13 0:83b2c6a67cce 85
ayu13 3:78b608ba0286 86 // target_rad = -(M_PI/6); //目標角度をπ/6(rad)に(単位に注意) //////////////////////////
ayu13 3:78b608ba0286 87 // target_deg = radToDeg(target_rad);
ayu13 0:83b2c6a67cce 88
ayu13 0:83b2c6a67cce 89 while (1) {
ayu13 0:83b2c6a67cce 90 //// ROS ////
ayu13 0:83b2c6a67cce 91 nh.spinOnce();
ayu13 4:3abefa13c2e3 92
ayu13 5:4fba3e91741f 93 if(timer.read()>DELTA_T) {
ayu13 5:4fba3e91741f 94 if(arm_num==1&&arm_deg==30) { //動作確認用
ayu13 5:4fba3e91741f 95 myled2 = 1;
ayu13 0:83b2c6a67cce 96 }
ayu13 5:4fba3e91741f 97 arm_rad = degToRad(arm_deg);
ayu13 5:4fba3e91741f 98 // target_rad = -arm_rad;
ayu13 5:4fba3e91741f 99 // target_deg = -arm_deg;
ayu13 5:4fba3e91741f 100 arm_deg = -30;
ayu13 5:4fba3e91741f 101 arm_rad = degToRad(arm_deg);
ayu13 0:83b2c6a67cce 102
ayu13 5:4fba3e91741f 103 if(state == 0) {
ayu13 5:4fba3e91741f 104 target_deg = -60;
ayu13 5:4fba3e91741f 105 target_rad = degToRad(target_deg);
ayu13 5:4fba3e91741f 106 state++;
ayu13 5:4fba3e91741f 107 } else if (state == 1) {
ayu13 5:4fba3e91741f 108 myled3 = 1;
ayu13 5:4fba3e91741f 109 if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す
ayu13 5:4fba3e91741f 110 myled1 = 1; //LED1 点灯
ayu13 5:4fba3e91741f 111 motor.AcOmega(target_rad); //速度式角度制御
ayu13 5:4fba3e91741f 112 } else { //目標角度に達したら停止
ayu13 5:4fba3e91741f 113 myled1=0; //LED1 消灯
ayu13 5:4fba3e91741f 114 motor.stop();
ayu13 5:4fba3e91741f 115 }
ayu13 5:4fba3e91741f 116 touchGrab(); //タッチセンサ
ayu13 5:4fba3e91741f 117 } else if (state == 2) {
ayu13 5:4fba3e91741f 118 myled3 = 0;
ayu13 5:4fba3e91741f 119 target_rad = arm_rad;
ayu13 5:4fba3e91741f 120 target_deg = arm_deg;
ayu13 5:4fba3e91741f 121 state++;
ayu13 5:4fba3e91741f 122 } else if (state == 3) {
ayu13 5:4fba3e91741f 123 if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す
ayu13 5:4fba3e91741f 124 myled1 = 1; //LED1 点灯
ayu13 3:78b608ba0286 125 motor.AcOmega(target_rad); //速度式角度制御
ayu13 5:4fba3e91741f 126 } else { //目標角度に達したら停止
ayu13 5:4fba3e91741f 127 myled1=0; //LED1 消灯
ayu13 5:4fba3e91741f 128 motor.stop();
ayu13 5:4fba3e91741f 129 //wait(100); //table停止用時間
ayu13 5:4fba3e91741f 130 servo_c.set_degree(0,servoDeg(deg_c_open)); //servo_c離す
ayu13 5:4fba3e91741f 131 myled4 = 0;
ayu13 3:78b608ba0286 132 }
ayu13 0:83b2c6a67cce 133 }
ayu13 5:4fba3e91741f 134
ayu13 5:4fba3e91741f 135 ////タッチセンサ////
ayu13 5:4fba3e91741f 136 // touchGrab();
ayu13 5:4fba3e91741f 137
ayu13 5:4fba3e91741f 138 ////アブソリュートエンコーダ////
ayu13 5:4fba3e91741f 139
ayu13 5:4fba3e91741f 140 // if(fabs(now_deg-target_deg) > 0.5) { //現在角度と目標角度が0.5°以上離れている間回す
ayu13 5:4fba3e91741f 141 // myled1 = 1; //LED1 点灯
ayu13 5:4fba3e91741f 142 //// if(hand == 1) { //タッチセンサが反応していたとき(ハンドルを掴んでいたとき)
ayu13 5:4fba3e91741f 143 // motor.AcOmega(target_rad); //速度式角度制御
ayu13 5:4fba3e91741f 144 //// } else if(hand == 0) {
ayu13 5:4fba3e91741f 145 //// myled4=0; //LED4 消灯
ayu13 5:4fba3e91741f 146 //// }
ayu13 5:4fba3e91741f 147 // } else if(fabs(now_deg-target_deg) < 0.5 && state == 3) { //目標角度に達したら停止
ayu13 5:4fba3e91741f 148 // myled1=0; //LED1 消灯
ayu13 5:4fba3e91741f 149 // motor.stop();
ayu13 5:4fba3e91741f 150 // hand = 0;
ayu13 5:4fba3e91741f 151 // //wait(100); //table停止用時間
ayu13 5:4fba3e91741f 152 // servo_c.set_degree(0,servoDeg(deg_c_open)); //servo_c離す
ayu13 5:4fba3e91741f 153 // }
ayu13 0:83b2c6a67cce 154 amt.rewriteCount();
ayu13 0:83b2c6a67cce 155 amt.calOmega();
ayu13 0:83b2c6a67cce 156 amt.getOmega();
ayu13 4:3abefa13c2e3 157 now_deg = amt.getDeg();
ayu13 0:83b2c6a67cce 158 now_count=amt.getCount();
ayu13 4:3abefa13c2e3 159 // motor.AcOmega(target_rad);
ayu13 0:83b2c6a67cce 160
ayu13 0:83b2c6a67cce 161 timer.reset();
ayu13 0:83b2c6a67cce 162 }
ayu13 0:83b2c6a67cce 163 wait_us(10);
ayu13 0:83b2c6a67cce 164 }
ayu13 0:83b2c6a67cce 165 }
ayu13 0:83b2c6a67cce 166
ayu13 0:83b2c6a67cce 167 ///////////////// 関数宣言部分 //////////////////////
ayu13 0:83b2c6a67cce 168 double servoDeg(double servo_value)
ayu13 0:83b2c6a67cce 169 {
ayu13 0:83b2c6a67cce 170 return (servo_value - 3500)*270/(11500 - 3500);
ayu13 0:83b2c6a67cce 171 }
ayu13 0:83b2c6a67cce 172 double radToDeg(double x)
ayu13 0:83b2c6a67cce 173 {
ayu13 0:83b2c6a67cce 174 return x*180/M_PI;
ayu13 2:b728a6564520 175 }
ayu13 2:b728a6564520 176 double degToRad(double x)
ayu13 2:b728a6564520 177 {
ayu13 2:b728a6564520 178 return x*M_PI/180;
ayu13 5:4fba3e91741f 179 }
ayu13 5:4fba3e91741f 180 void touchGrab()
ayu13 5:4fba3e91741f 181 {
ayu13 5:4fba3e91741f 182 if(ts_c1 == 0) { //タッチセンサ反応したらハンドルを掴む
ayu13 5:4fba3e91741f 183 wait_us(50);
ayu13 5:4fba3e91741f 184 if(ts_c1 == 0) {
ayu13 5:4fba3e91741f 185 servo_c.set_degree(0,servoDeg(deg_c_grab)); //servo_c掴む
ayu13 5:4fba3e91741f 186 state++;
ayu13 5:4fba3e91741f 187 // hand = 1;
ayu13 5:4fba3e91741f 188 myled4=1; //LED4 点灯
ayu13 5:4fba3e91741f 189 }
ayu13 5:4fba3e91741f 190 }
ayu13 0:83b2c6a67cce 191 }