Updated for checksum
main.cpp@2:32f69175c78e, 2021-03-30 (annotated)
- Committer:
- adimmit
- Date:
- Tue Mar 30 14:11:55 2021 +0000
- Revision:
- 2:32f69175c78e
- Parent:
- 1:aa253b5f5b65
- Child:
- 3:d5d603c7e5b6
updated some things
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adimmit | 0:76c761d3caf1 | 1 | |
adimmit | 1:aa253b5f5b65 | 2 | //counter for misc purposes |
adimmit | 1:aa253b5f5b65 | 3 | int counter3 = 0; |
adimmit | 0:76c761d3caf1 | 4 | //GO THROUGH AND RE-CHECK ALL THE VARIABLES, STRUCT NAMES, SIZES, BUFFERS + ETC!!! |
adimmit | 0:76c761d3caf1 | 5 | //ALSO GO THROUGH THE COMMENTS TO SEE IF THEY NEED CHANGING |
adimmit | 0:76c761d3caf1 | 6 | |
adimmit | 0:76c761d3caf1 | 7 | #include "mbed.h" |
adimmit | 0:76c761d3caf1 | 8 | #include "math_ops.h" |
adimmit | 0:76c761d3caf1 | 9 | #include <cstring> |
adimmit | 0:76c761d3caf1 | 10 | #include "leg_message.h" |
adimmit | 0:76c761d3caf1 | 11 | |
adimmit | 0:76c761d3caf1 | 12 | // length of receive/transmit buffers |
adimmit | 0:76c761d3caf1 | 13 | #define RX_LEN 98 //CHECK THESE BUFFER LENGHTS |
adimmit | 0:76c761d3caf1 | 14 | #define TX_LEN 98 //CHECK THESE BUFFER LENGHTS |
adimmit | 0:76c761d3caf1 | 15 | |
adimmit | 0:76c761d3caf1 | 16 | // length of outgoing/incoming messages |
adimmit | 0:76c761d3caf1 | 17 | #define DATA_LEN 44 //CHECK THESE BUFFER LENGHTS |
adimmit | 0:76c761d3caf1 | 18 | #define CMD_LEN 98 //CHECK THESE BUFFER LENGHTS |
adimmit | 0:76c761d3caf1 | 19 | |
adimmit | 0:76c761d3caf1 | 20 | // Master CAN ID /// |
adimmit | 0:76c761d3caf1 | 21 | #define CAN_ID 0x0 |
adimmit | 0:76c761d3caf1 | 22 | |
adimmit | 0:76c761d3caf1 | 23 | |
adimmit | 0:76c761d3caf1 | 24 | /// Value Limits /// |
adimmit | 0:76c761d3caf1 | 25 | #define P_MIN -12.5f |
adimmit | 0:76c761d3caf1 | 26 | #define P_MAX 12.5f |
adimmit | 0:76c761d3caf1 | 27 | #define V_MIN -65.0f |
adimmit | 0:76c761d3caf1 | 28 | #define V_MAX 65.0f |
adimmit | 0:76c761d3caf1 | 29 | #define KP_MIN 0.0f |
adimmit | 0:76c761d3caf1 | 30 | #define KP_MAX 500.0f |
adimmit | 0:76c761d3caf1 | 31 | #define KD_MIN 0.0f |
adimmit | 0:76c761d3caf1 | 32 | #define KD_MAX 5.0f |
adimmit | 0:76c761d3caf1 | 33 | #define T_MIN -18.0f |
adimmit | 0:76c761d3caf1 | 34 | #define T_MAX 18.0f |
adimmit | 0:76c761d3caf1 | 35 | |
adimmit | 0:76c761d3caf1 | 36 | /// Joint Soft Stops /// |
adimmit | 0:76c761d3caf1 | 37 | #define A_LIM_P 1.5f |
adimmit | 0:76c761d3caf1 | 38 | #define A_LIM_N -1.5f |
adimmit | 0:76c761d3caf1 | 39 | #define H_LIM_P 5.0f |
adimmit | 0:76c761d3caf1 | 40 | #define H_LIM_N -5.0f |
adimmit | 0:76c761d3caf1 | 41 | #define K_LIM_P 0.2f |
adimmit | 0:76c761d3caf1 | 42 | #define K_LIM_N 7.7f |
adimmit | 0:76c761d3caf1 | 43 | #define KP_SOFTSTOP 100.0f |
adimmit | 0:76c761d3caf1 | 44 | #define KD_SOFTSTOP 0.4f; |
adimmit | 0:76c761d3caf1 | 45 | |
adimmit | 0:76c761d3caf1 | 46 | #define ENABLE_CMD 0xFFFF |
adimmit | 0:76c761d3caf1 | 47 | #define DISABLE_CMD 0x1F1F |
adimmit | 0:76c761d3caf1 | 48 | |
adimmit | 0:76c761d3caf1 | 49 | spi_data_t spi_data; // data from spine to up |
adimmit | 0:76c761d3caf1 | 50 | spi_command_t spi_command; // data from up to spine |
adimmit | 0:76c761d3caf1 | 51 | |
adimmit | 0:76c761d3caf1 | 52 | // spi buffers |
adimmit | 0:76c761d3caf1 | 53 | uint16_t rx_buff[RX_LEN]; |
adimmit | 0:76c761d3caf1 | 54 | uint16_t tx_buff[TX_LEN]; |
adimmit | 0:76c761d3caf1 | 55 | |
adimmit | 0:76c761d3caf1 | 56 | DigitalOut led(PC_5); |
adimmit | 0:76c761d3caf1 | 57 | |
adimmit | 0:76c761d3caf1 | 58 | |
adimmit | 0:76c761d3caf1 | 59 | Serial pc(PA_2, PA_3); |
adimmit | 0:76c761d3caf1 | 60 | CAN can1(PB_12, PB_13, 1000000); // CAN Rx pin name, CAN Tx pin name |
adimmit | 0:76c761d3caf1 | 61 | CAN can2(PA_11, PA_12, 1000000); // CAN Rx pin name, CAN Tx pin name |
adimmit | 0:76c761d3caf1 | 62 | CAN can3(PA_8, PA_15, 1000000); // CAN Rx pin name, CAN Tx pin name //CAN4 on board |
adimmit | 0:76c761d3caf1 | 63 | |
adimmit | 0:76c761d3caf1 | 64 | CANMessage rxMsg1, rxMsg2, rxMsg3; |
adimmit | 0:76c761d3caf1 | 65 | CANMessage txMsg1, txMsg2, txMsg3; |
adimmit | 0:76c761d3caf1 | 66 | CANMessage q11_can, q21_can, q31_can, q12_can, q22_can, q32_can, q13_can, q23_can, q33_can; //TX Messages |
adimmit | 0:76c761d3caf1 | 67 | int ledState; |
adimmit | 0:76c761d3caf1 | 68 | Ticker sendCAN; |
adimmit | 0:76c761d3caf1 | 69 | int counter = 0; |
adimmit | 0:76c761d3caf1 | 70 | volatile bool msgAvailable = false; |
adimmit | 0:76c761d3caf1 | 71 | Ticker loop; |
adimmit | 0:76c761d3caf1 | 72 | |
adimmit | 0:76c761d3caf1 | 73 | int spi_enabled = 0; |
adimmit | 0:76c761d3caf1 | 74 | InterruptIn cs(PA_4); |
adimmit | 0:76c761d3caf1 | 75 | DigitalIn estop(PB_15); |
adimmit | 0:76c761d3caf1 | 76 | //SPISlave spi(PA_7, PA_6, PA_5, PA_4); |
adimmit | 0:76c761d3caf1 | 77 | |
adimmit | 0:76c761d3caf1 | 78 | |
adimmit | 0:76c761d3caf1 | 79 | grouped_act_state g1_state, g2_state, g3_state; |
adimmit | 0:76c761d3caf1 | 80 | grouped_act_control q1_control, q2_control, q3_control; |
adimmit | 0:76c761d3caf1 | 81 | |
adimmit | 0:76c761d3caf1 | 82 | uint16_t x = 0; |
adimmit | 0:76c761d3caf1 | 83 | uint16_t x2 = 0; |
adimmit | 0:76c761d3caf1 | 84 | uint16_t count = 0; |
adimmit | 0:76c761d3caf1 | 85 | uint16_t counter2 = 0; //SEE IF WE NEED TO UPDATE THESE TO ADD COUNTER3 AND X3 |
adimmit | 0:76c761d3caf1 | 86 | |
adimmit | 0:76c761d3caf1 | 87 | int control_mode = 1; |
adimmit | 0:76c761d3caf1 | 88 | int is_standing = 0; //SEE IF WE STILL NEED THE STANDING THING |
adimmit | 0:76c761d3caf1 | 89 | int enabled = 0; |
adimmit | 0:76c761d3caf1 | 90 | |
adimmit | 0:76c761d3caf1 | 91 | // generates fake spi data from spi command |
adimmit | 0:76c761d3caf1 | 92 | void test_control(); //MAY NEED TO GET RID OF THIS? |
adimmit | 0:76c761d3caf1 | 93 | void control(); |
adimmit | 0:76c761d3caf1 | 94 | |
adimmit | 0:76c761d3caf1 | 95 | |
adimmit | 0:76c761d3caf1 | 96 | /// CAN Command Packet Structure /// |
adimmit | 0:76c761d3caf1 | 97 | /// 16 bit position command, between -4*pi and 4*pi |
adimmit | 0:76c761d3caf1 | 98 | /// 12 bit velocity command, between -30 and + 30 rad/s |
adimmit | 0:76c761d3caf1 | 99 | /// 12 bit kp, between 0 and 500 N-m/rad |
adimmit | 0:76c761d3caf1 | 100 | /// 12 bit kd, between 0 and 100 N-m*s/rad |
adimmit | 0:76c761d3caf1 | 101 | /// 12 bit feed forward torque, between -18 and 18 N-m |
adimmit | 0:76c761d3caf1 | 102 | /// CAN Packet is 8 8-bit words |
adimmit | 0:76c761d3caf1 | 103 | /// Formatted as follows. For each quantity, bit 0 is LSB |
adimmit | 0:76c761d3caf1 | 104 | /// 0: [position[15-8]] |
adimmit | 0:76c761d3caf1 | 105 | /// 1: [position[7-0]] |
adimmit | 0:76c761d3caf1 | 106 | /// 2: [velocity[11-4]] |
adimmit | 0:76c761d3caf1 | 107 | /// 3: [velocity[3-0], kp[11-8]] |
adimmit | 0:76c761d3caf1 | 108 | /// 4: [kp[7-0]] |
adimmit | 0:76c761d3caf1 | 109 | /// 5: [kd[11-4]] |
adimmit | 0:76c761d3caf1 | 110 | /// 6: [kd[3-0], torque[11-8]] |
adimmit | 0:76c761d3caf1 | 111 | /// 7: [torque[7-0]] |
adimmit | 0:76c761d3caf1 | 112 | |
adimmit | 0:76c761d3caf1 | 113 | void pack_cmd(CANMessage * msg, joint_control joint){ |
adimmit | 0:76c761d3caf1 | 114 | |
adimmit | 0:76c761d3caf1 | 115 | /// limit data to be within bounds /// |
adimmit | 0:76c761d3caf1 | 116 | float p_des = fminf(fmaxf(P_MIN, joint.p_des), P_MAX); |
adimmit | 0:76c761d3caf1 | 117 | float v_des = fminf(fmaxf(V_MIN, joint.v_des), V_MAX); |
adimmit | 0:76c761d3caf1 | 118 | float kp = fminf(fmaxf(KP_MIN, joint.kp), KP_MAX); |
adimmit | 0:76c761d3caf1 | 119 | float kd = fminf(fmaxf(KD_MIN, joint.kd), KD_MAX); |
adimmit | 0:76c761d3caf1 | 120 | float t_ff = fminf(fmaxf(T_MIN, joint.t_ff), T_MAX); |
adimmit | 0:76c761d3caf1 | 121 | /// convert floats to unsigned ints /// |
adimmit | 0:76c761d3caf1 | 122 | uint16_t p_int = float_to_uint(p_des, P_MIN, P_MAX, 16); |
adimmit | 0:76c761d3caf1 | 123 | uint16_t v_int = float_to_uint(v_des, V_MIN, V_MAX, 12); |
adimmit | 0:76c761d3caf1 | 124 | uint16_t kp_int = float_to_uint(kp, KP_MIN, KP_MAX, 12); |
adimmit | 0:76c761d3caf1 | 125 | uint16_t kd_int = float_to_uint(kd, KD_MIN, KD_MAX, 12); |
adimmit | 0:76c761d3caf1 | 126 | uint16_t t_int = float_to_uint(t_ff, T_MIN, T_MAX, 12); |
adimmit | 0:76c761d3caf1 | 127 | /// pack ints into the can buffer /// |
adimmit | 0:76c761d3caf1 | 128 | msg->data[0] = p_int>>8; |
adimmit | 0:76c761d3caf1 | 129 | msg->data[1] = p_int&0xFF; |
adimmit | 0:76c761d3caf1 | 130 | msg->data[2] = v_int>>4; |
adimmit | 0:76c761d3caf1 | 131 | msg->data[3] = ((v_int&0xF)<<4)|(kp_int>>8); |
adimmit | 0:76c761d3caf1 | 132 | msg->data[4] = kp_int&0xFF; |
adimmit | 0:76c761d3caf1 | 133 | msg->data[5] = kd_int>>4; |
adimmit | 0:76c761d3caf1 | 134 | msg->data[6] = ((kd_int&0xF)<<4)|(t_int>>8); |
adimmit | 0:76c761d3caf1 | 135 | msg->data[7] = t_int&0xff; |
adimmit | 0:76c761d3caf1 | 136 | } |
adimmit | 0:76c761d3caf1 | 137 | |
adimmit | 0:76c761d3caf1 | 138 | /// CAN Reply Packet Structure /// |
adimmit | 0:76c761d3caf1 | 139 | /// 16 bit position, between -4*pi and 4*pi |
adimmit | 0:76c761d3caf1 | 140 | /// 12 bit velocity, between -30 and + 30 rad/s |
adimmit | 0:76c761d3caf1 | 141 | /// 12 bit current, between -40 and 40; |
adimmit | 0:76c761d3caf1 | 142 | /// CAN Packet is 5 8-bit words |
adimmit | 0:76c761d3caf1 | 143 | /// Formatted as follows. For each quantity, bit 0 is LSB |
adimmit | 0:76c761d3caf1 | 144 | /// 0: [position[15-8]] |
adimmit | 0:76c761d3caf1 | 145 | /// 1: [position[7-0]] |
adimmit | 0:76c761d3caf1 | 146 | /// 2: [velocity[11-4]] |
adimmit | 0:76c761d3caf1 | 147 | /// 3: [velocity[3-0], current[11-8]] |
adimmit | 0:76c761d3caf1 | 148 | /// 4: [current[7-0]] |
adimmit | 0:76c761d3caf1 | 149 | |
adimmit | 0:76c761d3caf1 | 150 | void unpack_reply(CANMessage msg, grouped_act_state * group){ |
adimmit | 0:76c761d3caf1 | 151 | /// unpack ints from can buffer /// |
adimmit | 0:76c761d3caf1 | 152 | uint16_t id = msg.data[0]; |
adimmit | 0:76c761d3caf1 | 153 | uint16_t p_int = (msg.data[1]<<8)|msg.data[2]; |
adimmit | 0:76c761d3caf1 | 154 | uint16_t v_int = (msg.data[3]<<4)|(msg.data[4]>>4); |
adimmit | 0:76c761d3caf1 | 155 | uint16_t i_int = ((msg.data[4]&0xF)<<8)|msg.data[5]; |
adimmit | 0:76c761d3caf1 | 156 | /// convert uints to floats /// |
adimmit | 0:76c761d3caf1 | 157 | float p = uint_to_float(p_int, P_MIN, P_MAX, 16); |
adimmit | 0:76c761d3caf1 | 158 | float v = uint_to_float(v_int, V_MIN, V_MAX, 12); |
adimmit | 0:76c761d3caf1 | 159 | float t = uint_to_float(i_int, -T_MAX, T_MAX, 12); |
adimmit | 0:76c761d3caf1 | 160 | |
adimmit | 0:76c761d3caf1 | 161 | if(id==1){ |
adimmit | 0:76c761d3caf1 | 162 | group->a.p = p; |
adimmit | 0:76c761d3caf1 | 163 | group->a.v = v; |
adimmit | 0:76c761d3caf1 | 164 | group->a.t = t; |
adimmit | 0:76c761d3caf1 | 165 | } |
adimmit | 0:76c761d3caf1 | 166 | else if(id==2){ |
adimmit | 0:76c761d3caf1 | 167 | group->h.p = p; |
adimmit | 0:76c761d3caf1 | 168 | group->h.v = v; |
adimmit | 0:76c761d3caf1 | 169 | group->h.t = t; |
adimmit | 0:76c761d3caf1 | 170 | } |
adimmit | 0:76c761d3caf1 | 171 | else if(id==3){ |
adimmit | 0:76c761d3caf1 | 172 | group->k.p = p; |
adimmit | 0:76c761d3caf1 | 173 | group->k.v = v; |
adimmit | 0:76c761d3caf1 | 174 | group->k.t = t; |
adimmit | 0:76c761d3caf1 | 175 | } |
adimmit | 0:76c761d3caf1 | 176 | } |
adimmit | 0:76c761d3caf1 | 177 | |
adimmit | 0:76c761d3caf1 | 178 | void rxISR1() { |
adimmit | 0:76c761d3caf1 | 179 | can1.read(rxMsg1); // read message into Rx message storage |
adimmit | 0:76c761d3caf1 | 180 | unpack_reply(rxMsg1, &g1_state); |
adimmit | 0:76c761d3caf1 | 181 | } |
adimmit | 0:76c761d3caf1 | 182 | void rxISR2(){ |
adimmit | 0:76c761d3caf1 | 183 | can2.read(rxMsg2); |
adimmit | 0:76c761d3caf1 | 184 | unpack_reply(rxMsg2, &g2_state); |
adimmit | 0:76c761d3caf1 | 185 | } |
adimmit | 0:76c761d3caf1 | 186 | void rxISR3(){ |
adimmit | 0:76c761d3caf1 | 187 | can3.read(rxMsg3); |
adimmit | 0:76c761d3caf1 | 188 | unpack_reply(rxMsg3, &g3_state); |
adimmit | 0:76c761d3caf1 | 189 | } |
adimmit | 0:76c761d3caf1 | 190 | |
adimmit | 0:76c761d3caf1 | 191 | void PackAll(){ |
adimmit | 0:76c761d3caf1 | 192 | pack_cmd(&q11_can, q1_control.a); |
adimmit | 0:76c761d3caf1 | 193 | pack_cmd(&q21_can, q1_control.h); |
adimmit | 0:76c761d3caf1 | 194 | pack_cmd(&q31_can, q1_control.k); |
adimmit | 0:76c761d3caf1 | 195 | pack_cmd(&q12_can, q2_control.a); |
adimmit | 1:aa253b5f5b65 | 196 | pack_cmd(&q22_can, q2_control.h); |
adimmit | 1:aa253b5f5b65 | 197 | pack_cmd(&q32_can, q2_control.k); |
adimmit | 0:76c761d3caf1 | 198 | pack_cmd(&q13_can, q3_control.a); |
adimmit | 1:aa253b5f5b65 | 199 | pack_cmd(&q23_can, q3_control.h); |
adimmit | 1:aa253b5f5b65 | 200 | pack_cmd(&q33_can, q3_control.k); |
adimmit | 0:76c761d3caf1 | 201 | |
adimmit | 0:76c761d3caf1 | 202 | } |
adimmit | 0:76c761d3caf1 | 203 | void WriteAll(){ |
adimmit | 0:76c761d3caf1 | 204 | //toggle = 1; |
adimmit | 0:76c761d3caf1 | 205 | can1.write(q11_can); |
adimmit | 0:76c761d3caf1 | 206 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 207 | can2.write(q12_can); |
adimmit | 0:76c761d3caf1 | 208 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 209 | can3.write(q13_can); |
adimmit | 0:76c761d3caf1 | 210 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 211 | can1.write(q21_can); |
adimmit | 0:76c761d3caf1 | 212 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 213 | can2.write(q22_can); |
adimmit | 0:76c761d3caf1 | 214 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 215 | can3.write(q23_can); |
adimmit | 0:76c761d3caf1 | 216 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 217 | can1.write(q31_can); |
adimmit | 0:76c761d3caf1 | 218 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 219 | can2.write(q32_can); |
adimmit | 0:76c761d3caf1 | 220 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 221 | can3.write(q33_can); |
adimmit | 0:76c761d3caf1 | 222 | wait(.00002); |
adimmit | 0:76c761d3caf1 | 223 | //toggle = 0; |
adimmit | 0:76c761d3caf1 | 224 | } |
adimmit | 0:76c761d3caf1 | 225 | |
adimmit | 0:76c761d3caf1 | 226 | void sendCMD(){ |
adimmit | 0:76c761d3caf1 | 227 | counter ++; |
adimmit | 0:76c761d3caf1 | 228 | |
adimmit | 0:76c761d3caf1 | 229 | PackAll(); |
adimmit | 0:76c761d3caf1 | 230 | |
adimmit | 0:76c761d3caf1 | 231 | if(counter>100){ |
adimmit | 1:aa253b5f5b65 | 232 | pc.printf("%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n\r", g1_state.a.p, g1_state.h.p, g1_state.k.p, g2_state.a.p, g2_state.h.p, g2_state.k.p, g3_state.a.p, g3_state.h.p, g3_state.k.p); |
adimmit | 0:76c761d3caf1 | 233 | counter = 0 ; |
adimmit | 0:76c761d3caf1 | 234 | } |
adimmit | 0:76c761d3caf1 | 235 | |
adimmit | 0:76c761d3caf1 | 236 | WriteAll(); |
adimmit | 0:76c761d3caf1 | 237 | } |
adimmit | 0:76c761d3caf1 | 238 | |
adimmit | 0:76c761d3caf1 | 239 | |
adimmit | 0:76c761d3caf1 | 240 | |
adimmit | 0:76c761d3caf1 | 241 | |
adimmit | 0:76c761d3caf1 | 242 | void Zero(CANMessage * msg){ |
adimmit | 0:76c761d3caf1 | 243 | msg->data[0] = 0xFF; |
adimmit | 0:76c761d3caf1 | 244 | msg->data[1] = 0xFF; |
adimmit | 0:76c761d3caf1 | 245 | msg->data[2] = 0xFF; |
adimmit | 0:76c761d3caf1 | 246 | msg->data[3] = 0xFF; |
adimmit | 0:76c761d3caf1 | 247 | msg->data[4] = 0xFF; |
adimmit | 0:76c761d3caf1 | 248 | msg->data[5] = 0xFF; |
adimmit | 0:76c761d3caf1 | 249 | msg->data[6] = 0xFF; |
adimmit | 0:76c761d3caf1 | 250 | msg->data[7] = 0xFE; |
adimmit | 0:76c761d3caf1 | 251 | WriteAll(); |
adimmit | 0:76c761d3caf1 | 252 | } |
adimmit | 0:76c761d3caf1 | 253 | |
adimmit | 0:76c761d3caf1 | 254 | void EnterMotorMode(CANMessage * msg){ |
adimmit | 0:76c761d3caf1 | 255 | msg->data[0] = 0xFF; |
adimmit | 0:76c761d3caf1 | 256 | msg->data[1] = 0xFF; |
adimmit | 0:76c761d3caf1 | 257 | msg->data[2] = 0xFF; |
adimmit | 0:76c761d3caf1 | 258 | msg->data[3] = 0xFF; |
adimmit | 0:76c761d3caf1 | 259 | msg->data[4] = 0xFF; |
adimmit | 0:76c761d3caf1 | 260 | msg->data[5] = 0xFF; |
adimmit | 0:76c761d3caf1 | 261 | msg->data[6] = 0xFF; |
adimmit | 0:76c761d3caf1 | 262 | msg->data[7] = 0xFC; |
adimmit | 0:76c761d3caf1 | 263 | //WriteAll(); |
adimmit | 0:76c761d3caf1 | 264 | } |
adimmit | 0:76c761d3caf1 | 265 | |
adimmit | 0:76c761d3caf1 | 266 | void ExitMotorMode(CANMessage * msg){ |
adimmit | 0:76c761d3caf1 | 267 | msg->data[0] = 0xFF; |
adimmit | 0:76c761d3caf1 | 268 | msg->data[1] = 0xFF; |
adimmit | 0:76c761d3caf1 | 269 | msg->data[2] = 0xFF; |
adimmit | 0:76c761d3caf1 | 270 | msg->data[3] = 0xFF; |
adimmit | 0:76c761d3caf1 | 271 | msg->data[4] = 0xFF; |
adimmit | 0:76c761d3caf1 | 272 | msg->data[5] = 0xFF; |
adimmit | 0:76c761d3caf1 | 273 | msg->data[6] = 0xFF; |
adimmit | 0:76c761d3caf1 | 274 | msg->data[7] = 0xFD; |
adimmit | 0:76c761d3caf1 | 275 | //WriteAll(); |
adimmit | 0:76c761d3caf1 | 276 | } |
adimmit | 0:76c761d3caf1 | 277 | void serial_isr(){ |
adimmit | 0:76c761d3caf1 | 278 | /// handle keyboard commands from the serial terminal /// |
adimmit | 0:76c761d3caf1 | 279 | while(pc.readable()){ |
adimmit | 0:76c761d3caf1 | 280 | char c = pc.getc(); |
adimmit | 0:76c761d3caf1 | 281 | //led = !led; |
adimmit | 0:76c761d3caf1 | 282 | switch(c){ |
adimmit | 0:76c761d3caf1 | 283 | case(27): |
adimmit | 0:76c761d3caf1 | 284 | //loop.detach(); |
adimmit | 1:aa253b5f5b65 | 285 | pc.printf("\n\r exiting motor mode \n\r"); |
adimmit | 0:76c761d3caf1 | 286 | ExitMotorMode(&q11_can); |
adimmit | 0:76c761d3caf1 | 287 | ExitMotorMode(&q21_can); |
adimmit | 0:76c761d3caf1 | 288 | ExitMotorMode(&q31_can); |
adimmit | 0:76c761d3caf1 | 289 | ExitMotorMode(&q12_can); |
adimmit | 0:76c761d3caf1 | 290 | ExitMotorMode(&q22_can); |
adimmit | 0:76c761d3caf1 | 291 | ExitMotorMode(&q32_can); |
adimmit | 0:76c761d3caf1 | 292 | ExitMotorMode(&q13_can); |
adimmit | 0:76c761d3caf1 | 293 | ExitMotorMode(&q23_can); |
adimmit | 0:76c761d3caf1 | 294 | ExitMotorMode(&q33_can); |
adimmit | 0:76c761d3caf1 | 295 | enabled = 0; |
adimmit | 0:76c761d3caf1 | 296 | break; |
adimmit | 0:76c761d3caf1 | 297 | case('m'): |
adimmit | 1:aa253b5f5b65 | 298 | pc.printf("\n\r entering motor mode \n\r"); |
adimmit | 0:76c761d3caf1 | 299 | EnterMotorMode(&q11_can); |
adimmit | 0:76c761d3caf1 | 300 | EnterMotorMode(&q21_can); |
adimmit | 0:76c761d3caf1 | 301 | EnterMotorMode(&q31_can); |
adimmit | 0:76c761d3caf1 | 302 | EnterMotorMode(&q12_can); |
adimmit | 0:76c761d3caf1 | 303 | EnterMotorMode(&q22_can); |
adimmit | 0:76c761d3caf1 | 304 | EnterMotorMode(&q32_can); |
adimmit | 0:76c761d3caf1 | 305 | EnterMotorMode(&q13_can); |
adimmit | 0:76c761d3caf1 | 306 | EnterMotorMode(&q23_can); |
adimmit | 0:76c761d3caf1 | 307 | EnterMotorMode(&q33_can); |
adimmit | 0:76c761d3caf1 | 308 | wait(.5); |
adimmit | 0:76c761d3caf1 | 309 | enabled = 1; |
adimmit | 0:76c761d3caf1 | 310 | //loop.attach(&sendCMD, .001); |
adimmit | 0:76c761d3caf1 | 311 | break; |
adimmit | 0:76c761d3caf1 | 312 | case('s'): |
adimmit | 1:aa253b5f5b65 | 313 | pc.printf("\n\r standing \n\r"); |
adimmit | 0:76c761d3caf1 | 314 | counter2 = 0; |
adimmit | 0:76c761d3caf1 | 315 | is_standing = 1; |
adimmit | 0:76c761d3caf1 | 316 | //stand(); |
adimmit | 0:76c761d3caf1 | 317 | break; |
adimmit | 0:76c761d3caf1 | 318 | case('z'): |
adimmit | 1:aa253b5f5b65 | 319 | pc.printf("\n\r zeroing \n\r"); |
adimmit | 0:76c761d3caf1 | 320 | Zero(&q11_can); |
adimmit | 0:76c761d3caf1 | 321 | Zero(&q21_can); |
adimmit | 0:76c761d3caf1 | 322 | Zero(&q31_can); |
adimmit | 0:76c761d3caf1 | 323 | Zero(&q12_can); |
adimmit | 0:76c761d3caf1 | 324 | Zero(&q22_can); |
adimmit | 0:76c761d3caf1 | 325 | Zero(&q32_can); |
adimmit | 0:76c761d3caf1 | 326 | Zero(&q13_can); |
adimmit | 0:76c761d3caf1 | 327 | Zero(&q23_can); |
adimmit | 0:76c761d3caf1 | 328 | Zero(&q33_can); |
adimmit | 0:76c761d3caf1 | 329 | break; |
adimmit | 0:76c761d3caf1 | 330 | } |
adimmit | 0:76c761d3caf1 | 331 | } |
adimmit | 0:76c761d3caf1 | 332 | WriteAll(); |
adimmit | 0:76c761d3caf1 | 333 | |
adimmit | 0:76c761d3caf1 | 334 | } |
adimmit | 0:76c761d3caf1 | 335 | |
adimmit | 0:76c761d3caf1 | 336 | uint32_t xor_checksum(uint32_t* data, size_t len) |
adimmit | 0:76c761d3caf1 | 337 | { |
adimmit | 0:76c761d3caf1 | 338 | uint32_t t = 0; |
adimmit | 0:76c761d3caf1 | 339 | for(int i = 0; i < len; i++) |
adimmit | 0:76c761d3caf1 | 340 | t = t ^ data[i]; |
adimmit | 0:76c761d3caf1 | 341 | return t; |
adimmit | 0:76c761d3caf1 | 342 | } |
adimmit | 0:76c761d3caf1 | 343 | |
adimmit | 2:32f69175c78e | 344 | |
adimmit | 2:32f69175c78e | 345 | |
adimmit | 2:32f69175c78e | 346 | void print_SPI_command() { |
adimmit | 2:32f69175c78e | 347 | pc.printf("SPI MESSAGE RECIEVED:\n"); |
adimmit | 2:32f69175c78e | 348 | |
adimmit | 2:32f69175c78e | 349 | pc.printf("MOTOR 1 Q: %f\n", spi_command.q_des_1s[0]); |
adimmit | 2:32f69175c78e | 350 | pc.printf("MOTOR 1 Qd: %f\n", spi_command.qd_des_1s[0]); |
adimmit | 2:32f69175c78e | 351 | pc.printf("MOTOR 1 Kp: %f\n", spi_command.kp_1s[0]); |
adimmit | 2:32f69175c78e | 352 | pc.printf("MOTOR 1 Kd: %f\n", spi_command.kd_1s[0]); |
adimmit | 2:32f69175c78e | 353 | pc.printf("MOTOR 1 T_FF: %f\n", spi_command.tau_1s_ff[0]); |
adimmit | 2:32f69175c78e | 354 | |
adimmit | 2:32f69175c78e | 355 | pc.printf("MOTOR 2 Q: %f\n", spi_command.q_des_2s[0]); |
adimmit | 2:32f69175c78e | 356 | pc.printf("MOTOR 2 Qd: %f\n", spi_command.qd_des_2s[0]); |
adimmit | 2:32f69175c78e | 357 | pc.printf("MOTOR 2 Kp: %f\n", spi_command.kp_2s[0]); |
adimmit | 2:32f69175c78e | 358 | pc.printf("MOTOR 2 Kd: %f\n", spi_command.kd_2s[0]); |
adimmit | 2:32f69175c78e | 359 | pc.printf("MOTOR 2 T_FF: %f\n", spi_command.tau_2s_ff[0]); |
adimmit | 2:32f69175c78e | 360 | |
adimmit | 2:32f69175c78e | 361 | pc.printf("MOTOR 3 Q: %f\n", spi_command.q_des_3s[0]); |
adimmit | 2:32f69175c78e | 362 | pc.printf("MOTOR 3 Qd: %f\n", spi_command.qd_des_3s[0]); |
adimmit | 2:32f69175c78e | 363 | pc.printf("MOTOR 3 Kp: %f\n", spi_command.kp_3s[0]); |
adimmit | 2:32f69175c78e | 364 | pc.printf("MOTOR 3 Kd: %f\n", spi_command.kd_3s[0]); |
adimmit | 2:32f69175c78e | 365 | pc.printf("MOTOR 3 T_FF: %f\n", spi_command.tau_3s_ff[0]); |
adimmit | 2:32f69175c78e | 366 | |
adimmit | 2:32f69175c78e | 367 | pc.printf("MOTOR 4 Q: %f\n", spi_command.q_des_1s[1]); |
adimmit | 2:32f69175c78e | 368 | pc.printf("MOTOR 4 Qd: %f\n", spi_command.qd_des_1s[1]); |
adimmit | 2:32f69175c78e | 369 | pc.printf("MOTOR 4 Kp: %f\n", spi_command.kp_1s[1]); |
adimmit | 2:32f69175c78e | 370 | pc.printf("MOTOR 4 Kd: %f\n", spi_command.kd_1s[1]); |
adimmit | 2:32f69175c78e | 371 | pc.printf("MOTOR 4 T_FF: %f\n", spi_command.tau_1s_ff[1]); |
adimmit | 2:32f69175c78e | 372 | |
adimmit | 2:32f69175c78e | 373 | pc.printf("MOTOR 5 Q: %f\n", spi_command.q_des_2s[1]); |
adimmit | 2:32f69175c78e | 374 | pc.printf("MOTOR 5 Qd: %f\n", spi_command.qd_des_2s[1]); |
adimmit | 2:32f69175c78e | 375 | pc.printf("MOTOR 5 Kp: %f\n", spi_command.kp_2s[1]); |
adimmit | 2:32f69175c78e | 376 | pc.printf("MOTOR 5 Kd: %f\n", spi_command.kd_2s[1]); |
adimmit | 2:32f69175c78e | 377 | pc.printf("MOTOR 5 T_FF: %f\n", spi_command.tau_2s_ff[1]); |
adimmit | 2:32f69175c78e | 378 | |
adimmit | 2:32f69175c78e | 379 | pc.printf("MOTOR 6 Q: %f\n", spi_command.q_des_3s[1]); |
adimmit | 2:32f69175c78e | 380 | pc.printf("MOTOR 6 Qd: %f\n", spi_command.qd_des_3s[1]); |
adimmit | 2:32f69175c78e | 381 | pc.printf("MOTOR 6 Kp: %f\n", spi_command.kp_3s[1]); |
adimmit | 2:32f69175c78e | 382 | pc.printf("MOTOR 6 Kd: %f\n", spi_command.kd_3s[1]); |
adimmit | 2:32f69175c78e | 383 | pc.printf("MOTOR 6 T_FF: %f\n", spi_command.tau_3s_ff[1]); |
adimmit | 2:32f69175c78e | 384 | |
adimmit | 2:32f69175c78e | 385 | pc.printf("MOTOR 7 Q: %f\n", spi_command.q_des_1s[2]); |
adimmit | 2:32f69175c78e | 386 | pc.printf("MOTOR 7 Qd: %f\n", spi_command.qd_des_1s[2]); |
adimmit | 2:32f69175c78e | 387 | pc.printf("MOTOR 7 Kp: %f\n", spi_command.kp_1s[2]); |
adimmit | 2:32f69175c78e | 388 | pc.printf("MOTOR 7 Kd: %f\n", spi_command.kd_1s[2]); |
adimmit | 2:32f69175c78e | 389 | pc.printf("MOTOR 7 T_FF: %f\n", spi_command.tau_1s_ff[2]); |
adimmit | 2:32f69175c78e | 390 | |
adimmit | 2:32f69175c78e | 391 | pc.printf("MOTOR 8 Q: %f\n", spi_command.q_des_2s[2]); |
adimmit | 2:32f69175c78e | 392 | pc.printf("MOTOR 8 Qd: %f\n", spi_command.qd_des_2s[2]); |
adimmit | 2:32f69175c78e | 393 | pc.printf("MOTOR 8 Kp: %f\n", spi_command.kp_2s[2]); |
adimmit | 2:32f69175c78e | 394 | pc.printf("MOTOR 8 Kd: %f\n", spi_command.kd_2s[2]); |
adimmit | 2:32f69175c78e | 395 | pc.printf("MOTOR 8 T_FF: %f\n", spi_command.tau_2s_ff[2]); |
adimmit | 2:32f69175c78e | 396 | |
adimmit | 2:32f69175c78e | 397 | pc.printf("MOTOR 9 Q: %f\n", spi_command.q_des_3s[2]); |
adimmit | 2:32f69175c78e | 398 | pc.printf("MOTOR 9 Qd: %f\n", spi_command.qd_des_3s[2]); |
adimmit | 2:32f69175c78e | 399 | pc.printf("MOTOR 9 Kp: %f\n", spi_command.kp_3s[2]); |
adimmit | 2:32f69175c78e | 400 | pc.printf("MOTOR 9 Kd: %f\n", spi_command.kd_3s[2]); |
adimmit | 2:32f69175c78e | 401 | pc.printf("MOTOR 9 T_FF: %f\n", spi_command.tau_3s_ff[2]); |
adimmit | 2:32f69175c78e | 402 | |
adimmit | 2:32f69175c78e | 403 | } |
adimmit | 2:32f69175c78e | 404 | |
adimmit | 2:32f69175c78e | 405 | |
adimmit | 2:32f69175c78e | 406 | |
adimmit | 2:32f69175c78e | 407 | |
adimmit | 2:32f69175c78e | 408 | |
adimmit | 0:76c761d3caf1 | 409 | void spi_isr(void) |
adimmit | 0:76c761d3caf1 | 410 | { |
adimmit | 1:aa253b5f5b65 | 411 | //pc.printf("CS ACTIVE\n"); |
adimmit | 0:76c761d3caf1 | 412 | GPIOC->ODR |= (1 << 8); |
adimmit | 0:76c761d3caf1 | 413 | GPIOC->ODR &= ~(1 << 8); |
adimmit | 0:76c761d3caf1 | 414 | int bytecount = 0; |
adimmit | 0:76c761d3caf1 | 415 | SPI1->DR = tx_buff[0]; |
adimmit | 0:76c761d3caf1 | 416 | while(cs == 0) { |
adimmit | 0:76c761d3caf1 | 417 | if(SPI1->SR&0x1) { |
adimmit | 1:aa253b5f5b65 | 418 | rx_buff[bytecount] = SPI1->DR; |
adimmit | 0:76c761d3caf1 | 419 | bytecount++; |
adimmit | 0:76c761d3caf1 | 420 | if(bytecount<TX_LEN) { |
adimmit | 0:76c761d3caf1 | 421 | SPI1->DR = tx_buff[bytecount]; |
adimmit | 0:76c761d3caf1 | 422 | } |
adimmit | 0:76c761d3caf1 | 423 | } |
adimmit | 0:76c761d3caf1 | 424 | } |
adimmit | 2:32f69175c78e | 425 | //pc.printf("RECIEVED: %d BYTES\n", bytecount); |
adimmit | 0:76c761d3caf1 | 426 | |
adimmit | 1:aa253b5f5b65 | 427 | //pc.printf("HERE'S A SPI COMMAND:\n"); |
adimmit | 0:76c761d3caf1 | 428 | // after reading, save into spi_command |
adimmit | 0:76c761d3caf1 | 429 | // should probably check checksum first! |
adimmit | 0:76c761d3caf1 | 430 | uint32_t calc_checksum = xor_checksum((uint32_t*)rx_buff,32); |
adimmit | 0:76c761d3caf1 | 431 | for(int i = 0; i < CMD_LEN; i++) |
adimmit | 0:76c761d3caf1 | 432 | { |
adimmit | 0:76c761d3caf1 | 433 | ((uint16_t*)(&spi_command))[i] = rx_buff[i]; |
adimmit | 2:32f69175c78e | 434 | //pc.printf("WORD %d RECIEVED: %d\n", i, rx_buff[i]); |
adimmit | 0:76c761d3caf1 | 435 | } |
adimmit | 2:32f69175c78e | 436 | print_SPI_command(); |
adimmit | 0:76c761d3caf1 | 437 | |
adimmit | 0:76c761d3caf1 | 438 | // run control, which fills in tx_buff for the next iteration |
adimmit | 2:32f69175c78e | 439 | /* |
adimmit | 0:76c761d3caf1 | 440 | if(calc_checksum != spi_command.checksum){ |
adimmit | 1:aa253b5f5b65 | 441 | spi_data.flags[1] = 0xdead; |
adimmit | 2:32f69175c78e | 442 | pc.printf("FAILED CHECKSUM\n"); |
adimmit | 2:32f69175c78e | 443 | pc.printf("ACTUAL: %d\n", calc_checksum); |
adimmit | 2:32f69175c78e | 444 | pc.printf("CURRENT: %d\n", spi_command.checksum);} |
adimmit | 2:32f69175c78e | 445 | */ |
adimmit | 0:76c761d3caf1 | 446 | |
adimmit | 0:76c761d3caf1 | 447 | //test_control(); |
adimmit | 0:76c761d3caf1 | 448 | //spi_data.q_abad[0] = 12.0f; |
adimmit | 0:76c761d3caf1 | 449 | control(); |
adimmit | 0:76c761d3caf1 | 450 | PackAll(); |
adimmit | 0:76c761d3caf1 | 451 | WriteAll(); |
adimmit | 0:76c761d3caf1 | 452 | |
adimmit | 0:76c761d3caf1 | 453 | |
adimmit | 0:76c761d3caf1 | 454 | //for (int i = 0; i<TX_LEN; i++) { |
adimmit | 0:76c761d3caf1 | 455 | // tx_buff[i] = 2*rx_buff[i]; |
adimmit | 0:76c761d3caf1 | 456 | //} |
adimmit | 0:76c761d3caf1 | 457 | // for (int i=0; i<TX_LEN; i++) { |
adimmit | 0:76c761d3caf1 | 458 | // //printf("%d ", rx_buff[i]); |
adimmit | 0:76c761d3caf1 | 459 | // } |
adimmit | 0:76c761d3caf1 | 460 | //printf("\n\r"); |
adimmit | 0:76c761d3caf1 | 461 | } |
adimmit | 0:76c761d3caf1 | 462 | |
adimmit | 0:76c761d3caf1 | 463 | int softstop_joint(joint_state state, joint_control * control, float limit_p, float limit_n){ |
adimmit | 0:76c761d3caf1 | 464 | if((state.p)>=limit_p){ |
adimmit | 0:76c761d3caf1 | 465 | //control->p_des = limit_p; |
adimmit | 0:76c761d3caf1 | 466 | control->v_des = 0.0f; |
adimmit | 0:76c761d3caf1 | 467 | control->kp = 0; |
adimmit | 0:76c761d3caf1 | 468 | control->kd = KD_SOFTSTOP; |
adimmit | 0:76c761d3caf1 | 469 | control->t_ff += KP_SOFTSTOP*(limit_p - state.p); |
adimmit | 0:76c761d3caf1 | 470 | return 1; |
adimmit | 0:76c761d3caf1 | 471 | } |
adimmit | 0:76c761d3caf1 | 472 | else if((state.p)<=limit_n){ |
adimmit | 0:76c761d3caf1 | 473 | //control->p_des = limit_n; |
adimmit | 0:76c761d3caf1 | 474 | control->v_des = 0.0f; |
adimmit | 0:76c761d3caf1 | 475 | control->kp = 0; |
adimmit | 0:76c761d3caf1 | 476 | control->kd = KD_SOFTSTOP; |
adimmit | 0:76c761d3caf1 | 477 | control->t_ff += KP_SOFTSTOP*(limit_n - state.p); |
adimmit | 0:76c761d3caf1 | 478 | return 1; |
adimmit | 0:76c761d3caf1 | 479 | } |
adimmit | 0:76c761d3caf1 | 480 | return 0; |
adimmit | 0:76c761d3caf1 | 481 | |
adimmit | 0:76c761d3caf1 | 482 | } |
adimmit | 0:76c761d3caf1 | 483 | |
adimmit | 0:76c761d3caf1 | 484 | |
adimmit | 0:76c761d3caf1 | 485 | void control() |
adimmit | 0:76c761d3caf1 | 486 | { |
adimmit | 0:76c761d3caf1 | 487 | |
adimmit | 0:76c761d3caf1 | 488 | if(((spi_command.flags[0]&0x1)==1) && (enabled==0)){ |
adimmit | 0:76c761d3caf1 | 489 | enabled = 1; |
adimmit | 0:76c761d3caf1 | 490 | //BUS ONE |
adimmit | 0:76c761d3caf1 | 491 | EnterMotorMode(&q11_can); |
adimmit | 0:76c761d3caf1 | 492 | can1.write(q11_can); |
adimmit | 0:76c761d3caf1 | 493 | EnterMotorMode(&q21_can); |
adimmit | 0:76c761d3caf1 | 494 | can1.write(q21_can); |
adimmit | 0:76c761d3caf1 | 495 | EnterMotorMode(&q31_can); |
adimmit | 0:76c761d3caf1 | 496 | can1.write(q31_can); |
adimmit | 0:76c761d3caf1 | 497 | //BUS TWO |
adimmit | 0:76c761d3caf1 | 498 | EnterMotorMode(&q12_can); |
adimmit | 0:76c761d3caf1 | 499 | can2.write(q12_can); |
adimmit | 0:76c761d3caf1 | 500 | EnterMotorMode(&q22_can); |
adimmit | 0:76c761d3caf1 | 501 | can2.write(q22_can); |
adimmit | 1:aa253b5f5b65 | 502 | EnterMotorMode(&q32_can); |
adimmit | 0:76c761d3caf1 | 503 | can2.write(q32_can); |
adimmit | 0:76c761d3caf1 | 504 | //BUS THREE |
adimmit | 0:76c761d3caf1 | 505 | EnterMotorMode(&q13_can); |
adimmit | 0:76c761d3caf1 | 506 | can3.write(q13_can); |
adimmit | 0:76c761d3caf1 | 507 | EnterMotorMode(&q23_can); |
adimmit | 0:76c761d3caf1 | 508 | can3.write(q23_can); |
adimmit | 0:76c761d3caf1 | 509 | EnterMotorMode(&q33_can); |
adimmit | 0:76c761d3caf1 | 510 | can3.write(q33_can); |
adimmit | 1:aa253b5f5b65 | 511 | pc.printf("e\n\r"); |
adimmit | 0:76c761d3caf1 | 512 | return; |
adimmit | 0:76c761d3caf1 | 513 | } |
adimmit | 0:76c761d3caf1 | 514 | else if((((spi_command.flags[0]&0x1))==0) && (enabled==1)){ |
adimmit | 0:76c761d3caf1 | 515 | enabled = 0; |
adimmit | 0:76c761d3caf1 | 516 | //BUS ONE |
adimmit | 0:76c761d3caf1 | 517 | ExitMotorMode(&q11_can); |
adimmit | 0:76c761d3caf1 | 518 | can1.write(q11_can); |
adimmit | 0:76c761d3caf1 | 519 | ExitMotorMode(&q21_can); |
adimmit | 0:76c761d3caf1 | 520 | can1.write(q21_can); |
adimmit | 0:76c761d3caf1 | 521 | ExitMotorMode(&q31_can); |
adimmit | 0:76c761d3caf1 | 522 | can1.write(q31_can); |
adimmit | 0:76c761d3caf1 | 523 | //BUS TWO |
adimmit | 0:76c761d3caf1 | 524 | ExitMotorMode(&q12_can); |
adimmit | 0:76c761d3caf1 | 525 | can2.write(q12_can); |
adimmit | 0:76c761d3caf1 | 526 | ExitMotorMode(&q22_can); |
adimmit | 0:76c761d3caf1 | 527 | can2.write(q22_can); |
adimmit | 1:aa253b5f5b65 | 528 | ExitMotorMode(&q32_can); |
adimmit | 0:76c761d3caf1 | 529 | can2.write(q32_can); |
adimmit | 0:76c761d3caf1 | 530 | //BUS THREE |
adimmit | 0:76c761d3caf1 | 531 | ExitMotorMode(&q13_can); |
adimmit | 0:76c761d3caf1 | 532 | can3.write(q13_can); |
adimmit | 0:76c761d3caf1 | 533 | ExitMotorMode(&q23_can); |
adimmit | 0:76c761d3caf1 | 534 | can3.write(q23_can); |
adimmit | 0:76c761d3caf1 | 535 | ExitMotorMode(&q33_can); |
adimmit | 0:76c761d3caf1 | 536 | can3.write(q33_can); |
adimmit | 1:aa253b5f5b65 | 537 | pc.printf("x\n\r"); |
adimmit | 0:76c761d3caf1 | 538 | return; |
adimmit | 0:76c761d3caf1 | 539 | } |
adimmit | 0:76c761d3caf1 | 540 | |
adimmit | 0:76c761d3caf1 | 541 | spi_data.q_1s[0] = g1_state.a.p; |
adimmit | 0:76c761d3caf1 | 542 | spi_data.q_2s[0] = g1_state.h.p; |
adimmit | 0:76c761d3caf1 | 543 | spi_data.q_3s[0] = g1_state.k.p; |
adimmit | 0:76c761d3caf1 | 544 | spi_data.qd_1s[0] = g1_state.a.v; |
adimmit | 0:76c761d3caf1 | 545 | spi_data.qd_2s[0] = g1_state.h.v; |
adimmit | 0:76c761d3caf1 | 546 | spi_data.qd_3s[0] = g1_state.k.v; |
adimmit | 0:76c761d3caf1 | 547 | |
adimmit | 0:76c761d3caf1 | 548 | spi_data.q_1s[1] = g1_state.a.p; |
adimmit | 0:76c761d3caf1 | 549 | spi_data.q_2s[1] = g1_state.h.p; |
adimmit | 0:76c761d3caf1 | 550 | spi_data.q_3s[1] = g1_state.k.p; |
adimmit | 0:76c761d3caf1 | 551 | spi_data.qd_1s[1] = g1_state.a.v; |
adimmit | 0:76c761d3caf1 | 552 | spi_data.qd_2s[1] = g1_state.h.v; |
adimmit | 0:76c761d3caf1 | 553 | spi_data.qd_3s[1] = g1_state.k.v; |
adimmit | 0:76c761d3caf1 | 554 | |
adimmit | 0:76c761d3caf1 | 555 | spi_data.q_1s[2] = g1_state.a.p; |
adimmit | 0:76c761d3caf1 | 556 | spi_data.q_2s[2] = g1_state.h.p; |
adimmit | 0:76c761d3caf1 | 557 | spi_data.q_3s[2] = g1_state.k.p; |
adimmit | 0:76c761d3caf1 | 558 | spi_data.qd_1s[2] = g1_state.a.v; |
adimmit | 0:76c761d3caf1 | 559 | spi_data.qd_2s[2] = g1_state.h.v; |
adimmit | 0:76c761d3caf1 | 560 | spi_data.qd_3s[2] = g1_state.k.v; |
adimmit | 0:76c761d3caf1 | 561 | |
adimmit | 0:76c761d3caf1 | 562 | |
adimmit | 0:76c761d3caf1 | 563 | |
adimmit | 0:76c761d3caf1 | 564 | if(estop==0){ |
adimmit | 0:76c761d3caf1 | 565 | //printf("estopped!!!!\n\r"); |
adimmit | 0:76c761d3caf1 | 566 | memset(&q1_control, 0, sizeof(q1_control)); |
adimmit | 0:76c761d3caf1 | 567 | memset(&q2_control, 0, sizeof(q2_control)); |
adimmit | 0:76c761d3caf1 | 568 | memset(&q3_control, 0, sizeof(q3_control)); |
adimmit | 0:76c761d3caf1 | 569 | spi_data.flags[0] = 0xdead; |
adimmit | 0:76c761d3caf1 | 570 | spi_data.flags[1] = 0xdead; |
adimmit | 0:76c761d3caf1 | 571 | spi_data.flags[2] = 0xdead; |
adimmit | 0:76c761d3caf1 | 572 | led = 1; |
adimmit | 0:76c761d3caf1 | 573 | } |
adimmit | 0:76c761d3caf1 | 574 | |
adimmit | 0:76c761d3caf1 | 575 | else{ |
adimmit | 0:76c761d3caf1 | 576 | led = 0; |
adimmit | 0:76c761d3caf1 | 577 | |
adimmit | 0:76c761d3caf1 | 578 | memset(&q1_control, 0, sizeof(q1_control)); |
adimmit | 0:76c761d3caf1 | 579 | memset(&q2_control, 0, sizeof(q2_control)); |
adimmit | 0:76c761d3caf1 | 580 | memset(&q3_control, 0, sizeof(q3_control)); |
adimmit | 0:76c761d3caf1 | 581 | |
adimmit | 0:76c761d3caf1 | 582 | |
adimmit | 0:76c761d3caf1 | 583 | q1_control.a.p_des = spi_command.q_des_1s[0]; |
adimmit | 0:76c761d3caf1 | 584 | q1_control.a.v_des = spi_command.qd_des_1s[0]; |
adimmit | 0:76c761d3caf1 | 585 | q1_control.a.kp = spi_command.kp_1s[0]; |
adimmit | 0:76c761d3caf1 | 586 | q1_control.a.kd = spi_command.kd_1s[0]; |
adimmit | 0:76c761d3caf1 | 587 | q1_control.a.t_ff = spi_command.tau_1s_ff[0]; |
adimmit | 0:76c761d3caf1 | 588 | |
adimmit | 0:76c761d3caf1 | 589 | q1_control.h.p_des = spi_command.q_des_2s[0]; |
adimmit | 0:76c761d3caf1 | 590 | q1_control.h.v_des = spi_command.qd_des_2s[0]; |
adimmit | 0:76c761d3caf1 | 591 | q1_control.h.kp = spi_command.kp_2s[0]; |
adimmit | 0:76c761d3caf1 | 592 | q1_control.h.kd = spi_command.kd_2s[0]; |
adimmit | 0:76c761d3caf1 | 593 | q1_control.h.t_ff = spi_command.tau_2s_ff[0]; |
adimmit | 0:76c761d3caf1 | 594 | |
adimmit | 0:76c761d3caf1 | 595 | q1_control.k.p_des = spi_command.q_des_3s[0]; |
adimmit | 0:76c761d3caf1 | 596 | q1_control.k.v_des = spi_command.qd_des_3s[0]; |
adimmit | 0:76c761d3caf1 | 597 | q1_control.k.kp = spi_command.kp_3s[0]; |
adimmit | 0:76c761d3caf1 | 598 | q1_control.k.kd = spi_command.kd_3s[0]; |
adimmit | 0:76c761d3caf1 | 599 | q1_control.k.t_ff = spi_command.tau_3s_ff[0]; |
adimmit | 0:76c761d3caf1 | 600 | |
adimmit | 0:76c761d3caf1 | 601 | q2_control.a.p_des = spi_command.q_des_1s[1]; |
adimmit | 0:76c761d3caf1 | 602 | q2_control.a.v_des = spi_command.qd_des_1s[1]; |
adimmit | 0:76c761d3caf1 | 603 | q2_control.a.kp = spi_command.kp_1s[1]; |
adimmit | 0:76c761d3caf1 | 604 | q2_control.a.kd = spi_command.kd_1s[1]; |
adimmit | 0:76c761d3caf1 | 605 | q2_control.a.t_ff = spi_command.tau_1s_ff[1]; |
adimmit | 0:76c761d3caf1 | 606 | |
adimmit | 0:76c761d3caf1 | 607 | q2_control.h.p_des = spi_command.q_des_2s[1]; |
adimmit | 0:76c761d3caf1 | 608 | q2_control.h.v_des = spi_command.qd_des_2s[1]; |
adimmit | 0:76c761d3caf1 | 609 | q2_control.h.kp = spi_command.kp_2s[1]; |
adimmit | 0:76c761d3caf1 | 610 | q2_control.h.kd = spi_command.kd_2s[1]; |
adimmit | 0:76c761d3caf1 | 611 | q2_control.h.t_ff = spi_command.tau_2s_ff[1]; |
adimmit | 0:76c761d3caf1 | 612 | |
adimmit | 0:76c761d3caf1 | 613 | q2_control.k.p_des = spi_command.q_des_3s[1]; |
adimmit | 0:76c761d3caf1 | 614 | q2_control.k.v_des = spi_command.qd_des_3s[1]; |
adimmit | 0:76c761d3caf1 | 615 | q2_control.k.kp = spi_command.kp_3s[1]; |
adimmit | 0:76c761d3caf1 | 616 | q2_control.k.kd = spi_command.kd_3s[1]; |
adimmit | 0:76c761d3caf1 | 617 | q2_control.k.t_ff = spi_command.tau_3s_ff[1]; |
adimmit | 0:76c761d3caf1 | 618 | |
adimmit | 0:76c761d3caf1 | 619 | q3_control.a.p_des = spi_command.q_des_1s[2]; |
adimmit | 0:76c761d3caf1 | 620 | q3_control.a.v_des = spi_command.qd_des_1s[2]; |
adimmit | 0:76c761d3caf1 | 621 | q3_control.a.kp = spi_command.kp_1s[2]; |
adimmit | 0:76c761d3caf1 | 622 | q3_control.a.kd = spi_command.kd_1s[2]; |
adimmit | 0:76c761d3caf1 | 623 | q3_control.a.t_ff = spi_command.tau_1s_ff[2]; |
adimmit | 0:76c761d3caf1 | 624 | |
adimmit | 0:76c761d3caf1 | 625 | q3_control.h.p_des = spi_command.q_des_2s[2]; |
adimmit | 0:76c761d3caf1 | 626 | q3_control.h.v_des = spi_command.qd_des_2s[2]; |
adimmit | 0:76c761d3caf1 | 627 | q3_control.h.kp = spi_command.kp_2s[2]; |
adimmit | 0:76c761d3caf1 | 628 | q3_control.h.kd = spi_command.kd_2s[2]; |
adimmit | 0:76c761d3caf1 | 629 | q3_control.h.t_ff = spi_command.tau_2s_ff[2]; |
adimmit | 0:76c761d3caf1 | 630 | |
adimmit | 0:76c761d3caf1 | 631 | q3_control.k.p_des = spi_command.q_des_3s[2]; |
adimmit | 0:76c761d3caf1 | 632 | q3_control.k.v_des = spi_command.qd_des_3s[2]; |
adimmit | 0:76c761d3caf1 | 633 | q3_control.k.kp = spi_command.kp_3s[2]; |
adimmit | 0:76c761d3caf1 | 634 | q3_control.k.kd = spi_command.kd_3s[2]; |
adimmit | 0:76c761d3caf1 | 635 | q3_control.k.t_ff = spi_command.tau_3s_ff[2]; |
adimmit | 0:76c761d3caf1 | 636 | |
adimmit | 0:76c761d3caf1 | 637 | |
adimmit | 0:76c761d3caf1 | 638 | spi_data.flags[0] = 0; |
adimmit | 0:76c761d3caf1 | 639 | spi_data.flags[1] = 0; |
adimmit | 0:76c761d3caf1 | 640 | spi_data.flags[2] = 0; |
adimmit | 0:76c761d3caf1 | 641 | spi_data.flags[0] |= softstop_joint(g1_state.a, &q1_control.a, A_LIM_P, A_LIM_N); |
adimmit | 0:76c761d3caf1 | 642 | spi_data.flags[0] |= (softstop_joint(g1_state.h, &q1_control.h, H_LIM_P, H_LIM_N))<<1; |
adimmit | 0:76c761d3caf1 | 643 | //spi_data.flags[0] |= (softstop_joint(g1_state.k, &q1_control.k, K_LIM_P, K_LIM_N))<<2; |
adimmit | 0:76c761d3caf1 | 644 | spi_data.flags[1] |= softstop_joint(g2_state.a, &q2_control.a, A_LIM_P, A_LIM_N); |
adimmit | 0:76c761d3caf1 | 645 | spi_data.flags[1] |= (softstop_joint(g2_state.h, &q2_control.h, H_LIM_P, H_LIM_N))<<1; |
adimmit | 0:76c761d3caf1 | 646 | //spi_data.flags[1] |= (softstop_joint(g2_state.k, &q2_control.k, K_LIM_P, K_LIM_N))<<2; |
adimmit | 0:76c761d3caf1 | 647 | spi_data.flags[2] |= softstop_joint(g3_state.a, &q3_control.a, A_LIM_P, A_LIM_N); |
adimmit | 0:76c761d3caf1 | 648 | spi_data.flags[2] |= (softstop_joint(g3_state.h, &q3_control.h, H_LIM_P, H_LIM_N))<<1; |
adimmit | 0:76c761d3caf1 | 649 | //spi_data.flags[2] |= (softstop_joint(g3_state.k, &q3_control.k, K_LIM_P, K_LIM_N))<<2; |
adimmit | 0:76c761d3caf1 | 650 | |
adimmit | 0:76c761d3caf1 | 651 | //spi_data.flags[0] = 0xbeef; |
adimmit | 0:76c761d3caf1 | 652 | //spi_data.flags[1] = 0xbeef; |
adimmit | 0:76c761d3caf1 | 653 | //PackAll(); |
adimmit | 0:76c761d3caf1 | 654 | //WriteAll(); |
adimmit | 0:76c761d3caf1 | 655 | } |
adimmit | 0:76c761d3caf1 | 656 | spi_data.checksum = xor_checksum((uint32_t*)&spi_data,14); |
adimmit | 0:76c761d3caf1 | 657 | for(int i = 0; i < DATA_LEN; i++){ |
adimmit | 0:76c761d3caf1 | 658 | tx_buff[i] = ((uint16_t*)(&spi_data))[i];} |
adimmit | 0:76c761d3caf1 | 659 | |
adimmit | 0:76c761d3caf1 | 660 | } |
adimmit | 0:76c761d3caf1 | 661 | |
adimmit | 0:76c761d3caf1 | 662 | |
adimmit | 0:76c761d3caf1 | 663 | void test_control() |
adimmit | 0:76c761d3caf1 | 664 | { |
adimmit | 0:76c761d3caf1 | 665 | for(int i = 0; i < 3; i++) |
adimmit | 0:76c761d3caf1 | 666 | { |
adimmit | 0:76c761d3caf1 | 667 | spi_data.q_1s[i] = spi_command.q_des_1s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 668 | spi_data.q_2s[i] = spi_command.q_des_2s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 669 | spi_data.q_3s[i] = spi_command.q_des_3s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 670 | |
adimmit | 0:76c761d3caf1 | 671 | spi_data.qd_1s[i] = spi_command.qd_des_1s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 672 | spi_data.qd_2s[i] = spi_command.qd_des_2s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 673 | spi_data.qd_3s[i] = spi_command.qd_des_3s[i] + 1.f; |
adimmit | 0:76c761d3caf1 | 674 | } |
adimmit | 0:76c761d3caf1 | 675 | |
adimmit | 0:76c761d3caf1 | 676 | spi_data.flags[0] = 0xdead; |
adimmit | 0:76c761d3caf1 | 677 | //spi_data.flags[1] = 0xbeef; |
adimmit | 0:76c761d3caf1 | 678 | |
adimmit | 0:76c761d3caf1 | 679 | // only do first 56 bytes of message. |
adimmit | 0:76c761d3caf1 | 680 | spi_data.checksum = xor_checksum((uint32_t*)&spi_data,14); |
adimmit | 0:76c761d3caf1 | 681 | |
adimmit | 0:76c761d3caf1 | 682 | for(int i = 0; i < DATA_LEN; i++) |
adimmit | 0:76c761d3caf1 | 683 | tx_buff[i] = ((uint16_t*)(&spi_data))[i]; |
adimmit | 0:76c761d3caf1 | 684 | } |
adimmit | 0:76c761d3caf1 | 685 | |
adimmit | 0:76c761d3caf1 | 686 | void init_spi(void){ |
adimmit | 0:76c761d3caf1 | 687 | SPISlave *spi = new SPISlave(PA_7, PA_6, PA_5, PA_4); |
adimmit | 0:76c761d3caf1 | 688 | spi->format(16, 0); |
adimmit | 0:76c761d3caf1 | 689 | spi->frequency(12000000); |
adimmit | 0:76c761d3caf1 | 690 | spi->reply(0x0); |
adimmit | 0:76c761d3caf1 | 691 | cs.fall(&spi_isr); |
adimmit | 1:aa253b5f5b65 | 692 | pc.printf("done\n\r"); |
adimmit | 0:76c761d3caf1 | 693 | } |
adimmit | 0:76c761d3caf1 | 694 | |
adimmit | 0:76c761d3caf1 | 695 | int main() { |
adimmit | 0:76c761d3caf1 | 696 | wait(1); |
adimmit | 0:76c761d3caf1 | 697 | //led = 1; |
adimmit | 1:aa253b5f5b65 | 698 | pc.baud(115200); //MAYBE CHANGE THIS IF NEEDED |
adimmit | 0:76c761d3caf1 | 699 | pc.attach(&serial_isr); |
adimmit | 0:76c761d3caf1 | 700 | estop.mode(PullUp); |
adimmit | 0:76c761d3caf1 | 701 | //spi.format(16, 0); |
adimmit | 0:76c761d3caf1 | 702 | //spi.frequency(1000000); |
adimmit | 0:76c761d3caf1 | 703 | //spi.reply(0x0); |
adimmit | 0:76c761d3caf1 | 704 | //cs.fall(&spi_isr); |
adimmit | 0:76c761d3caf1 | 705 | |
adimmit | 0:76c761d3caf1 | 706 | //can1.frequency(1000000); // set bit rate to 1Mbps |
adimmit | 0:76c761d3caf1 | 707 | //can1.attach(&rxISR1); // attach 'CAN receive-complete' interrupt handler |
adimmit | 0:76c761d3caf1 | 708 | can1.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); //set up can filter |
adimmit | 0:76c761d3caf1 | 709 | //can2.frequency(1000000); // set bit rate to 1Mbps |
adimmit | 0:76c761d3caf1 | 710 | //can2.attach(&rxISR2); // attach 'CAN receive-complete' interrupt handler |
adimmit | 0:76c761d3caf1 | 711 | can2.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); //set up can filter |
adimmit | 0:76c761d3caf1 | 712 | can3.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); //set up can filter |
adimmit | 0:76c761d3caf1 | 713 | |
adimmit | 0:76c761d3caf1 | 714 | memset(&tx_buff, 0, TX_LEN * sizeof(uint16_t)); |
adimmit | 0:76c761d3caf1 | 715 | memset(&spi_data, 0, sizeof(spi_data_t)); |
adimmit | 0:76c761d3caf1 | 716 | memset(&spi_command,0,sizeof(spi_command_t)); |
adimmit | 0:76c761d3caf1 | 717 | |
adimmit | 0:76c761d3caf1 | 718 | |
adimmit | 0:76c761d3caf1 | 719 | NVIC_SetPriority(TIM5_IRQn, 1); |
adimmit | 0:76c761d3caf1 | 720 | //NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
adimmit | 0:76c761d3caf1 | 721 | //NVIC_SetPriority(CAN2_RX0_IRQn, 3); |
adimmit | 0:76c761d3caf1 | 722 | |
adimmit | 0:76c761d3caf1 | 723 | pc.printf("\n\r SPIne\n\r"); |
adimmit | 0:76c761d3caf1 | 724 | //printf("%d\n\r", RX_ID << 18); |
adimmit | 0:76c761d3caf1 | 725 | |
adimmit | 0:76c761d3caf1 | 726 | q11_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 727 | q21_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 728 | q31_can.len = 8; |
adimmit | 0:76c761d3caf1 | 729 | q12_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 730 | q22_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 731 | q32_can.len = 8; |
adimmit | 0:76c761d3caf1 | 732 | q13_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 733 | q23_can.len = 8; //transmit 8 bytes |
adimmit | 0:76c761d3caf1 | 734 | q33_can.len = 8; |
adimmit | 0:76c761d3caf1 | 735 | rxMsg1.len = 6; //receive 6 bytes |
adimmit | 0:76c761d3caf1 | 736 | rxMsg2.len = 6; |
adimmit | 0:76c761d3caf1 | 737 | rxMsg3.len = 6; |
adimmit | 0:76c761d3caf1 | 738 | |
adimmit | 0:76c761d3caf1 | 739 | q11_can.id = 0x1; |
adimmit | 0:76c761d3caf1 | 740 | q21_can.id = 0x2; |
adimmit | 0:76c761d3caf1 | 741 | q31_can.id = 0x3; |
adimmit | 0:76c761d3caf1 | 742 | q12_can.id = 0x1; |
adimmit | 0:76c761d3caf1 | 743 | q22_can.id = 0x2; |
adimmit | 0:76c761d3caf1 | 744 | q32_can.id = 0x3; |
adimmit | 0:76c761d3caf1 | 745 | q13_can.id = 0x1; |
adimmit | 0:76c761d3caf1 | 746 | q23_can.id = 0x2; |
adimmit | 0:76c761d3caf1 | 747 | q33_can.id = 0x3; |
adimmit | 0:76c761d3caf1 | 748 | |
adimmit | 0:76c761d3caf1 | 749 | pack_cmd(&q11_can, q1_control.a); |
adimmit | 0:76c761d3caf1 | 750 | pack_cmd(&q12_can, q2_control.a); |
adimmit | 0:76c761d3caf1 | 751 | pack_cmd(&q13_can, q3_control.a); |
adimmit | 0:76c761d3caf1 | 752 | pack_cmd(&q21_can, q1_control.h); |
adimmit | 0:76c761d3caf1 | 753 | pack_cmd(&q22_can, q2_control.h); |
adimmit | 0:76c761d3caf1 | 754 | pack_cmd(&q23_can, q3_control.h); |
adimmit | 0:76c761d3caf1 | 755 | pack_cmd(&q31_can, q1_control.k); |
adimmit | 0:76c761d3caf1 | 756 | pack_cmd(&q32_can, q2_control.k); |
adimmit | 0:76c761d3caf1 | 757 | pack_cmd(&q33_can, q3_control.k); |
adimmit | 0:76c761d3caf1 | 758 | WriteAll(); |
adimmit | 1:aa253b5f5b65 | 759 | |
adimmit | 1:aa253b5f5b65 | 760 | //just debugging things |
adimmit | 1:aa253b5f5b65 | 761 | pc.printf("SETUP VARS ALL DONE\n"); |
adimmit | 0:76c761d3caf1 | 762 | |
adimmit | 0:76c761d3caf1 | 763 | |
adimmit | 0:76c761d3caf1 | 764 | // SPI doesn't work if enabled while the CS pin is pulled low |
adimmit | 0:76c761d3caf1 | 765 | // Wait for CS to not be low, then enable SPI |
adimmit | 0:76c761d3caf1 | 766 | if(!spi_enabled){ |
adimmit | 1:aa253b5f5b65 | 767 | while((spi_enabled==0) && (cs.read() ==0)){pc.printf("waiting for CS Pin\n"); wait_us(10);} |
adimmit | 0:76c761d3caf1 | 768 | init_spi(); |
adimmit | 0:76c761d3caf1 | 769 | spi_enabled = 1; |
adimmit | 1:aa253b5f5b65 | 770 | pc.printf("SPI ENABLED AND READY\n"); |
adimmit | 0:76c761d3caf1 | 771 | } |
adimmit | 0:76c761d3caf1 | 772 | |
adimmit | 0:76c761d3caf1 | 773 | //spi_command=set the thing here... |
adimmit | 0:76c761d3caf1 | 774 | |
adimmit | 0:76c761d3caf1 | 775 | while(1) { |
adimmit | 1:aa253b5f5b65 | 776 | //pc.printf("test, of SPINE\r\n"); |
adimmit | 0:76c761d3caf1 | 777 | counter++; |
adimmit | 0:76c761d3caf1 | 778 | can2.read(rxMsg2); |
adimmit | 0:76c761d3caf1 | 779 | unpack_reply(rxMsg2, &g2_state); |
adimmit | 0:76c761d3caf1 | 780 | can1.read(rxMsg1); // read message into Rx message storage |
adimmit | 0:76c761d3caf1 | 781 | unpack_reply(rxMsg1, &g1_state); |
adimmit | 0:76c761d3caf1 | 782 | can3.read(rxMsg3); // read message into Rx message storage |
adimmit | 0:76c761d3caf1 | 783 | unpack_reply(rxMsg3, &g3_state); |
adimmit | 0:76c761d3caf1 | 784 | wait_us(10); |
adimmit | 1:aa253b5f5b65 | 785 | |
adimmit | 1:aa253b5f5b65 | 786 | //print heatbeat (always will print message 0) |
adimmit | 2:32f69175c78e | 787 | /* |
adimmit | 1:aa253b5f5b65 | 788 | if (counter3 == 100000) { //for debugging purposes |
adimmit | 1:aa253b5f5b65 | 789 | pc.printf("HEARTBEAT\n"); |
adimmit | 1:aa253b5f5b65 | 790 | counter3 = 0; |
adimmit | 1:aa253b5f5b65 | 791 | } |
adimmit | 1:aa253b5f5b65 | 792 | counter3++; |
adimmit | 2:32f69175c78e | 793 | */ |
adimmit | 0:76c761d3caf1 | 794 | } |
adimmit | 2:32f69175c78e | 795 | |
adimmit | 0:76c761d3caf1 | 796 | } |