Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CAN.cpp
00001 #include "CAN.h" 00002 #include "used_leg_message.h" 00003 00004 00005 00006 //定义设备 00007 CAN knee_can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name 00008 CAN ankle_can(PB_12, PB_13); // CAN Rx pin name, CAN Tx pin name 00009 // 定义CAN消息 00010 CANMessage knee_rxMsg, ankle_rxMsg; // 主控收到的CAN消息 00011 CANMessage knee_txMsg, ankle_txMsg; // 主控发送的CAN消息 00012 00013 //=================================函数=======================================// 00014 00015 /// 将控制参数打包入CAN消息中 00016 /// CAN Command Packet Structure 00017 /// 16 bit position command, between -4*pi and 4*pi 00018 /// 12 bit velocity command, between -30 and + 30 rad/s 00019 /// 12 bit kp, between 0 and 500 N-m/rad 00020 /// 12 bit kd, between 0 and 100 N-m*s/rad 00021 /// 12 bit feed forward torque, between -18 and 18 N-m 00022 /// CAN Packet is 8 8-bit words 00023 /// Formatted as follows. For each quantity, bit 0 is LSB 00024 /// 0: [position[15-8]] 00025 /// 1: [position[7-0]] 00026 /// 2: [velocity[11-4]] 00027 /// 3: [velocity[3-0], kp[11-8]] 00028 /// 4: [kp[7-0]] 00029 /// 5: [kd[11-4]] 00030 /// 6: [kd[3-0], torque[11-8]] 00031 /// 7: [torque[7-0]] 00032 void pack_cmd(CANMessage * msg, joint_control joint){ 00033 00034 /// limit data to be within bounds /// 00035 float p_des = fminf(fmaxf(P_MIN, joint.p_des), P_MAX); 00036 float v_des = fminf(fmaxf(V_MIN, joint.v_des), V_MAX); 00037 float kp = fminf(fmaxf(KP_MIN, joint.kp), KP_MAX); 00038 float kd = fminf(fmaxf(KD_MIN, joint.kd), KD_MAX); 00039 float t_ff = fminf(fmaxf(T_MIN, joint.t_ff), T_MAX); 00040 /// convert floats to unsigned ints /// 00041 uint16_t p_int = float_to_uint(p_des, P_MIN, P_MAX, 16); 00042 uint16_t v_int = float_to_uint(v_des, V_MIN, V_MAX, 12); 00043 uint16_t kp_int = float_to_uint(kp, KP_MIN, KP_MAX, 12); 00044 uint16_t kd_int = float_to_uint(kd, KD_MIN, KD_MAX, 12); 00045 uint16_t t_int = float_to_uint(t_ff, T_MIN, T_MAX, 12); 00046 /// pack ints into the can buffer /// 00047 msg->data[0] = p_int>>8; 00048 msg->data[1] = p_int&0xFF; 00049 msg->data[2] = v_int>>4; 00050 msg->data[3] = ((v_int&0xF)<<4)|(kp_int>>8); 00051 msg->data[4] = kp_int&0xFF; 00052 msg->data[5] = kd_int>>4; 00053 msg->data[6] = ((kd_int&0xF)<<4)|(t_int>>8); 00054 msg->data[7] = t_int&0xff; 00055 } 00056 00057 00058 // 多个控制器联合打包准备发送 00059 void PackAll(){ 00060 pack_cmd(&knee_txMsg, a_control.knee); 00061 pack_cmd(&ankle_txMsg, a_control.ankle); 00062 } 00063 00064 00065 // 写联合打包的数据 00066 void WriteAll(){ 00067 knee_can.write(knee_txMsg); 00068 wait(.00002); 00069 ankle_can.write(ankle_txMsg); 00070 wait(.00002); 00071 } 00072 00073 00074 /// 提取信息并存入状态结构体 00075 /// CAN Reply Packet Structure 00076 /// 16 bit position, between -4*pi and 4*pi 00077 /// 12 bit velocity, between -30 and + 30 rad/s 00078 /// 12 bit current, between -40 and 40; 00079 /// CAN Packet is 5 8-bit words 00080 /// Formatted as follows. For each quantity, bit 0 is LSB 00081 /// 0: [position[15-8]] 00082 /// 1: [position[7-0]] 00083 /// 2: [velocity[11-4]] 00084 /// 3: [velocity[3-0], current[11-8]] 00085 /// 4: [current[7-0]] 00086 void unpack_reply(CANMessage msg, leg_state * state){ 00087 /// unpack ints from can buffer /// 00088 uint16_t id = msg.data[0]; 00089 uint16_t p_int = (msg.data[1]<<8)|msg.data[2]; 00090 uint16_t v_int = (msg.data[3]<<4)|(msg.data[4]>>4); 00091 uint16_t i_int = ((msg.data[4]&0xF)<<8)|msg.data[5]; 00092 /// convert uints to floats /// 00093 float p = uint_to_float(p_int, P_MIN, P_MAX, 16); 00094 float v = uint_to_float(v_int, V_MIN, V_MAX, 12); 00095 float t = uint_to_float(i_int, -T_MAX, T_MAX, 12); 00096 00097 if(id==0x02){ 00098 state->knee_state.p = p; 00099 state->knee_state.v = v; 00100 state->knee_state.t = t; 00101 } 00102 else if(id==0x01){ 00103 state->ankle_state.p = p; 00104 state->ankle_state.v = v; 00105 state->ankle_state.t = t; 00106 } 00107 } 00108 00109 00110 00111 00112
Generated on Wed Jul 13 2022 08:32:47 by
