1
Dependencies: mcp2515 mbed-dev-f303
CAN/CAN.cpp@3:940a9e40d327, 2021-06-30 (annotated)
- Committer:
- yezhong
- Date:
- Wed Jun 30 12:15:37 2021 +0000
- Revision:
- 3:940a9e40d327
- Parent:
- 2:cd74a8cb03b0
- Child:
- 4:2503c88a564f
111
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
panzhan | 0:d80c66cb1b3a | 1 | #include "CAN.h" |
panzhan | 0:d80c66cb1b3a | 2 | #include "used_leg_message.h" |
panzhan | 0:d80c66cb1b3a | 3 | |
panzhan | 0:d80c66cb1b3a | 4 | |
panzhan | 0:d80c66cb1b3a | 5 | |
panzhan | 0:d80c66cb1b3a | 6 | //定义设备 |
yezhong | 2:cd74a8cb03b0 | 7 | CAN pf_can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
yezhong | 2:cd74a8cb03b0 | 8 | CAN df_can(PB_12, PB_13); // CAN Rx pin name, CAN Tx pin name |
panzhan | 0:d80c66cb1b3a | 9 | // 定义CAN消息 |
yezhong | 2:cd74a8cb03b0 | 10 | CANMessage pf_rxMsg, df_rxMsg; // 主控收到的CAN消息 |
yezhong | 2:cd74a8cb03b0 | 11 | CANMessage PF_can, DF_can; // 主控发送的CAN消息 |
panzhan | 0:d80c66cb1b3a | 12 | |
panzhan | 0:d80c66cb1b3a | 13 | //=================================函数=======================================// |
panzhan | 0:d80c66cb1b3a | 14 | |
panzhan | 0:d80c66cb1b3a | 15 | /// 将控制参数打包入CAN消息中 |
panzhan | 0:d80c66cb1b3a | 16 | /// CAN Command Packet Structure |
panzhan | 0:d80c66cb1b3a | 17 | /// 16 bit position command, between -4*pi and 4*pi |
panzhan | 0:d80c66cb1b3a | 18 | /// 12 bit velocity command, between -30 and + 30 rad/s |
panzhan | 0:d80c66cb1b3a | 19 | /// 12 bit kp, between 0 and 500 N-m/rad |
panzhan | 0:d80c66cb1b3a | 20 | /// 12 bit kd, between 0 and 100 N-m*s/rad |
panzhan | 0:d80c66cb1b3a | 21 | /// 12 bit feed forward torque, between -18 and 18 N-m |
panzhan | 0:d80c66cb1b3a | 22 | /// CAN Packet is 8 8-bit words |
panzhan | 0:d80c66cb1b3a | 23 | /// Formatted as follows. For each quantity, bit 0 is LSB |
panzhan | 0:d80c66cb1b3a | 24 | /// 0: [position[15-8]] |
panzhan | 0:d80c66cb1b3a | 25 | /// 1: [position[7-0]] |
panzhan | 0:d80c66cb1b3a | 26 | /// 2: [velocity[11-4]] |
panzhan | 0:d80c66cb1b3a | 27 | /// 3: [velocity[3-0], kp[11-8]] |
panzhan | 0:d80c66cb1b3a | 28 | /// 4: [kp[7-0]] |
panzhan | 0:d80c66cb1b3a | 29 | /// 5: [kd[11-4]] |
panzhan | 0:d80c66cb1b3a | 30 | /// 6: [kd[3-0], torque[11-8]] |
panzhan | 0:d80c66cb1b3a | 31 | /// 7: [torque[7-0]] |
panzhan | 0:d80c66cb1b3a | 32 | void pack_cmd(CANMessage * msg, joint_control joint){ |
panzhan | 0:d80c66cb1b3a | 33 | |
panzhan | 0:d80c66cb1b3a | 34 | /// limit data to be within bounds /// |
panzhan | 0:d80c66cb1b3a | 35 | float p_des = fminf(fmaxf(P_MIN, joint.p_des), P_MAX); |
panzhan | 0:d80c66cb1b3a | 36 | float v_des = fminf(fmaxf(V_MIN, joint.v_des), V_MAX); |
panzhan | 0:d80c66cb1b3a | 37 | float kp = fminf(fmaxf(KP_MIN, joint.kp), KP_MAX); |
panzhan | 0:d80c66cb1b3a | 38 | float kd = fminf(fmaxf(KD_MIN, joint.kd), KD_MAX); |
panzhan | 0:d80c66cb1b3a | 39 | float t_ff = fminf(fmaxf(T_MIN, joint.t_ff), T_MAX); |
panzhan | 0:d80c66cb1b3a | 40 | /// convert floats to unsigned ints /// |
panzhan | 0:d80c66cb1b3a | 41 | uint16_t p_int = float_to_uint(p_des, P_MIN, P_MAX, 16); |
panzhan | 0:d80c66cb1b3a | 42 | uint16_t v_int = float_to_uint(v_des, V_MIN, V_MAX, 12); |
panzhan | 0:d80c66cb1b3a | 43 | uint16_t kp_int = float_to_uint(kp, KP_MIN, KP_MAX, 12); |
panzhan | 0:d80c66cb1b3a | 44 | uint16_t kd_int = float_to_uint(kd, KD_MIN, KD_MAX, 12); |
panzhan | 0:d80c66cb1b3a | 45 | uint16_t t_int = float_to_uint(t_ff, T_MIN, T_MAX, 12); |
panzhan | 0:d80c66cb1b3a | 46 | /// pack ints into the can buffer /// |
panzhan | 0:d80c66cb1b3a | 47 | msg->data[0] = p_int>>8; |
panzhan | 0:d80c66cb1b3a | 48 | msg->data[1] = p_int&0xFF; |
panzhan | 0:d80c66cb1b3a | 49 | msg->data[2] = v_int>>4; |
panzhan | 0:d80c66cb1b3a | 50 | msg->data[3] = ((v_int&0xF)<<4)|(kp_int>>8); |
panzhan | 0:d80c66cb1b3a | 51 | msg->data[4] = kp_int&0xFF; |
panzhan | 0:d80c66cb1b3a | 52 | msg->data[5] = kd_int>>4; |
panzhan | 0:d80c66cb1b3a | 53 | msg->data[6] = ((kd_int&0xF)<<4)|(t_int>>8); |
panzhan | 0:d80c66cb1b3a | 54 | msg->data[7] = t_int&0xff; |
panzhan | 0:d80c66cb1b3a | 55 | } |
panzhan | 0:d80c66cb1b3a | 56 | |
panzhan | 0:d80c66cb1b3a | 57 | |
panzhan | 0:d80c66cb1b3a | 58 | // 多个控制器联合打包准备发送 |
panzhan | 0:d80c66cb1b3a | 59 | void PackAll(){ |
yezhong | 2:cd74a8cb03b0 | 60 | pack_cmd(&PF_can, a_control.pf); |
yezhong | 2:cd74a8cb03b0 | 61 | pack_cmd(&DF_can, a_control.df); |
panzhan | 0:d80c66cb1b3a | 62 | } |
panzhan | 0:d80c66cb1b3a | 63 | |
panzhan | 0:d80c66cb1b3a | 64 | |
panzhan | 0:d80c66cb1b3a | 65 | // 写联合打包的数据 |
panzhan | 0:d80c66cb1b3a | 66 | void WriteAll(){ |
yezhong | 2:cd74a8cb03b0 | 67 | pf_can.write(PF_can); |
panzhan | 0:d80c66cb1b3a | 68 | wait(.00002); |
yezhong | 2:cd74a8cb03b0 | 69 | df_can.write(DF_can); |
panzhan | 0:d80c66cb1b3a | 70 | wait(.00002); |
panzhan | 0:d80c66cb1b3a | 71 | } |
panzhan | 0:d80c66cb1b3a | 72 | |
panzhan | 0:d80c66cb1b3a | 73 | |
panzhan | 0:d80c66cb1b3a | 74 | /// 提取信息并存入状态结构体 |
panzhan | 0:d80c66cb1b3a | 75 | /// CAN Reply Packet Structure |
panzhan | 0:d80c66cb1b3a | 76 | /// 16 bit position, between -4*pi and 4*pi |
panzhan | 0:d80c66cb1b3a | 77 | /// 12 bit velocity, between -30 and + 30 rad/s |
panzhan | 0:d80c66cb1b3a | 78 | /// 12 bit current, between -40 and 40; |
panzhan | 0:d80c66cb1b3a | 79 | /// CAN Packet is 5 8-bit words |
panzhan | 0:d80c66cb1b3a | 80 | /// Formatted as follows. For each quantity, bit 0 is LSB |
panzhan | 0:d80c66cb1b3a | 81 | /// 0: [position[15-8]] |
panzhan | 0:d80c66cb1b3a | 82 | /// 1: [position[7-0]] |
panzhan | 0:d80c66cb1b3a | 83 | /// 2: [velocity[11-4]] |
panzhan | 0:d80c66cb1b3a | 84 | /// 3: [velocity[3-0], current[11-8]] |
panzhan | 0:d80c66cb1b3a | 85 | /// 4: [current[7-0]] |
yezhong | 2:cd74a8cb03b0 | 86 | void unpack_reply(CANMessage msg, ankle_state * ankle){ |
panzhan | 0:d80c66cb1b3a | 87 | /// unpack ints from can buffer /// |
panzhan | 0:d80c66cb1b3a | 88 | uint16_t id = msg.data[0]; |
panzhan | 0:d80c66cb1b3a | 89 | uint16_t p_int = (msg.data[1]<<8)|msg.data[2]; |
panzhan | 0:d80c66cb1b3a | 90 | uint16_t v_int = (msg.data[3]<<4)|(msg.data[4]>>4); |
panzhan | 0:d80c66cb1b3a | 91 | uint16_t i_int = ((msg.data[4]&0xF)<<8)|msg.data[5]; |
panzhan | 0:d80c66cb1b3a | 92 | /// convert uints to floats /// |
panzhan | 0:d80c66cb1b3a | 93 | float p = uint_to_float(p_int, P_MIN, P_MAX, 16); |
panzhan | 0:d80c66cb1b3a | 94 | float v = uint_to_float(v_int, V_MIN, V_MAX, 12); |
panzhan | 0:d80c66cb1b3a | 95 | float t = uint_to_float(i_int, -T_MAX, T_MAX, 12); |
yezhong | 2:cd74a8cb03b0 | 96 | |
panzhan | 0:d80c66cb1b3a | 97 | |
yezhong | 3:940a9e40d327 | 98 | if(id==0x03){ |
yezhong | 2:cd74a8cb03b0 | 99 | ankle->pf.p = p; |
yezhong | 2:cd74a8cb03b0 | 100 | ankle->pf.v = v; |
yezhong | 2:cd74a8cb03b0 | 101 | ankle->pf.t = t; |
panzhan | 0:d80c66cb1b3a | 102 | } |
yezhong | 3:940a9e40d327 | 103 | else if(id==0x03){ |
yezhong | 2:cd74a8cb03b0 | 104 | ankle->df.p = p; |
yezhong | 2:cd74a8cb03b0 | 105 | ankle->df.v = v; |
yezhong | 2:cd74a8cb03b0 | 106 | ankle->df.t = t; |
panzhan | 0:d80c66cb1b3a | 107 | } |
panzhan | 0:d80c66cb1b3a | 108 | } |
panzhan | 0:d80c66cb1b3a | 109 | |
panzhan | 0:d80c66cb1b3a | 110 | |
panzhan | 0:d80c66cb1b3a | 111 | |
panzhan | 0:d80c66cb1b3a | 112 | |
panzhan | 0:d80c66cb1b3a | 113 |