Skip to content

Commit

Permalink
Merge pull request westonrobot#45 from westonrobot/feature-bms_extend
Browse files Browse the repository at this point in the history
Feature bms extend
  • Loading branch information
rdu-weston authored Jan 15, 2024
2 parents ae2a120 + bcbfd43 commit b764db0
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 51 deletions.
10 changes: 4 additions & 6 deletions include/ugv_sdk/details/interface/agilex_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,10 @@ typedef struct {
#define BMS_PROT4_WEAK_SIGNAL_SWITCH_OPEN_SET_MASK ((uint8_t)0x10)

typedef struct {
uint8_t protection_code1;
uint8_t protection_code2;
uint8_t protection_code3;
uint8_t protection_code4;
uint8_t battery_max_teperature;
uint8_t battery_min_teperature;
uint8_t alarm_status_1;
uint8_t alarm_status_2;
uint8_t warn_status_1;
uint8_t warn_status_2;
} BmsExtendedMessage;

/************ Query/config messages ****************/
Expand Down
8 changes: 8 additions & 0 deletions include/ugv_sdk/details/interface/hunter_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ struct HunterActuatorState {
ActuatorStateMessageV1 actuator_state[3];
};

struct HunterCommonSensorState {
SdkTimePoint time_stamp;

BmsBasicMessage bms_basic_state;
BmsExtendedMessage bms_extend_state;
};

struct HunterInterface {
virtual ~HunterInterface() = default;

Expand All @@ -45,6 +52,7 @@ struct HunterInterface {
// get robot state
virtual HunterCoreState GetRobotState() = 0;
virtual HunterActuatorState GetActuatorState() = 0;
virtual HunterCommonSensorState GetCommonSensorState() = 0;
};
} // namespace westonrobot

Expand Down
8 changes: 7 additions & 1 deletion include/ugv_sdk/details/interface/ranger_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct RangerCoreState {

RcStateMessage rc_state;
OdometryMessage odometry;
BmsBasicMessage bms_basic_state;
};

struct RangerActuatorState {
Expand All @@ -40,6 +39,12 @@ struct RangerActuatorState {
ActuatorLSStateMessage actuator_ls_state[8];
};

struct RangerCommonSensorState {
SdkTimePoint time_stamp;

BmsBasicMessage bms_basic_state;
};

/////////////////////////////////////////////////////////////////////////

struct RangerInterface {
Expand All @@ -64,6 +69,7 @@ struct RangerInterface {
// get robot state
virtual RangerCoreState GetRobotState() = 0;
virtual RangerActuatorState GetActuatorState() = 0;
virtual RangerCommonSensorState GetCommonSensorState() = 0;
};
} // namespace westonrobot

Expand Down
7 changes: 6 additions & 1 deletion include/ugv_sdk/details/interface/scout_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct ScoutCoreState {
MotionStateMessage motion_state;
LightStateMessage light_state;
RcStateMessage rc_state;
BmsBasicMessage bms_basic_state;
};

struct ScoutActuatorState {
Expand All @@ -37,6 +36,12 @@ struct ScoutActuatorState {
ActuatorStateMessageV1 actuator_state[4];
};

struct ScoutCommonSensorState {
SdkTimePoint time_stamp;

BmsBasicMessage bms_basic_state;
};

struct ScoutInterface {
virtual ~ScoutInterface() = default;

Expand Down
31 changes: 18 additions & 13 deletions include/ugv_sdk/details/robot_base/agilex_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ struct CoreStateMsgGroup {
LightStateMessage light_state;
MotionModeStateMessage motion_mode_state;
RcStateMessage rc_state;

BmsBasicMessage bms_basic_state;
};

struct ActuatorStateMsgGroup {
Expand All @@ -46,8 +44,10 @@ struct ActuatorStateMsgGroup {
MotorSpeedMessage motor_speeds; // ranger only
};

struct SensorStateMsgGroup {
struct CommonSensorStateMsgGroup {
SdkTimePoint time_stamp;
BmsBasicMessage bms_basic_state;
BmsExtendedMessage bms_extend_state;
};

template <typename ParserType>
Expand Down Expand Up @@ -183,7 +183,7 @@ class AgilexBase : public RobotCommonInterface {
return actuator_state_msgs_;
}

SensorStateMsgGroup GetSensorStateMsgGroup() {
CommonSensorStateMsgGroup GetCommonSensorStateMsgGroup() {
std::lock_guard<std::mutex> guard(common_sensor_state_mtx_);
return common_sensor_state_msgs_;
}
Expand All @@ -205,7 +205,7 @@ class AgilexBase : public RobotCommonInterface {

/* feedback group 3: common sensor */
std::mutex common_sensor_state_mtx_;
SensorStateMsgGroup common_sensor_state_msgs_;
CommonSensorStateMsgGroup common_sensor_state_msgs_;

std::mutex version_str_buf_mtx_;
std::string version_string_buffer_;
Expand Down Expand Up @@ -279,7 +279,7 @@ class AgilexBase : public RobotCommonInterface {
if (parser_.DecodeMessage(rx_frame, &status_msg)) {
UpdateRobotCoreState(status_msg);
UpdateActuatorState(status_msg);
UpdateSensorState(status_msg);
UpdateCommonSensorState(status_msg);
UpdateResponseVersion(status_msg);
}
}
Expand Down Expand Up @@ -318,12 +318,6 @@ class AgilexBase : public RobotCommonInterface {
core_state_msgs_.rc_state = status_msg.body.rc_state_msg;
break;
}
case AgxMsgBmsBasic: {
// std::cout << "system status feedback received" << std::endl;
core_state_msgs_.time_stamp = SdkClock::now();
core_state_msgs_.bms_basic_state = status_msg.body.bms_basic_msg;
break;
}
default:
break;
}
Expand Down Expand Up @@ -381,11 +375,22 @@ class AgilexBase : public RobotCommonInterface {
}
}

void UpdateSensorState(const AgxMessage &status_msg) {
void UpdateCommonSensorState(const AgxMessage &status_msg) {
std::lock_guard<std::mutex> guard(common_sensor_state_mtx_);
// std::cout << common_sensor_state_msgs_.bms_basic_state.battery_soc<<
// std::endl;
switch (status_msg.type) {
case AgxMsgBmsBasic: {
// std::cout << "system status feedback received" << std::endl;
common_sensor_state_msgs_.time_stamp = SdkClock::now();
common_sensor_state_msgs_.bms_basic_state =
status_msg.body.bms_basic_msg;
break;
}
case AgxMsgBmsExtended: {
common_sensor_state_msgs_.bms_extend_state =
status_msg.body.bms_extended_msg;
}
default:
break;
}
Expand Down
31 changes: 31 additions & 0 deletions include/ugv_sdk/details/robot_base/hunter_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ class HunterBase : public AgilexBase<ParserType>, public HunterInterface {
return hunter_actuator;
}

HunterCommonSensorState GetCommonSensorState() override {
auto common_sensor =
AgilexBase<ParserType>::GetCommonSensorStateMsgGroup();

HunterCommonSensorState hunter_bms;

hunter_bms.time_stamp = common_sensor.time_stamp;

hunter_bms.bms_basic_state.current = common_sensor.bms_basic_state.current;
// Note: BMS CAN message definition is not consistent across AgileX robots.
// Robots with steering mechanism should additionally divide the voltage by
// 10.
hunter_bms.bms_basic_state.voltage =
common_sensor.bms_basic_state.voltage * 0.1f;
hunter_bms.bms_basic_state.battery_soc =
common_sensor.bms_basic_state.battery_soc;
hunter_bms.bms_basic_state.battery_soh =
common_sensor.bms_basic_state.battery_soh;
hunter_bms.bms_basic_state.temperature =
common_sensor.bms_basic_state.temperature;
hunter_bms.bms_extend_state.alarm_status_1 =
common_sensor.bms_extend_state.alarm_status_1;
hunter_bms.bms_extend_state.alarm_status_2 =
common_sensor.bms_extend_state.alarm_status_2;
hunter_bms.bms_extend_state.warn_status_1 =
common_sensor.bms_extend_state.warn_status_1;
hunter_bms.bms_extend_state.warn_status_2 =
common_sensor.bms_extend_state.warn_status_2;

return hunter_bms;
}
void ActivateBrake() override {
AgilexBase<ParserType>::SetBrakeMode(AgxBrakeMode::BRAKE_MODE_LOCK);
}
Expand Down
36 changes: 24 additions & 12 deletions include/ugv_sdk/details/robot_base/ranger_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ class RangerBaseV2 : public AgilexBase<ProtocolV2Parser>,
ranger_state.rc_state = state.rc_state;
ranger_state.motion_mode_state = state.motion_mode_state;

ranger_state.bms_basic_state.current = state.bms_basic_state.current;
// Note: BMS CAN message definition is not consistent across AgileX robots.
// Robots with steering mechanism should additionally divide the voltage by
// 10.
ranger_state.bms_basic_state.voltage = state.bms_basic_state.voltage * 0.1f;
ranger_state.bms_basic_state.battery_soc =
state.bms_basic_state.battery_soc;
ranger_state.bms_basic_state.battery_soh =
state.bms_basic_state.battery_soh;
ranger_state.bms_basic_state.temperature =
state.bms_basic_state.temperature;

return ranger_state;
}

Expand All @@ -99,6 +87,30 @@ class RangerBaseV2 : public AgilexBase<ProtocolV2Parser>,
}
return ranger_actuator;
}

RangerCommonSensorState GetCommonSensorState() override {
auto common_sensor =
AgilexBase<ProtocolV2Parser>::GetCommonSensorStateMsgGroup();

RangerCommonSensorState ranger_bms;

ranger_bms.time_stamp = common_sensor.time_stamp;

ranger_bms.bms_basic_state.current = common_sensor.bms_basic_state.current;
// Note: BMS CAN message definition is not consistent across AgileX robots.
// Robots with steering mechanism should additionally divide the voltage by
// 10.
ranger_bms.bms_basic_state.voltage =
common_sensor.bms_basic_state.voltage * 0.1f;
ranger_bms.bms_basic_state.battery_soc =
common_sensor.bms_basic_state.battery_soc;
ranger_bms.bms_basic_state.battery_soh =
common_sensor.bms_basic_state.battery_soh;
ranger_bms.bms_basic_state.temperature =
common_sensor.bms_basic_state.temperature;

return ranger_bms;
}
};

// Note: Ranger Mini V1 uses a modified AgileX V2 protocol
Expand Down
1 change: 0 additions & 1 deletion include/ugv_sdk/details/robot_base/scout_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class ScoutBase : public AgilexBase<ParserType>, public ScoutInterface {
scout_state.motion_state = state.motion_state;
scout_state.light_state = state.light_state;
scout_state.rc_state = state.rc_state;
scout_state.bms_basic_state = state.bms_basic_state;
return scout_state;
}

Expand Down
1 change: 1 addition & 0 deletions include/ugv_sdk/mobile_robot/hunter_robot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class HunterRobot : public RobotCommonInterface, public HunterInterface {
// get robot state
HunterCoreState GetRobotState() override;
HunterActuatorState GetActuatorState() override;
HunterCommonSensorState GetCommonSensorState() override;

private:
RobotCommonInterface* robot_;
Expand Down
1 change: 1 addition & 0 deletions include/ugv_sdk/mobile_robot/ranger_robot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RangerRobot : public RobotCommonInterface, public RangerInterface {
// get robot state
RangerCoreState GetRobotState() override;
RangerActuatorState GetActuatorState() override;
RangerCommonSensorState GetCommonSensorState() override;

private:
RobotCommonInterface* robot_;
Expand Down
6 changes: 4 additions & 2 deletions sample/ranger_demo/ranger_robot_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int main(int argc, char *argv[]) {

auto state = ranger->GetRobotState();
auto motion = ranger->GetActuatorState();
auto sensor = ranger->GetCommonSensorState();

std::cout << "-------------------------------" << std::endl;
std::cout << "count: " << count << std::endl;
Expand All @@ -58,9 +59,10 @@ int main(int argc, char *argv[]) {
<< static_cast<int>(state.system_state.vehicle_state)
<< std::endl;
std::cout << "battery voltage: " << state.system_state.battery_voltage
<< ", battery current: " << state.bms_basic_state.current
<< "battery voltage (BMS): " << sensor.bms_basic_state.voltage
<< ", battery current: " << sensor.bms_basic_state.current
<< ", SOC: "
<< static_cast<int>(state.bms_basic_state.battery_soc)
<< static_cast<int>(sensor.bms_basic_state.battery_soc)
<< std::endl;
std::cout << "velocity (linear, angular, lateral, steering): "
<< std::setw(6) << state.motion_state.linear_velocity << ", "
Expand Down
4 changes: 4 additions & 0 deletions src/mobile_robot/hunter_robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ HunterActuatorState HunterRobot::GetActuatorState() {
auto hunter = dynamic_cast<HunterInterface*>(robot_);
return hunter->GetActuatorState();
}
HunterCommonSensorState HunterRobot::GetCommonSensorState() {
auto hunter = dynamic_cast<HunterInterface*>(robot_);
return hunter->GetCommonSensorState();
}
} // namespace westonrobot
4 changes: 4 additions & 0 deletions src/mobile_robot/ranger_robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ RangerActuatorState RangerRobot::GetActuatorState() {
auto ranger = dynamic_cast<RangerInterface*>(robot_);
return ranger->GetActuatorState();
}
RangerCommonSensorState RangerRobot::GetCommonSensorState() {
auto ranger = dynamic_cast<RangerInterface*>(robot_);
return ranger->GetCommonSensorState();
}
} // namespace westonrobot
12 changes: 4 additions & 8 deletions src/protocol_v2/agilex_msg_parser_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,10 @@ bool DecodeCanFrameV2(const struct can_frame *rx_frame, AgxMessage *msg) {
case CAN_MSG_BMS_EXTENDED_ID: {
msg->type = AgxMsgBmsExtended;
BmsExtendedFrame *frame = (BmsExtendedFrame *)(rx_frame->data);
msg->body.bms_extended_msg.protection_code1 = frame->protection_code1;
msg->body.bms_extended_msg.protection_code2 = frame->protection_code2;
msg->body.bms_extended_msg.protection_code3 = frame->protection_code3;
msg->body.bms_extended_msg.protection_code4 = frame->protection_code4;
msg->body.bms_extended_msg.battery_max_teperature =
frame->battery_max_teperature;
msg->body.bms_extended_msg.battery_min_teperature =
frame->battery_min_teperature;
msg->body.bms_extended_msg.alarm_status_1 = frame->alarm_status_1;
msg->body.bms_extended_msg.alarm_status_2 = frame->alarm_status_2;
msg->body.bms_extended_msg.warn_status_1 = frame->warn_status_1;
msg->body.bms_extended_msg.warn_status_2 = frame->warn_status_2;
break;
}
/*************** query/config frame **************/
Expand Down
15 changes: 8 additions & 7 deletions src/protocol_v2/agilex_protocol_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,14 @@ typedef struct {
} BmsBasicFrame;

typedef struct {
uint8_t protection_code1;
uint8_t protection_code2;
uint8_t protection_code3;
uint8_t protection_code4;
uint8_t battery_max_teperature;
uint8_t battery_min_teperature;
struct16_t count;
uint8_t alarm_status_1;
uint8_t alarm_status_2;
uint8_t warn_status_1;
uint8_t warn_status_2;
uint8_t reserved0;
uint8_t reserved1;
uint8_t reserved2;
uint8_t reserved3;
} BmsExtendedFrame;

// query/config
Expand Down

0 comments on commit b764db0

Please sign in to comment.