13 #include <spdlog/spdlog.h>
36 std::atomic<bool> g_stop_sched = {
false};
43 static unsigned int loop_counter = 0;
48 throw std::runtime_error(
49 "PeriodicTask: Fault occurred on the connected robot, exiting ...");
53 switch (loop_counter % 1000) {
56 "Sending benchmark signal to both workstation PC's serial "
57 "port and robot server's digital out port[0]");
65 auto n = write(g_fd,
"0", 1);
67 spdlog::error(
"Failed to write to serial port");
74 robot.
SetDigitalOutputs(std::vector<unsigned int> {0}, std::vector<bool> {
false});
82 }
catch (
const std::exception& e) {
83 spdlog::error(e.what());
91 std::cout <<
"Required arguments: [robot_sn] [serial_port_name]" << std::endl;
92 std::cout <<
" robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
93 std::cout <<
" serial_port_name: /dev/ttyS0 for COM1, /dev/ttyS1 for COM2, /dev/ttyUSB0 for USB-serial converter" << std::endl;
94 std::cout <<
"Optional arguments: None" << std::endl;
95 std::cout << std::endl;
99 int main(
int argc,
char* argv[])
103 if (argc < 3 || flexiv::rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
109 std::string robot_sn = argv[1];
112 std::string serial_port = argv[2];
122 spdlog::warn(
"Fault occurred on the connected robot, trying to clear ...");
125 spdlog::error(
"Fault cannot be cleared, exiting ...");
128 spdlog::info(
"Fault on the connected robot is cleared");
132 spdlog::info(
"Enabling robot ...");
137 std::this_thread::sleep_for(std::chrono::seconds(1));
139 spdlog::info(
"Robot is now operational");
144 g_fd = open(serial_port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL | O_CLOEXEC);
147 spdlog::error(
"Unable to open serial port [{}]", serial_port);
151 spdlog::warn(
"Benchmark signal will be sent every 1 second");
158 std::bind(PeriodicTask, std::ref(robot)),
"HP periodic", 1, scheduler.
max_priority());
163 while (!g_stop_sched) {
164 std::this_thread::sleep_for(std::chrono::milliseconds(1));
169 }
catch (
const std::exception& e) {
170 spdlog::error(e.what());
Main interface with the robot, containing several function categories and background services.
void SetDigitalOutputs(const std::vector< unsigned int > &port_idx, const std::vector< bool > &values)
[Blocking] Set one or more digital output ports, including 16 on the control box plus 2 inside the wr...
bool operational(bool verbose=true) const
[Non-blocking] Whether the robot is ready to be operated, which requires the following conditions to ...
void Enable()
[Blocking] Enable the robot, if E-stop is released and there's no fault, the robot will release brake...
bool fault() const
[Non-blocking] Whether the robot is in fault state.
bool ClearFault(unsigned int timeout_sec=30)
[Blocking] Try to clear minor or critical fault of the robot without a power cycle.
Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are...
int max_priority() const
[Non-blocking] Get maximum available priority for user tasks.
void AddTask(std::function< void(void)> &&callback, const std::string &task_name, int interval, int priority, int cpu_affinity=-1)
[Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigne...
void Stop()
[Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed...
void Start()
[Blocking] Start all added tasks. A dedicated thread will be created for each added task and the peri...