ライブラリ化を行った後
Dependencies: QEI accelerator bit_test cyclic_io cyclic_var cylinder event_var limit mbed mecanum motor_drive pid pid_encoder rs422_put sbdbt servo
Fork of 17robo_Practice1 by
main.cpp@25:124aee8a7742, 2017-08-07 (annotated)
- Committer:
- echo_piyo
- Date:
- Mon Aug 07 08:48:15 2017 +0000
- Revision:
- 25:124aee8a7742
- Parent:
- 24:da0fc94add43
- Child:
- 26:3280d0300b04
(?)????????????????; ??????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
echo_piyo | 0:bf96e953cdb8 | 1 | #include "mbed.h" |
echo_piyo | 4:a6cc2f03e69b | 2 | #include "math.h" |
echo_piyo | 0:bf96e953cdb8 | 3 | #include "bit_test.h" |
echo_piyo | 0:bf96e953cdb8 | 4 | #include "RS422_put.h" |
echo_piyo | 0:bf96e953cdb8 | 5 | #include "sbdbt.h" |
echo_piyo | 0:bf96e953cdb8 | 6 | #include "mecanum.h" |
echo_piyo | 0:bf96e953cdb8 | 7 | #include "bno055_lib.h" |
echo_piyo | 0:bf96e953cdb8 | 8 | #include "bno055_use.h" |
echo_piyo | 0:bf96e953cdb8 | 9 | #include "pid.h" |
echo_piyo | 0:bf96e953cdb8 | 10 | #include "limit.h" |
echo_piyo | 4:a6cc2f03e69b | 11 | #include "accelerator.h" |
echo_piyo | 10:04f2a82cfd89 | 12 | #include "encorder.h" |
echo_piyo | 14:aac2f18f6779 | 13 | #include "cyclic.h" |
echo_piyo | 14:aac2f18f6779 | 14 | #include "cyclic_IO.h" |
echo_piyo | 15:0fdf483769bf | 15 | #include "cylinder.h" |
echo_piyo | 13:dfae731e239f | 16 | //#include "QEI.h" |
echo_piyo | 0:bf96e953cdb8 | 17 | |
echo_piyo | 0:bf96e953cdb8 | 18 | #define pc_baud 460800 |
echo_piyo | 0:bf96e953cdb8 | 19 | #define sbdbt_tx p13 |
echo_piyo | 0:bf96e953cdb8 | 20 | #define sbdbt_rx p14 |
echo_piyo | 0:bf96e953cdb8 | 21 | #define sbdbt_baud 115200 |
echo_piyo | 0:bf96e953cdb8 | 22 | #define rs422_tx p28 |
echo_piyo | 0:bf96e953cdb8 | 23 | #define rs422_rx p27 |
echo_piyo | 0:bf96e953cdb8 | 24 | #define rs422_baud 115200 |
echo_piyo | 3:9ef929639c4a | 25 | #define output_period 0.015 |
echo_piyo | 23:aeff5fd36ae1 | 26 | #define nucleo_num 5 |
echo_piyo | 0:bf96e953cdb8 | 27 | #define pi 3.141592 |
echo_piyo | 0:bf96e953cdb8 | 28 | #define n1_id 3 |
echo_piyo | 0:bf96e953cdb8 | 29 | #define n2_id 4 |
echo_piyo | 10:04f2a82cfd89 | 30 | #define n3_id 5 |
echo_piyo | 14:aac2f18f6779 | 31 | #define n4_id 6 |
echo_piyo | 14:aac2f18f6779 | 32 | #define n5_id 7 |
echo_piyo | 5:6efda58ff71b | 33 | #define yaw_Kp 0.01 |
echo_piyo | 5:6efda58ff71b | 34 | #define yaw_Ki 0.01 |
echo_piyo | 5:6efda58ff71b | 35 | #define yaw_Kd 0.01 |
echo_piyo | 10:04f2a82cfd89 | 36 | #define acceleration 25 |
echo_piyo | 10:04f2a82cfd89 | 37 | #define pin_cylinder_on p17 |
echo_piyo | 10:04f2a82cfd89 | 38 | #define pin_cylinder_off p18 |
echo_piyo | 13:dfae731e239f | 39 | #define pin_interrupt p23 |
echo_piyo | 12:1fec80ae8a2c | 40 | #define encoder_A p25 |
echo_piyo | 12:1fec80ae8a2c | 41 | #define encoder_B p26 |
echo_piyo | 10:04f2a82cfd89 | 42 | #define enc_Kp 0.01 |
echo_piyo | 10:04f2a82cfd89 | 43 | #define enc_Ki 0.01 |
echo_piyo | 10:04f2a82cfd89 | 44 | #define enc_Kd 0.01 |
echo_piyo | 24:da0fc94add43 | 45 | #define powerdown 0.6 |
echo_piyo | 0:bf96e953cdb8 | 46 | |
echo_piyo | 13:dfae731e239f | 47 | DigitalOut led1(LED1); |
echo_piyo | 15:0fdf483769bf | 48 | //DigitalOut led2(LED2); |
echo_piyo | 15:0fdf483769bf | 49 | DigitalOut led3(LED3); |
echo_piyo | 15:0fdf483769bf | 50 | //DigitalOut led4(LED4); |
echo_piyo | 13:dfae731e239f | 51 | DigitalIn interrupt(pin_interrupt); |
echo_piyo | 0:bf96e953cdb8 | 52 | Serial pc(USBTX,USBRX); |
echo_piyo | 0:bf96e953cdb8 | 53 | RS422 rs422(rs422_tx, rs422_rx); |
echo_piyo | 0:bf96e953cdb8 | 54 | Sbdbt sbdbt(sbdbt_tx, sbdbt_rx); |
echo_piyo | 0:bf96e953cdb8 | 55 | Ticker output_timer; |
echo_piyo | 0:bf96e953cdb8 | 56 | Mecanum mecanum; |
echo_piyo | 0:bf96e953cdb8 | 57 | Bno055 bno055; |
echo_piyo | 2:d5b8f8e62923 | 58 | Position_pid yaw_pid; |
echo_piyo | 4:a6cc2f03e69b | 59 | Accel v1; |
echo_piyo | 4:a6cc2f03e69b | 60 | Accel v2; |
echo_piyo | 4:a6cc2f03e69b | 61 | Accel v3; |
echo_piyo | 4:a6cc2f03e69b | 62 | Accel v4; |
echo_piyo | 15:0fdf483769bf | 63 | Cylinder cylinder(pin_cylinder_on,pin_cylinder_off); |
echo_piyo | 17:5c4718c603dc | 64 | Cyclic sword_left; |
echo_piyo | 20:347daec6c9a3 | 65 | Cyclic sword_right; |
echo_piyo | 12:1fec80ae8a2c | 66 | |
echo_piyo | 12:1fec80ae8a2c | 67 | //追加点 |
echo_piyo | 13:dfae731e239f | 68 | Encoder enc_cylinder(encoder_A,encoder_B); |
echo_piyo | 13:dfae731e239f | 69 | //QEI wheel(encoder_A, encoder_B, NC, 624); |
echo_piyo | 9:6486f4b3ac50 | 70 | |
echo_piyo | 0:bf96e953cdb8 | 71 | void setup(); |
echo_piyo | 0:bf96e953cdb8 | 72 | void output(); |
echo_piyo | 15:0fdf483769bf | 73 | void motor_cal(); |
echo_piyo | 16:e49df474e4c6 | 74 | void cylinder_cal(); |
echo_piyo | 10:04f2a82cfd89 | 75 | void boost(); |
echo_piyo | 12:1fec80ae8a2c | 76 | void cylinder_origin(); |
echo_piyo | 17:5c4718c603dc | 77 | void sword_left_cal(); |
echo_piyo | 21:6568c3587003 | 78 | void sword_right_cal(); |
echo_piyo | 2:d5b8f8e62923 | 79 | float yaw, target_yaw; |
echo_piyo | 0:bf96e953cdb8 | 80 | |
echo_piyo | 21:6568c3587003 | 81 | |
echo_piyo | 0:bf96e953cdb8 | 82 | int main() |
echo_piyo | 0:bf96e953cdb8 | 83 | { |
echo_piyo | 0:bf96e953cdb8 | 84 | setup(); |
echo_piyo | 2:d5b8f8e62923 | 85 | while(1) { |
echo_piyo | 17:5c4718c603dc | 86 | //pc.printf("Pulses is: %i\r\n",sbdbt.down); |
echo_piyo | 0:bf96e953cdb8 | 87 | } |
echo_piyo | 0:bf96e953cdb8 | 88 | } |
echo_piyo | 0:bf96e953cdb8 | 89 | |
echo_piyo | 0:bf96e953cdb8 | 90 | void setup() |
echo_piyo | 0:bf96e953cdb8 | 91 | { |
echo_piyo | 1:2d878962e6ea | 92 | wait(1); |
echo_piyo | 19:76a387e4bcf6 | 93 | bno055.begin(); |
echo_piyo | 0:bf96e953cdb8 | 94 | wait(1); |
echo_piyo | 19:76a387e4bcf6 | 95 | bno055.firstRead(); |
echo_piyo | 0:bf96e953cdb8 | 96 | pc.baud(pc_baud); |
echo_piyo | 0:bf96e953cdb8 | 97 | sbdbt.begin(sbdbt_baud); |
echo_piyo | 0:bf96e953cdb8 | 98 | rs422.begin(rs422_baud); |
echo_piyo | 19:76a387e4bcf6 | 99 | cylinder_origin(); |
echo_piyo | 0:bf96e953cdb8 | 100 | output_timer.attach(&output, output_period); |
echo_piyo | 2:d5b8f8e62923 | 101 | yaw_pid.setup(yaw_Kp, yaw_Ki, yaw_Kd); |
echo_piyo | 10:04f2a82cfd89 | 102 | mecanum.setupdeg(bno055.getYawRad()+180.0); |
echo_piyo | 4:a6cc2f03e69b | 103 | v1.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 104 | v2.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 105 | v3.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 106 | v4.setup(acceleration,output_period); |
echo_piyo | 12:1fec80ae8a2c | 107 | |
echo_piyo | 12:1fec80ae8a2c | 108 | //追加点 |
echo_piyo | 13:dfae731e239f | 109 | enc_cylinder.setup(1200); |
echo_piyo | 13:dfae731e239f | 110 | enc_cylinder.set_parameter(enc_Kp,enc_Ki,enc_Kd); |
echo_piyo | 12:1fec80ae8a2c | 111 | } |
echo_piyo | 12:1fec80ae8a2c | 112 | |
echo_piyo | 15:0fdf483769bf | 113 | void output() |
echo_piyo | 15:0fdf483769bf | 114 | { |
echo_piyo | 15:0fdf483769bf | 115 | motor_cal(); |
echo_piyo | 16:e49df474e4c6 | 116 | cylinder_cal(); |
echo_piyo | 17:5c4718c603dc | 117 | sword_left_cal(); |
echo_piyo | 21:6568c3587003 | 118 | sword_right_cal(); |
echo_piyo | 18:ae4d97945b83 | 119 | //boost(); |
echo_piyo | 15:0fdf483769bf | 120 | |
echo_piyo | 15:0fdf483769bf | 121 | static int counter; |
echo_piyo | 23:aeff5fd36ae1 | 122 | int id[nucleo_num] = {n1_id, n2_id, n3_id, n4_id, n5_id}; |
echo_piyo | 15:0fdf483769bf | 123 | |
echo_piyo | 15:0fdf483769bf | 124 | switch (counter) { |
echo_piyo | 15:0fdf483769bf | 125 | case 0: |
echo_piyo | 24:da0fc94add43 | 126 | rs422.put(id[counter], v1.duty(mecanum.v1()*powerdown), v3.duty(mecanum.v3()*powerdown)); |
echo_piyo | 15:0fdf483769bf | 127 | counter++; |
echo_piyo | 15:0fdf483769bf | 128 | break; |
echo_piyo | 15:0fdf483769bf | 129 | case 1: |
echo_piyo | 24:da0fc94add43 | 130 | rs422.put(id[counter], v2.duty(mecanum.v2()*powerdown), v4.duty(mecanum.v4()*powerdown)); |
echo_piyo | 15:0fdf483769bf | 131 | counter ++; |
echo_piyo | 15:0fdf483769bf | 132 | break; |
echo_piyo | 15:0fdf483769bf | 133 | case 2: |
echo_piyo | 19:76a387e4bcf6 | 134 | rs422.put(id[counter], sbdbt.right_y, 0.0); |
echo_piyo | 17:5c4718c603dc | 135 | counter ++; |
echo_piyo | 17:5c4718c603dc | 136 | break; |
echo_piyo | 17:5c4718c603dc | 137 | case 3: |
echo_piyo | 24:da0fc94add43 | 138 | rs422.put(id[counter], (-sbdbt.sankaku*0.8+sbdbt.batu*0.8), (sbdbt.up*0.8-sbdbt.down*0.8)); |
echo_piyo | 20:347daec6c9a3 | 139 | counter ++; |
echo_piyo | 20:347daec6c9a3 | 140 | break; |
echo_piyo | 20:347daec6c9a3 | 141 | case 4: |
echo_piyo | 24:da0fc94add43 | 142 | rs422.put(id[counter], ((float)sword_right.getState()*0.8),((float)sword_left.getState()*0.8)); |
echo_piyo | 15:0fdf483769bf | 143 | counter = 0; |
echo_piyo | 15:0fdf483769bf | 144 | break; |
echo_piyo | 15:0fdf483769bf | 145 | default: |
echo_piyo | 15:0fdf483769bf | 146 | break; |
echo_piyo | 15:0fdf483769bf | 147 | }; |
echo_piyo | 15:0fdf483769bf | 148 | } |
echo_piyo | 15:0fdf483769bf | 149 | |
echo_piyo | 17:5c4718c603dc | 150 | void sword_left_cal(){ |
echo_piyo | 20:347daec6c9a3 | 151 | sword_left.cyclic(sbdbt.left); |
echo_piyo | 20:347daec6c9a3 | 152 | } |
echo_piyo | 25:124aee8a7742 | 153 | |
echo_piyo | 20:347daec6c9a3 | 154 | void sword_right_cal(){ |
echo_piyo | 20:347daec6c9a3 | 155 | sword_right.cyclic(sbdbt.maru); |
echo_piyo | 17:5c4718c603dc | 156 | } |
echo_piyo | 15:0fdf483769bf | 157 | |
echo_piyo | 12:1fec80ae8a2c | 158 | void cylinder_origin(){ |
echo_piyo | 12:1fec80ae8a2c | 159 | while(interrupt){ |
echo_piyo | 14:aac2f18f6779 | 160 | led1 = 1; |
echo_piyo | 12:1fec80ae8a2c | 161 | rs422.put(5, -0.8, 0.0); |
echo_piyo | 12:1fec80ae8a2c | 162 | } |
echo_piyo | 14:aac2f18f6779 | 163 | led1 = 0; |
echo_piyo | 0:bf96e953cdb8 | 164 | } |
echo_piyo | 0:bf96e953cdb8 | 165 | |
echo_piyo | 19:76a387e4bcf6 | 166 | void cylinder_cal() |
echo_piyo | 19:76a387e4bcf6 | 167 | { |
echo_piyo | 20:347daec6c9a3 | 168 | cylinder.cyclic(sbdbt.right); |
echo_piyo | 19:76a387e4bcf6 | 169 | } |
echo_piyo | 19:76a387e4bcf6 | 170 | |
echo_piyo | 10:04f2a82cfd89 | 171 | void boost(){ |
echo_piyo | 10:04f2a82cfd89 | 172 | if(sbdbt.sankaku) { |
echo_piyo | 10:04f2a82cfd89 | 173 | mecanum.boost_forward(); |
echo_piyo | 10:04f2a82cfd89 | 174 | } |
echo_piyo | 10:04f2a82cfd89 | 175 | if(sbdbt.batu) { |
echo_piyo | 10:04f2a82cfd89 | 176 | mecanum.boost_back(); |
echo_piyo | 10:04f2a82cfd89 | 177 | } |
echo_piyo | 10:04f2a82cfd89 | 178 | if(sbdbt.shikaku) { |
echo_piyo | 10:04f2a82cfd89 | 179 | mecanum.boost_left(); |
echo_piyo | 10:04f2a82cfd89 | 180 | } |
echo_piyo | 10:04f2a82cfd89 | 181 | if(sbdbt.maru) { |
echo_piyo | 10:04f2a82cfd89 | 182 | mecanum.boost_right(); |
echo_piyo | 10:04f2a82cfd89 | 183 | } |
echo_piyo | 10:04f2a82cfd89 | 184 | } |
echo_piyo | 10:04f2a82cfd89 | 185 | |
echo_piyo | 15:0fdf483769bf | 186 | void motor_cal() |
echo_piyo | 5:6efda58ff71b | 187 | { |
echo_piyo | 4:a6cc2f03e69b | 188 | yaw = bno055.getYawRad(); |
echo_piyo | 4:a6cc2f03e69b | 189 | target_yaw = yaw; |
echo_piyo | 4:a6cc2f03e69b | 190 | yaw_pid.cal(target_yaw, yaw, output_period); |
echo_piyo | 4:a6cc2f03e69b | 191 | mecanum.sbdbt_cal(sbdbt.left_x, sbdbt.left_y, sbdbt.l1, sbdbt.r1, yaw_pid.duty(), bno055.getYawRad()); |
echo_piyo | 5:6efda58ff71b | 192 | // pc.printf("%f\t data %f\t %f\t %f\t %f\t\r\n", bno055.getYawRad(), sbdbt.left_x, sbdbt.left_y, mecanum.VX(), mecanum.VY()); |
echo_piyo | 14:aac2f18f6779 | 193 | } |