AK80-64 AK Series Actuator Driver Manual V1.0.8

Table Of Contents
https://www.cubemars.com/
51 / 52
msg->data[4] = kp_int&0xFF; // KP 8 bit lower
msg->data[5] = kd_int>>4; // Kd 8 bit higher
msg->data[6] = ((kd_int&0xF)<<4)|(kp_int>>8); //
KP 4 bit lower torque 4 bit higher
msg->data[7] = t_int&0xff; // torque 4 bit lower
}
When sending packets, all the numbers should be converted into integer numbers by the
following functions and then sent to the motor.
int float_to_uint(float x, float x_min, float x_max, unsigned int bits)
{
/// Converts a float to an unsigned int, given range and number of bits ///
float span = x_max - x_min;
if(x < x_min) x = x_min;
else if(x > x_max) x = x_max;
return (int) ((x- x_min)*((float)((1<<bits)/span)));
}
Receive routine code
void unpack_reply(CANMessage msg){
/// unpack ints from can buffer ///
int id = msg.data[0]; //驱动 ID
int p_int = (msg.data[1]<<8)|msg.data[2]; //Motor position data
int v_int = (msg.data[3]<<4)|(msg.data[4]>>4); // Motor speed data
int i_int = ((msg.data[4]&0xF)<<8)|msg.data[5]; // Motor torque data
/// convert ints to floats ///
float p = uint_to_float(p_int, P_MIN, P_MAX, 16);
float v = uint_to_float(v_int, V_MIN, V_MAX, 12);
float i = uint_to_float(i_int, -I_MAX, I_MAX, 12);
if(id == 1){
postion = p; //
Read the corresponding data according to the ID code
speed = v;
torque = i;
}
}
All numbers are converted to floating-point by the following function.
float uint_to_float(int x_int, float x_min, float x_max, int bits){