sampleProgram
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_fuzi by
main.cpp@13:dfae731e239f, 2017-07-17 (annotated)
- Committer:
- echo_piyo
- Date:
- Mon Jul 17 04:55:49 2017 +0000
- Revision:
- 13:dfae731e239f
- Parent:
- 12:1fec80ae8a2c
- Child:
- 14:aac2f18f6779
cyclick.h?????????????????
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 | 13:dfae731e239f | 13 | #include "cyclick.h" |
echo_piyo | 13:dfae731e239f | 14 | //#include "QEI.h" |
echo_piyo | 0:bf96e953cdb8 | 15 | |
echo_piyo | 0:bf96e953cdb8 | 16 | #define pc_baud 460800 |
echo_piyo | 0:bf96e953cdb8 | 17 | #define sbdbt_tx p13 |
echo_piyo | 0:bf96e953cdb8 | 18 | #define sbdbt_rx p14 |
echo_piyo | 0:bf96e953cdb8 | 19 | #define sbdbt_baud 115200 |
echo_piyo | 0:bf96e953cdb8 | 20 | #define rs422_tx p28 |
echo_piyo | 0:bf96e953cdb8 | 21 | #define rs422_rx p27 |
echo_piyo | 0:bf96e953cdb8 | 22 | #define rs422_baud 115200 |
echo_piyo | 3:9ef929639c4a | 23 | #define output_period 0.015 |
echo_piyo | 0:bf96e953cdb8 | 24 | #define nucleo_num 3 |
echo_piyo | 0:bf96e953cdb8 | 25 | #define pi 3.141592 |
echo_piyo | 0:bf96e953cdb8 | 26 | #define n1_id 3 |
echo_piyo | 0:bf96e953cdb8 | 27 | #define n2_id 4 |
echo_piyo | 10:04f2a82cfd89 | 28 | #define n3_id 5 |
echo_piyo | 5:6efda58ff71b | 29 | #define yaw_Kp 0.01 |
echo_piyo | 5:6efda58ff71b | 30 | #define yaw_Ki 0.01 |
echo_piyo | 5:6efda58ff71b | 31 | #define yaw_Kd 0.01 |
echo_piyo | 10:04f2a82cfd89 | 32 | #define acceleration 25 |
echo_piyo | 10:04f2a82cfd89 | 33 | #define pin_cylinder_on p17 |
echo_piyo | 10:04f2a82cfd89 | 34 | #define pin_cylinder_off p18 |
echo_piyo | 13:dfae731e239f | 35 | #define pin_interrupt p23 |
echo_piyo | 12:1fec80ae8a2c | 36 | #define encoder_A p25 |
echo_piyo | 12:1fec80ae8a2c | 37 | #define encoder_B p26 |
echo_piyo | 10:04f2a82cfd89 | 38 | #define enc_Kp 0.01 |
echo_piyo | 10:04f2a82cfd89 | 39 | #define enc_Ki 0.01 |
echo_piyo | 10:04f2a82cfd89 | 40 | #define enc_Kd 0.01 |
echo_piyo | 0:bf96e953cdb8 | 41 | |
echo_piyo | 13:dfae731e239f | 42 | DigitalOut led1(LED1); |
echo_piyo | 13:dfae731e239f | 43 | DigitalIn interrupt(pin_interrupt); |
echo_piyo | 0:bf96e953cdb8 | 44 | Serial pc(USBTX,USBRX); |
echo_piyo | 0:bf96e953cdb8 | 45 | RS422 rs422(rs422_tx, rs422_rx); |
echo_piyo | 0:bf96e953cdb8 | 46 | Sbdbt sbdbt(sbdbt_tx, sbdbt_rx); |
echo_piyo | 0:bf96e953cdb8 | 47 | Ticker output_timer; |
echo_piyo | 0:bf96e953cdb8 | 48 | Mecanum mecanum; |
echo_piyo | 0:bf96e953cdb8 | 49 | Bno055 bno055; |
echo_piyo | 2:d5b8f8e62923 | 50 | Position_pid yaw_pid; |
echo_piyo | 4:a6cc2f03e69b | 51 | Accel v1; |
echo_piyo | 4:a6cc2f03e69b | 52 | Accel v2; |
echo_piyo | 4:a6cc2f03e69b | 53 | Accel v3; |
echo_piyo | 4:a6cc2f03e69b | 54 | Accel v4; |
echo_piyo | 13:dfae731e239f | 55 | Cyclic_IO led2(LED2); |
echo_piyo | 13:dfae731e239f | 56 | Cyclic_IO sylinder_on(pin_cylinder_on); |
echo_piyo | 13:dfae731e239f | 57 | Cyclic_IO sylinder_off(pin_cylinder_off); |
echo_piyo | 12:1fec80ae8a2c | 58 | |
echo_piyo | 12:1fec80ae8a2c | 59 | //追加点 |
echo_piyo | 13:dfae731e239f | 60 | Encoder enc_cylinder(encoder_A,encoder_B); |
echo_piyo | 13:dfae731e239f | 61 | //QEI wheel(encoder_A, encoder_B, NC, 624); |
echo_piyo | 9:6486f4b3ac50 | 62 | |
echo_piyo | 0:bf96e953cdb8 | 63 | void setup(); |
echo_piyo | 0:bf96e953cdb8 | 64 | void output(); |
echo_piyo | 0:bf96e953cdb8 | 65 | void put_output(); |
echo_piyo | 10:04f2a82cfd89 | 66 | void cylinder_check(); |
echo_piyo | 10:04f2a82cfd89 | 67 | void boost(); |
echo_piyo | 12:1fec80ae8a2c | 68 | void cylinder_origin(); |
echo_piyo | 2:d5b8f8e62923 | 69 | float yaw, target_yaw; |
echo_piyo | 0:bf96e953cdb8 | 70 | |
echo_piyo | 0:bf96e953cdb8 | 71 | int main() |
echo_piyo | 0:bf96e953cdb8 | 72 | { |
echo_piyo | 0:bf96e953cdb8 | 73 | setup(); |
echo_piyo | 2:d5b8f8e62923 | 74 | while(1) { |
echo_piyo | 13:dfae731e239f | 75 | led1 = interrupt; |
echo_piyo | 13:dfae731e239f | 76 | //pc.printf("Pulses is: %i\r\n",wheel.getPulses()); |
echo_piyo | 0:bf96e953cdb8 | 77 | } |
echo_piyo | 0:bf96e953cdb8 | 78 | } |
echo_piyo | 0:bf96e953cdb8 | 79 | |
echo_piyo | 0:bf96e953cdb8 | 80 | void setup() |
echo_piyo | 0:bf96e953cdb8 | 81 | { |
echo_piyo | 1:2d878962e6ea | 82 | wait(1); |
echo_piyo | 0:bf96e953cdb8 | 83 | bno055.begin(); |
echo_piyo | 0:bf96e953cdb8 | 84 | wait(1); |
echo_piyo | 0:bf96e953cdb8 | 85 | bno055.firstRead(); |
echo_piyo | 0:bf96e953cdb8 | 86 | pc.baud(pc_baud); |
echo_piyo | 0:bf96e953cdb8 | 87 | sbdbt.begin(sbdbt_baud); |
echo_piyo | 0:bf96e953cdb8 | 88 | rs422.begin(rs422_baud); |
echo_piyo | 0:bf96e953cdb8 | 89 | output_timer.attach(&output, output_period); |
echo_piyo | 2:d5b8f8e62923 | 90 | yaw_pid.setup(yaw_Kp, yaw_Ki, yaw_Kd); |
echo_piyo | 10:04f2a82cfd89 | 91 | mecanum.setupdeg(bno055.getYawRad()+180.0); |
echo_piyo | 4:a6cc2f03e69b | 92 | v1.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 93 | v2.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 94 | v3.setup(acceleration,output_period); |
echo_piyo | 4:a6cc2f03e69b | 95 | v4.setup(acceleration,output_period); |
echo_piyo | 13:dfae731e239f | 96 | //cylinder_origin(); |
echo_piyo | 12:1fec80ae8a2c | 97 | |
echo_piyo | 12:1fec80ae8a2c | 98 | //追加点 |
echo_piyo | 13:dfae731e239f | 99 | enc_cylinder.setup(1200); |
echo_piyo | 13:dfae731e239f | 100 | enc_cylinder.set_parameter(enc_Kp,enc_Ki,enc_Kd); |
echo_piyo | 12:1fec80ae8a2c | 101 | } |
echo_piyo | 12:1fec80ae8a2c | 102 | |
echo_piyo | 12:1fec80ae8a2c | 103 | void cylinder_origin(){ |
echo_piyo | 12:1fec80ae8a2c | 104 | while(interrupt){ |
echo_piyo | 12:1fec80ae8a2c | 105 | rs422.put(5, -0.8, 0.0); |
echo_piyo | 12:1fec80ae8a2c | 106 | } |
echo_piyo | 0:bf96e953cdb8 | 107 | } |
echo_piyo | 0:bf96e953cdb8 | 108 | |
echo_piyo | 10:04f2a82cfd89 | 109 | void boost(){ |
echo_piyo | 10:04f2a82cfd89 | 110 | if(sbdbt.sankaku) { |
echo_piyo | 10:04f2a82cfd89 | 111 | mecanum.boost_forward(); |
echo_piyo | 10:04f2a82cfd89 | 112 | } |
echo_piyo | 10:04f2a82cfd89 | 113 | if(sbdbt.batu) { |
echo_piyo | 10:04f2a82cfd89 | 114 | mecanum.boost_back(); |
echo_piyo | 10:04f2a82cfd89 | 115 | } |
echo_piyo | 10:04f2a82cfd89 | 116 | if(sbdbt.shikaku) { |
echo_piyo | 10:04f2a82cfd89 | 117 | mecanum.boost_left(); |
echo_piyo | 10:04f2a82cfd89 | 118 | } |
echo_piyo | 10:04f2a82cfd89 | 119 | if(sbdbt.maru) { |
echo_piyo | 10:04f2a82cfd89 | 120 | mecanum.boost_right(); |
echo_piyo | 10:04f2a82cfd89 | 121 | } |
echo_piyo | 10:04f2a82cfd89 | 122 | } |
echo_piyo | 10:04f2a82cfd89 | 123 | |
echo_piyo | 10:04f2a82cfd89 | 124 | void cylinder_check() |
echo_piyo | 10:04f2a82cfd89 | 125 | { |
echo_piyo | 13:dfae731e239f | 126 | //printf("up\t%d\tstate\t%d\tdown\t%d\tstate\t%d\t\r\n",sbdbt.up,sylinder_on.getState(),sbdbt.down,sylinder_off.getState()); |
echo_piyo | 13:dfae731e239f | 127 | /* |
echo_piyo | 13:dfae731e239f | 128 | if(sylinder_on?){ |
echo_piyo | 13:dfae731e239f | 129 | sylinder_on.cyclic(sbdbt.up); |
echo_piyo | 13:dfae731e239f | 130 | }else if(){ |
echo_piyo | 13:dfae731e239f | 131 | sylinder_off.cyclic(sbdbt.down); |
echo_piyo | 10:04f2a82cfd89 | 132 | } |
echo_piyo | 13:dfae731e239f | 133 | led2.cyclic(sbdbt.left); |
echo_piyo | 13:dfae731e239f | 134 | */ |
echo_piyo | 10:04f2a82cfd89 | 135 | } |
echo_piyo | 10:04f2a82cfd89 | 136 | |
echo_piyo | 5:6efda58ff71b | 137 | void put_output() |
echo_piyo | 5:6efda58ff71b | 138 | { |
echo_piyo | 4:a6cc2f03e69b | 139 | yaw = bno055.getYawRad(); |
echo_piyo | 4:a6cc2f03e69b | 140 | target_yaw = yaw; |
echo_piyo | 4:a6cc2f03e69b | 141 | yaw_pid.cal(target_yaw, yaw, output_period); |
echo_piyo | 4:a6cc2f03e69b | 142 | mecanum.sbdbt_cal(sbdbt.left_x, sbdbt.left_y, sbdbt.l1, sbdbt.r1, yaw_pid.duty(), bno055.getYawRad()); |
echo_piyo | 5:6efda58ff71b | 143 | // 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 | 0:bf96e953cdb8 | 144 | } |
echo_piyo | 0:bf96e953cdb8 | 145 | |
echo_piyo | 0:bf96e953cdb8 | 146 | void output() |
echo_piyo | 0:bf96e953cdb8 | 147 | { |
echo_piyo | 4:a6cc2f03e69b | 148 | put_output(); |
echo_piyo | 12:1fec80ae8a2c | 149 | cylinder_check(); |
echo_piyo | 10:04f2a82cfd89 | 150 | boost(); |
echo_piyo | 4:a6cc2f03e69b | 151 | |
echo_piyo | 0:bf96e953cdb8 | 152 | static int counter; |
echo_piyo | 0:bf96e953cdb8 | 153 | int id[nucleo_num] = {n1_id, n2_id, n3_id}; |
echo_piyo | 0:bf96e953cdb8 | 154 | |
echo_piyo | 0:bf96e953cdb8 | 155 | switch (counter) { |
echo_piyo | 0:bf96e953cdb8 | 156 | case 0: |
echo_piyo | 4:a6cc2f03e69b | 157 | rs422.put(id[counter], v1.duty(mecanum.v1()), v3.duty(mecanum.v3())); |
echo_piyo | 0:bf96e953cdb8 | 158 | counter++; |
echo_piyo | 0:bf96e953cdb8 | 159 | break; |
echo_piyo | 0:bf96e953cdb8 | 160 | case 1: |
echo_piyo | 4:a6cc2f03e69b | 161 | rs422.put(id[counter], v2.duty(mecanum.v2()), v4.duty(mecanum.v4())); |
echo_piyo | 0:bf96e953cdb8 | 162 | counter ++; |
echo_piyo | 0:bf96e953cdb8 | 163 | break; |
echo_piyo | 0:bf96e953cdb8 | 164 | case 2: |
echo_piyo | 10:04f2a82cfd89 | 165 | rs422.put(id[counter], sbdbt.right_y, 0.0); |
echo_piyo | 0:bf96e953cdb8 | 166 | counter = 0; |
echo_piyo | 0:bf96e953cdb8 | 167 | break; |
echo_piyo | 0:bf96e953cdb8 | 168 | default: |
echo_piyo | 0:bf96e953cdb8 | 169 | break; |
echo_piyo | 0:bf96e953cdb8 | 170 | }; |
echo_piyo | 5:6efda58ff71b | 171 | } |
echo_piyo | 5:6efda58ff71b | 172 |