mirror of
https://github.com/odriverobotics/ODrive.git
synced 2026-09-21 23:44:48 +08:00
Create new functions without float or offset
This commit is contained in:
@@ -41,7 +41,7 @@ enum InputMode_t {
|
||||
|
||||
// Fetch a specific signal from the message
|
||||
template <typename T>
|
||||
float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel) {
|
||||
uint64_t tempVal = 0;
|
||||
uint64_t mask = (1ULL << length) - 1;
|
||||
|
||||
@@ -56,16 +56,21 @@ float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t len
|
||||
|
||||
T retVal;
|
||||
std::memcpy(&retVal, &tempVal, sizeof(T));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T retVal = can_getSignal<T>(msg, startBit, length, isIntel);
|
||||
return (retVal * factor) + offset;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void can_setSignal(can_Message_t& msg, const T& val, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T scaledVal = (val - offset) / factor;
|
||||
uint64_t valAsBits = 0;
|
||||
std::memcpy(&valAsBits, &scaledVal, sizeof(scaledVal));
|
||||
|
||||
template<typename T>
|
||||
void can_setSignal(can_Message_t& msg, const T& val, const uint8_t startBit, const uint8_t length, const bool isIntel){
|
||||
uint64_t mask = (1ULL << length) - 1;
|
||||
uint64_t valAsBits = 0;
|
||||
std::memcpy(&valAsBits, &val, sizeof(T));
|
||||
|
||||
|
||||
if (isIntel) {
|
||||
uint64_t data = 0;
|
||||
@@ -88,6 +93,12 @@ void can_setSignal(can_Message_t& msg, const T& val, const uint8_t startBit, con
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void can_setSignal(can_Message_t& msg, const T& val, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T scaledVal = (val - offset) / factor;
|
||||
can_setSignal<T>(msg, scaledVal, startBit, length, isIntel);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
float can_getSignal(can_Message_t msg, const can_Signal_t& signal) {
|
||||
return can_getSignal<T>(msg, signal.startBit, signal.length, signal.isIntel, signal.factor, signal.offset);
|
||||
|
||||
@@ -185,7 +185,7 @@ void CANSimple::set_axis_nodeid_callback(Axis* axis, can_Message_t& msg) {
|
||||
}
|
||||
|
||||
void CANSimple::set_axis_requested_state_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->requested_state_ = static_cast<Axis::State_t>(can_getSignal<int16_t>(msg, 0, 16, true, 1, 0));
|
||||
axis->requested_state_ = static_cast<Axis::State_t>(can_getSignal<int32_t>(msg, 0, 16, true));
|
||||
}
|
||||
void CANSimple::set_axis_startup_config_callback(Axis* axis, can_Message_t& msg) {
|
||||
// Not Implemented
|
||||
@@ -276,7 +276,7 @@ void CANSimple::get_encoder_count_callback(Axis* axis, can_Message_t& msg) {
|
||||
}
|
||||
|
||||
void CANSimple::set_input_pos_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->controller_.input_pos_ = can_getSignal<int32_t>(msg, 0, 32, true, 1, 0);
|
||||
axis->controller_.input_pos_ = can_getSignal<int32_t>(msg, 0, 32, true);
|
||||
axis->controller_.input_vel_ = can_getSignal<int16_t>(msg, 32, 16, true, 0.1f, 0);
|
||||
axis->controller_.input_current_ = can_getSignal<int16_t>(msg, 48, 16, true, 0.01f, 0);
|
||||
axis->controller_.input_pos_updated();
|
||||
@@ -292,12 +292,12 @@ void CANSimple::set_input_current_callback(Axis* axis, can_Message_t& msg) {
|
||||
}
|
||||
|
||||
void CANSimple::set_controller_modes_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->controller_.config_.control_mode = static_cast<Controller::ControlMode_t>(can_getSignal<int32_t>(msg, 0, 32, true, 1, 0));
|
||||
axis->controller_.config_.input_mode = static_cast<Controller::InputMode_t>(can_getSignal<int32_t>(msg, 32, 32, true, 1, 0));
|
||||
axis->controller_.config_.control_mode = static_cast<Controller::ControlMode_t>(can_getSignal<int32_t>(msg, 0, 32, true));
|
||||
axis->controller_.config_.input_mode = static_cast<Controller::InputMode_t>(can_getSignal<int32_t>(msg, 32, 32, true));
|
||||
}
|
||||
|
||||
void CANSimple::set_vel_limit_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->controller_.config_.vel_limit = can_getSignal<float>(msg, 0, 32, true, 1, 0);
|
||||
axis->controller_.config_.vel_limit = can_getSignal<float>(msg, 0, 32, true);
|
||||
}
|
||||
|
||||
void CANSimple::start_anticogging_callback(Axis* axis, can_Message_t& msg) {
|
||||
@@ -305,16 +305,16 @@ void CANSimple::start_anticogging_callback(Axis* axis, can_Message_t& msg) {
|
||||
}
|
||||
|
||||
void CANSimple::set_traj_vel_limit_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->trap_.config_.vel_limit = can_getSignal<float>(msg, 0, 32, true, 1, 0);
|
||||
axis->trap_.config_.vel_limit = can_getSignal<float>(msg, 0, 32, true);
|
||||
}
|
||||
|
||||
void CANSimple::set_traj_accel_limits_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->trap_.config_.accel_limit = can_getSignal<float>(msg, 0, 32, true, 1, 0);
|
||||
axis->trap_.config_.decel_limit = can_getSignal<float>(msg, 32, 32, true, 1, 0);
|
||||
axis->trap_.config_.accel_limit = can_getSignal<float>(msg, 0, 32, true);
|
||||
axis->trap_.config_.decel_limit = can_getSignal<float>(msg, 32, 32, true);
|
||||
}
|
||||
|
||||
void CANSimple::set_traj_A_per_css_callback(Axis* axis, can_Message_t& msg) {
|
||||
axis->controller_.config_.inertia = can_getSignal<float>(msg, 0, 32, true, 1, 0);
|
||||
axis->controller_.config_.inertia = can_getSignal<float>(msg, 0, 32, true);
|
||||
}
|
||||
|
||||
void CANSimple::get_iq_callback(Axis* axis, can_Message_t& msg) {
|
||||
|
||||
@@ -88,7 +88,7 @@ class ODriveCAN {
|
||||
|
||||
#include <iterator>
|
||||
template <typename T>
|
||||
float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel) {
|
||||
uint64_t tempVal = 0;
|
||||
uint64_t mask = (1ULL << length) - 1;
|
||||
|
||||
@@ -103,6 +103,12 @@ float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t len
|
||||
|
||||
T retVal;
|
||||
std::memcpy(&retVal, &tempVal, sizeof(T));
|
||||
return retVal;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
float can_getSignal(can_Message_t msg, const uint8_t startBit, const uint8_t length, const bool isIntel, const float factor, const float offset) {
|
||||
T retVal = can_getSignal<T>(msg, startBit, length, isIntel);
|
||||
return (retVal * factor) + offset;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user