tested timeout

This commit is contained in:
Ruixiang Du
2020-09-15 11:00:16 +08:00
parent 0887261a99
commit a872dd0e0e
5 changed files with 119 additions and 109 deletions

View File

@@ -11,24 +11,19 @@
using namespace westonrobot; using namespace westonrobot;
int main(int argc, char **argv) int main(int argc, char **argv) {
{
std::string device_name; std::string device_name;
int32_t baud_rate = 0; int32_t baud_rate = 0;
if (argc == 2) if (argc == 2) {
{
device_name = {argv[1]}; device_name = {argv[1]};
std::cout << "Specified CAN: " << device_name << std::endl; std::cout << "Specified CAN: " << device_name << std::endl;
} } else if (argc == 3) {
else if (argc == 3)
{
device_name = {argv[1]}; device_name = {argv[1]};
baud_rate = std::stol(argv[2]); baud_rate = std::stol(argv[2]);
std::cout << "Specified serial: " << device_name << "@" << baud_rate << std::endl; std::cout << "Specified serial: " << device_name << "@" << baud_rate
} << std::endl;
else } else {
{
std::cout << "Usage: app_scout_demo <interface>" << std::endl std::cout << "Usage: app_scout_demo <interface>" << std::endl
<< "Example 1: ./app_scout_demo can0" << std::endl << "Example 1: ./app_scout_demo can0" << std::endl
<< "Example 2: ./app_scout_demo /dev/ttyUSB0 115200" << std::endl; << "Example 2: ./app_scout_demo /dev/ttyUSB0 115200" << std::endl;
@@ -38,77 +33,88 @@ int main(int argc, char **argv)
ScoutBase scout; ScoutBase scout;
scout.Connect(device_name, baud_rate); scout.Connect(device_name, baud_rate);
// light control // // light control
std::cout << "Light: const off" << std::endl; // std::cout << "Light: const off" << std::endl;
scout.SetLightCommand({ScoutLightCmd::LightMode::CONST_OFF, 0, ScoutLightCmd::LightMode::CONST_OFF, 0}); // scout.SetLightCommand({ScoutLightCmd::LightMode::CONST_OFF, 0,
sleep(3); // ScoutLightCmd::LightMode::CONST_OFF, 0});
std::cout << "Light: const on" << std::endl; // sleep(3);
scout.SetLightCommand({ScoutLightCmd::LightMode::CONST_ON, 0, ScoutLightCmd::LightMode::CONST_ON, 0}); // std::cout << "Light: const on" << std::endl;
sleep(3); // scout.SetLightCommand({ScoutLightCmd::LightMode::CONST_ON, 0,
std::cout << "Light: breath" << std::endl; // ScoutLightCmd::LightMode::CONST_ON, 0});
scout.SetLightCommand({ScoutLightCmd::LightMode::BREATH, 0, ScoutLightCmd::LightMode::BREATH, 0}); // sleep(3);
sleep(3); // std::cout << "Light: breath" << std::endl;
std::cout << "Light: custom 90-80" << std::endl; // scout.SetLightCommand({ScoutLightCmd::LightMode::BREATH, 0,
scout.SetLightCommand({ScoutLightCmd::LightMode::CUSTOM, 90, ScoutLightCmd::LightMode::CUSTOM, 80}); // ScoutLightCmd::LightMode::BREATH, 0});
sleep(3); // sleep(3);
std::cout << "Light: diabled cmd control" << std::endl; // std::cout << "Light: custom 90-80" << std::endl;
scout.DisableLightCmdControl(); // scout.SetLightCommand({ScoutLightCmd::LightMode::CUSTOM, 90,
// ScoutLightCmd::LightMode::CUSTOM, 80});
// sleep(3);
// std::cout << "Light: diabled cmd control" << std::endl;
// scout.DisableLightCmdControl();
int count = 0; int count = 0;
while (true) while (true) {
{
// motion control // motion control
if (count < 5) // if (count < 5)
{ // {
// std::cout << "Motor: 0.2, 0" << std::endl;
// scout.SetMotionCommand(0.2, 0.0);
// }
// else if (count < 10)
// {
// std::cout << "Motor: 0.8, 0" << std::endl;
// scout.SetMotionCommand(0.8, 0.0);
// }
// else if (count < 15)
// {
// std::cout << "Motor: 1.5, 0" << std::endl;
// scout.SetMotionCommand(1.5, 0.0);
// }
// else if (count < 20)
// {
// std::cout << "Motor: 1.0, 0.5" << std::endl;
// scout.SetMotionCommand(1.0, 0.5);
// }
// else if (count < 25)
// {
// std::cout << "Motor: 0.0, 0" << std::endl;
// scout.SetMotionCommand(0.0, 0.0);
// }
// else if (count < 30)
// {
// std::cout << "Motor: -0.5, 0" << std::endl;
// scout.SetMotionCommand(-0.5, 0.0);
// }
// else if (count < 35)
// {
// std::cout << "Motor: -1.0, -0.5" << std::endl;
// scout.SetMotionCommand(-1.0, -0.5);
// }
// else if (count < 40)
// {
// std::cout << "Motor: 0.0, 0, Light: breath" << std::endl;
// scout.SetMotionCommand(0.0, 0.0);
// scout.SetLightCommand({ScoutLightCmd::LightMode::BREATH, 0,
// ScoutLightCmd::LightMode::BREATH, 0});
// }
if (count < 250) {
std::cout << "Motor: 0.2, 0" << std::endl; std::cout << "Motor: 0.2, 0" << std::endl;
scout.SetMotionCommand(0.2, 0.0); scout.SetMotionCommand(0.2, 0.0);
} }
else if (count < 10)
{
std::cout << "Motor: 0.8, 0" << std::endl;
scout.SetMotionCommand(0.8, 0.0);
}
else if (count < 15)
{
std::cout << "Motor: 1.5, 0" << std::endl;
scout.SetMotionCommand(1.5, 0.0);
}
else if (count < 20)
{
std::cout << "Motor: 1.0, 0.5" << std::endl;
scout.SetMotionCommand(1.0, 0.5);
}
else if (count < 25)
{
std::cout << "Motor: 0.0, 0" << std::endl;
scout.SetMotionCommand(0.0, 0.0);
}
else if (count < 30)
{
std::cout << "Motor: -0.5, 0" << std::endl;
scout.SetMotionCommand(-0.5, 0.0);
}
else if (count < 35)
{
std::cout << "Motor: -1.0, -0.5" << std::endl;
scout.SetMotionCommand(-1.0, -0.5);
}
else if (count < 40)
{
std::cout << "Motor: 0.0, 0, Light: breath" << std::endl;
scout.SetMotionCommand(0.0, 0.0);
scout.SetLightCommand({ScoutLightCmd::LightMode::BREATH, 0, ScoutLightCmd::LightMode::BREATH, 0});
}
auto state = scout.GetScoutState(); auto state = scout.GetScoutState();
std::cout << "-------------------------------" << std::endl; std::cout << "-------------------------------" << std::endl;
std::cout << "count: " << count << std::endl; std::cout << "count: " << count << std::endl;
std::cout << "control mode: " << static_cast<int>(state.control_mode) << " , base state: " << static_cast<int>(state.base_state) << std::endl; std::cout << "control mode: " << static_cast<int>(state.control_mode)
<< " , base state: " << static_cast<int>(state.base_state)
<< std::endl;
std::cout << "battery voltage: " << state.battery_voltage << std::endl; std::cout << "battery voltage: " << state.battery_voltage << std::endl;
std::cout << "velocity (linear, angular): " << state.linear_velocity << ", " << state.angular_velocity << std::endl; std::cout << "velocity (linear, angular): " << state.linear_velocity << ", "
<< state.angular_velocity << std::endl;
std::cout << "-------------------------------" << std::endl; std::cout << "-------------------------------" << std::endl;
sleep(1); usleep(20000);
++count; ++count;
} }

View File

@@ -54,7 +54,7 @@ class MobileBase {
// timeout to be implemented in each vehicle // timeout to be implemented in each vehicle
bool enable_timeout_ = true; bool enable_timeout_ = true;
uint32_t timeout_ms_ = 100; uint32_t timeout_ms_ = 500;
uint32_t watchdog_counter_ = 0; uint32_t watchdog_counter_ = 0;
void FeedCmdTimeoutWatchdog() { watchdog_counter_ = 0; }; void FeedCmdTimeoutWatchdog() { watchdog_counter_ = 0; };

View File

@@ -17,7 +17,6 @@ namespace westonrobot {
void HunterBase::SendRobotCmd() { void HunterBase::SendRobotCmd() {
static uint8_t cmd_count = 0; static uint8_t cmd_count = 0;
SendMotionCmd(cmd_count++); SendMotionCmd(cmd_count++);
FeedCmdTimeoutWatchdog();
} }
void HunterBase::SendMotionCmd(uint8_t count) { void HunterBase::SendMotionCmd(uint8_t count) {
@@ -81,6 +80,8 @@ void HunterBase::SetMotionCommand(
current_motion_cmd_.angular_velocity = static_cast<int8_t>( current_motion_cmd_.angular_velocity = static_cast<int8_t>(
steering_angle / HunterMotionCmd::max_steering_angle * 100.0); steering_angle / HunterMotionCmd::max_steering_angle * 100.0);
current_motion_cmd_.fault_clear_flag = fault_clr_flag; current_motion_cmd_.fault_clear_flag = fault_clr_flag;
FeedCmdTimeoutWatchdog();
} }
void HunterBase::ParseCANFrame(can_frame *rx_frame) { void HunterBase::ParseCANFrame(can_frame *rx_frame) {

View File

@@ -74,14 +74,16 @@ void MobileBase::ControlLoop(int32_t period_ms) {
timeout_iter_num = static_cast<uint32_t>(timeout_ms_ / period_ms); timeout_iter_num = static_cast<uint32_t>(timeout_ms_ / period_ms);
std::cout << "Timeout iteration number: " << timeout_iter_num << std::endl; std::cout << "Timeout iteration number: " << timeout_iter_num << std::endl;
} }
while (true) { while (true) {
ctrl_sw.tic(); ctrl_sw.tic();
if (enable_timeout_) { if (enable_timeout_) {
++watchdog_counter_; if (watchdog_counter_ < timeout_iter_num) {
if (watchdog_counter_ < timeout_iter_num)
SendRobotCmd(); SendRobotCmd();
else ++watchdog_counter_;
} else {
std::cout << "Warning: cmd timeout, not sent to robot" << std::endl; std::cout << "Warning: cmd timeout, not sent to robot" << std::endl;
}
} else { } else {
SendRobotCmd(); SendRobotCmd();
} }

View File

@@ -18,7 +18,6 @@ void ScoutBase::SendRobotCmd() {
static uint8_t light_cmd_count = 0; static uint8_t light_cmd_count = 0;
SendMotionCmd(cmd_count++); SendMotionCmd(cmd_count++);
if (light_ctrl_requested_) SendLightCmd(light_cmd_count++); if (light_ctrl_requested_) SendLightCmd(light_cmd_count++);
FeedCmdTimeoutWatchdog();
} }
void ScoutBase::SendMotionCmd(uint8_t count) { void ScoutBase::SendMotionCmd(uint8_t count) {
@@ -56,12 +55,12 @@ void ScoutBase::SendMotionCmd(uint8_t count) {
can_frame m_frame; can_frame m_frame;
EncodeScoutMsgToCAN(&m_msg, &m_frame); EncodeScoutMsgToCAN(&m_msg, &m_frame);
can_if_->SendFrame(m_frame); can_if_->SendFrame(m_frame);
std::cout << "CAN cmd sent" << std::endl; // std::cout << "CAN cmd sent" << std::endl;
} else { } else {
// send to serial port // send to serial port
EncodeScoutMsgToUART(&m_msg, tx_buffer_, &tx_cmd_len_); EncodeScoutMsgToUART(&m_msg, tx_buffer_, &tx_cmd_len_);
serial_if_->SendBytes(tx_buffer_, tx_cmd_len_); serial_if_->SendBytes(tx_buffer_, tx_cmd_len_);
std::cout << "serial cmd sent" << std::endl; // std::cout << "serial cmd sent" << std::endl;
} }
} }
@@ -161,6 +160,8 @@ void ScoutBase::SetMotionCommand(
current_motion_cmd_.angular_velocity = static_cast<int8_t>( current_motion_cmd_.angular_velocity = static_cast<int8_t>(
angular_vel / ScoutMotionCmd::max_angular_velocity * 100.0); angular_vel / ScoutMotionCmd::max_angular_velocity * 100.0);
current_motion_cmd_.fault_clear_flag = fault_clr_flag; current_motion_cmd_.fault_clear_flag = fault_clr_flag;
FeedCmdTimeoutWatchdog();
} }
void ScoutBase::SetLightCommand(ScoutLightCmd cmd) { void ScoutBase::SetLightCommand(ScoutLightCmd cmd) {