00

Dependencies:   mcp2515 mbed-dev-f303

Committer:
yezhong
Date:
Tue Mar 22 02:03:58 2022 +0000
Revision:
8:1ab9699af5ae
Parent:
5:2503c88a564f
1

Who changed what in which revision?

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