rby1-sdk
Loading...
Searching...
No Matches
robot.h
1#pragma once
2
3#include "rby1-sdk/version.h"
4
5#include <condition_variable>
6#include <functional>
7#include <string>
8
9#include "control_manager_state.h"
10#include "dynamics/robot.h"
11#include "export.h"
12#include "log.h"
13#include "net/types.h"
14#include "robot_command_builder.h"
15#include "robot_command_feedback.h"
16#include "robot_info.h"
17#include "robot_state.h"
18
19namespace rb {
20
21template <typename T>
22class Robot;
23
24template <typename T>
26
27template <typename T>
29
30template <typename T>
32
33template <typename T>
35
36template <typename T>
38
39template <typename T>
40struct ControlInput;
41
42template <typename T>
43struct ControlState;
44
45struct PIDGain;
46
47struct Color;
48
49struct SerialDevice;
50
51class SerialStream;
52
53class SerialStreamImpl;
54
55} // namespace rb
56
57namespace rb {
58
67template <typename T>
68class RBY1_SDK_API Robot : public std::enable_shared_from_this<Robot<T>> {
69 public:
70 using ModelType = T;
71
78 static std::shared_ptr<Robot<T>> Create(std::string address);
79
80 ~Robot();
81
87 std::string GetAddress();
88
98 bool Connect(int max_retries = 5, int timeout_ms = 1000, std::function<bool()> signal_check = nullptr);
99
104
110 [[nodiscard]] bool IsConnected() const;
111
121
127 double GetTimeScale() const;
128
135 double SetTimeScale(double time_scale) const;
136
143 bool PowerOn(const std::string& dev_name) const;
144
151 bool PowerOff(const std::string& dev_name) const;
152
159 bool IsPowerOn(const std::string& dev_name) const;
160
167 bool ServoOn(const std::string& dev_name) const;
168
175 bool IsServoOn(const std::string& dev_name) const;
176
183 bool ServoOff(const std::string& dev_name) const;
184
192 bool SetPositionPGain(const std::string& dev_name, uint16_t p_gain) const;
193
201 bool SetPositionIGain(const std::string& dev_name, uint16_t i_gain) const;
202
210 bool SetPositionDGain(const std::string& dev_name, uint16_t d_gain) const;
211
221 bool SetPositionPIDGain(const std::string& dev_name, uint16_t p_gain, uint16_t i_gain, uint16_t d_gain) const;
222
230 bool SetPositionPIDGain(const std::string& dev_name, const rb::PIDGain& pid_gain) const;
231
237 std::vector<rb::PIDGain> GetTorsoPositionPIDGains() const;
238
244 std::vector<rb::PIDGain> GetRightArmPositionPIDGains() const;
245
251 std::vector<rb::PIDGain> GetLeftArmPositionPIDGains() const;
252
258 std::vector<rb::PIDGain> GetHeadPositionPIDGains() const;
259
266 rb::PIDGain GetPositionPIDGain(const std::string& dev_name) const;
267
274 bool BrakeEngage(const std::string& dev_name) const;
275
282 bool BrakeRelease(const std::string& dev_name) const;
283
290 bool HomeOffsetReset(const std::string& dev_name) const;
291
298 bool SetPresetPosition(const std::string& joint_name) const;
299
308 bool EnableControlManager(bool unlimited_mode_enabled = false) const;
309
316
323
329 bool CancelControl() const;
330
338 bool SetToolFlangeOutputVoltage(const std::string& name, int voltage) const;
339
348 bool SetToolFlangeDigitalOutput(const std::string& name, unsigned int channel, bool state) const;
349
358 bool SetToolFlangeDigitalOutputDual(const std::string& name, bool state_0, bool state_1) const;
359
366 void StartStateUpdate(const std::function<void(const RobotState<T>&)>& cb, double rate);
367
374 void StartStateUpdate(const std::function<void(const RobotState<T>&, const ControlManagerState&)>& cb, double rate);
375
380
387 void StartLogStream(const std::function<void(const std::vector<Log>&)>& cb, double rate);
388
393
400
407 std::vector<Log> GetLastLog(unsigned int count) const;
408
414 std::vector<std::string> GetFaultLogList() const;
415
422
430 std::unique_ptr<RobotCommandHandler<T>> SendCommand(const RobotCommandBuilder& builder, int priority = 1);
431
438 std::unique_ptr<RobotCommandStreamHandler<T>> CreateCommandStream(int priority = 1);
439
453 bool Control(std::function<ControlInput<T>(const ControlState<T>&)> control, int port = 0, int priority = 1);
454
462 bool ResetOdometry(double angle, const Eigen::Vector<double, 2>& position);
463
469 std::vector<std::pair<std::string, int>> GetParameterList() const;
470
479 bool SetParameter(const std::string& name, const std::string& value, bool write_db = true);
480
487 std::string GetParameter(const std::string& name) const;
488
489 [[deprecated("Use FactoryResetParameter() instead.")]]
490 bool ResetParameterToDefault(const std::string& name) const;
491
492 [[deprecated("Use FactoryResetAllParameters() instead.")]]
493 void ResetAllParametersToDefault() const;
494
501 bool FactoryResetParameter(const std::string& name) const;
502
507
514 bool ResetParameter(const std::string& name) const;
515
519 void ResetAllParameters() const;
520
526 std::string GetRobotModel() const;
527
535 bool ImportRobotModel(const std::string& name, const std::string& model) const;
536
542 bool SyncTime();
543
550
557 bool StartTimeSync(long period_sec = 10 /* sec */);
558
565
572 std::shared_ptr<dyn::Robot<T::kRobotDOF>> GetDynamics(const std::string& urdf_model = "");
573
584 bool SetLEDColor(const Color& color, double duration = 1 /* sec */, double transition_time = 0 /* sec */,
585 bool blinking = false, double blinking_freq = 1 /* Hz */);
586
592 std::tuple<struct timespec, std::string, std::string> GetSystemTime() const;
593
601 bool SetSystemTime(struct timespec utc_time, std::optional<std::string> time_zone = std::nullopt) const;
602
609 bool SetBatteryLevel(double level) const;
610
619 bool SetBatteryConfig(double cutoff_voltage, double fully_charged_voltage, const std::array<double, 4>& coefficients);
620
626 bool ResetBatteryConfig() const;
627
636 bool WaitForControlReady(long timeout_ms) const;
637
644
650 std::vector<WifiNetwork> ScanWifi() const;
651
663 bool ConnectWifi(const std::string& ssid, const std::string& password = "", bool use_dhcp = true,
664 const std::string& ip_address = "", const std::string& gateway = "",
665 const std::vector<std::string>& dns = {}) const;
666
672 bool DisconnectWifi() const;
673
679 std::optional<WifiStatus> GetWifiStatus() const;
680
686 std::vector<SerialDevice> GetSerialDeviceList() const;
687
698 std::unique_ptr<SerialStream> OpenSerialStream(const std::string& device_path, int buadrate, int bytesize,
699 char parity, int stopbits) const;
700
708 bool DownloadFile(const std::string& path, std::ostream& output) const;
709
717 bool DownloadFileToCallback(const std::string& path, std::function<void(const char*, size_t)> on_chunk) const;
718
719 private:
720 explicit Robot(std::string address);
721
722 private:
723 std::shared_ptr<RobotImpl<T>> impl_;
724};
725
733template <typename T>
734class RBY1_SDK_API RobotCommandHandler {
735 public:
737
743 bool IsDone() const;
744
750 void Wait();
751
758 bool WaitFor(int timeout_ms);
759
763 void Cancel();
764
774
780 bool GetStatus() const;
781
782 private:
783 explicit RobotCommandHandler(std::unique_ptr<RobotCommandHandlerImpl<T>> impl);
784
785 std::unique_ptr<RobotCommandHandlerImpl<T>> impl_;
786
787 friend class RobotImpl<T>;
788};
789
799template <typename T>
800class RBY1_SDK_API RobotCommandStreamHandler {
801 public:
803
809 bool IsDone() const;
810
816 void Wait();
817
825 bool WaitFor(int timeout_ms);
826
841 RobotCommandFeedback SendCommand(const RobotCommandBuilder& builder, int timeout_ms = 1000);
842
853 RobotCommandFeedback RequestFeedback(int timeout_ms = 1000);
854
855 void Cancel();
856
857 private:
858 explicit RobotCommandStreamHandler(std::unique_ptr<RobotCommandStreamHandlerImpl<T>> impl);
859
860 std::unique_ptr<RobotCommandStreamHandlerImpl<T>> impl_;
861
862 friend class RobotImpl<T>;
863};
864
872template <typename T>
873struct RBY1_SDK_API ControlInput {
875 Eigen::Vector<bool, T::kRobotDOF> mode{Eigen::Vector<bool, T::kRobotDOF>::Constant(false)};
877 Eigen::Vector<double, T::kRobotDOF> target{Eigen::Vector<double, T::kRobotDOF>::Zero()};
879 Eigen::Vector<unsigned int, T::kRobotDOF> feedback_gain{Eigen::Vector<unsigned int, T::kRobotDOF>::Zero()};
881 Eigen::Vector<double, T::kRobotDOF> feedforward_torque{Eigen::Vector<double, T::kRobotDOF>::Zero()};
883 bool finish{false};
884};
885
893template <typename T>
894struct RBY1_SDK_API ControlState {
896 double t{0.};
898 Eigen::Vector<bool, T::kRobotDOF> is_ready{Eigen::Vector<bool, T::kRobotDOF>::Constant(false)};
900 Eigen::Vector<double, T::kRobotDOF> position{Eigen::Vector<double, T::kRobotDOF>::Zero()};
902 Eigen::Vector<double, T::kRobotDOF> velocity{Eigen::Vector<double, T::kRobotDOF>::Zero()};
904 Eigen::Vector<double, T::kRobotDOF> current{Eigen::Vector<double, T::kRobotDOF>::Zero()};
906 Eigen::Vector<double, T::kRobotDOF> torque{Eigen::Vector<double, T::kRobotDOF>::Zero()};
907};
908
914struct RBY1_SDK_API PIDGain {
916 uint16_t p_gain;
918 uint16_t i_gain;
920 uint16_t d_gain;
921};
922
928struct RBY1_SDK_API Color {
932 Color() : r(0), g(0), b(0) {}
933
941 Color(uint8_t _r, uint8_t _g, uint8_t _b) : r(_r), g(_g), b(_b) {}
942
944 uint8_t r{0};
946 uint8_t g{0};
948 uint8_t b{0};
949};
950
956struct RBY1_SDK_API SerialDevice {
958 std::string path;
960 std::string description;
961};
962
969class RBY1_SDK_API SerialStream {
970 public:
972
981 bool Connect(bool verbose) const;
982
988 void Disconnect() const;
989
995 void Wait() const;
996
1003 bool WaitFor(int timeout_ms) const;
1004
1010 bool IsOpened() const;
1011
1017 bool IsCancelled() const;
1018
1024 bool IsDone() const;
1025
1034 void SetReadCallback(const std::function<void(const std::string&)>& cb);
1035
1042 bool Write(const std::string& data);
1043
1050 bool Write(const char* data);
1051
1059 bool Write(const char* data, int n);
1060
1067 bool WriteByte(char ch);
1068
1069 private:
1070 explicit SerialStream(std::unique_ptr<SerialStreamImpl> impl);
1071
1072 std::unique_ptr<SerialStreamImpl> impl_;
1073
1074 template <typename T>
1075 friend class RobotImpl;
1076};
1077
1078} // namespace rb
Definition robot_command_builder.h:730
Definition robot_command_feedback.h:395
Robot command handler.
Definition robot.h:734
bool GetStatus() const
Get gRPC status.
void Wait()
Wait for the command execution to complete.
void Cancel()
Cancel the command execution.
bool IsDone() const
Check if the command execution is complete.
bool WaitFor(int timeout_ms)
Wait for the command execution with timeout.
RobotCommandFeedback Get()
Wait for the command execution and get feedback.
Definition robot.h:31
Robot command stream handler for continuous command sending.
Definition robot.h:800
void Wait()
Wait for the command stream to complete.
bool WaitFor(int timeout_ms)
Wait for the command stream to complete with timeout.
RobotCommandFeedback SendCommand(const RobotCommandBuilder &builder, int timeout_ms=1000)
Send a command through the stream.
RobotCommandFeedback RequestFeedback(int timeout_ms=1000)
Request feedback from the current command stream.
bool IsDone() const
Check if the command stream is complete.
Robot control interface.
Definition robot.h:68
std::vector< SerialDevice > GetSerialDeviceList() const
Get list of available serial devices on the robot.
bool ResetBatteryConfig() const
Reset battery configuration to default.
std::string GetAddress()
Get the robot's network address.
ControlManagerState GetControlManagerState() const
Get control manager state.
std::vector< WifiNetwork > ScanWifi() const
Scan for available WiFi networks.
std::vector< std::string > GetFaultLogList() const
Get fault log list.
void StartStateUpdate(const std::function< void(const RobotState< T > &)> &cb, double rate)
Start state update callback.
bool ServoOff(const std::string &dev_name) const
Disable servo control for a device.
std::tuple< struct timespec, std::string, std::string > GetSystemTime() const
Get robot system time.
bool BrakeEngage(const std::string &dev_name) const
Engage the brake for a device.
bool SyncTime()
Synchronize time with the robot.
bool ResetOdometry(double angle, const Eigen::Vector< double, 2 > &position)
Reset odometry to specified values.
bool ResetNetworkSetting() const
Reset network settings to default.
bool SetParameter(const std::string &name, const std::string &value, bool write_db=true)
Set a robot parameter.
bool SetPositionDGain(const std::string &dev_name, uint16_t d_gain) const
Set position derivative gain for a device.
bool ResetFaultControlManager() const
Reset fault in the control manager.
void StopLogStream()
Stop log stream callback.
bool PowerOn(const std::string &dev_name) const
Power on a device.
bool Connect(int max_retries=5, int timeout_ms=1000, std::function< bool()> signal_check=nullptr)
Attempts to establish a connection with the robot.
std::optional< WifiStatus > GetWifiStatus() const
Get WiFi connection status.
void StopStateUpdate()
Stop state update callback.
bool IsServoOn(const std::string &dev_name) const
Check if servo control is enabled for a device.
bool DisconnectWifi() const
Disconnect from WiFi network.
bool ImportRobotModel(const std::string &name, const std::string &model) const
Import robot model.
rb::PIDGain GetPositionPIDGain(const std::string &dev_name) const
Get position PID gain for a specific device.
std::string GetParameter(const std::string &name) const
Get a robot parameter value.
std::vector< Log > GetLastLog(unsigned int count) const
Get last log entries.
RobotInfo GetRobotInfo() const
Retrieves static information about the robot.
bool SetPositionIGain(const std::string &dev_name, uint16_t i_gain) const
Set position integral gain for a device.
void FactoryResetAllParameters() const
Factory reset all parameters to their default values.
std::unique_ptr< RobotCommandStreamHandler< T > > CreateCommandStream(int priority=1)
Create a command stream for continuous command sending.
bool SetPositionPIDGain(const std::string &dev_name, uint16_t p_gain, uint16_t i_gain, uint16_t d_gain) const
Set position PID gains for a device.
bool DownloadFileToCallback(const std::string &path, std::function< void(const char *, size_t)> on_chunk) const
Download a file from the robot using a callback function.
bool StartTimeSync(long period_sec=10)
Start time synchronization.
bool PowerOff(const std::string &dev_name) const
Power off a device.
bool DownloadFile(const std::string &path, std::ostream &output) const
Download a file from the robot.
static std::shared_ptr< Robot< T > > Create(std::string address)
Create a robot instance.
bool SetToolFlangeDigitalOutput(const std::string &name, unsigned int channel, bool state) const
Set the digital output state of a specific channel on the tool flange.
bool SetBatteryLevel(double level) const
Set battery level.
void ResetAllParameters() const
Reset all parameters to their default values.
bool EnableControlManager(bool unlimited_mode_enabled=false) const
Enable the control manager.
std::vector< rb::PIDGain > GetLeftArmPositionPIDGains() const
Get left arm position PID gains.
void Disconnect()
Disconnect from the robot.
std::string GetRobotModel() const
Get robot model information.
bool CancelControl() const
Cancel current control operation.
double GetTimeScale() const
Get the current time scale factor.
RobotState< T > GetState() const
Get current robot state.
bool SetPositionPGain(const std::string &dev_name, uint16_t p_gain) const
Set position proportional gain for a device.
std::shared_ptr< dyn::Robot< T::kRobotDOF > > GetDynamics(const std::string &urdf_model="")
Get robot dynamics model.
bool StopTimeSync()
Stop time synchronization.
bool ResetParameter(const std::string &name) const
Reset a parameter to its default value.
bool ConnectWifi(const std::string &ssid, const std::string &password="", bool use_dhcp=true, const std::string &ip_address="", const std::string &gateway="", const std::vector< std::string > &dns={}) const
Connect to WiFi network.
void StartStateUpdate(const std::function< void(const RobotState< T > &, const ControlManagerState &)> &cb, double rate)
Start state update callback with control manager state.
bool Control(std::function< ControlInput< T >(const ControlState< T > &)> control, int port=0, int priority=1)
Start a blocking real-time control loop using a custom control callback.
bool SetToolFlangeOutputVoltage(const std::string &name, int voltage) const
Set tool flange output voltage.
bool SetLEDColor(const Color &color, double duration=1, double transition_time=0, bool blinking=false, double blinking_freq=1)
Set LED color.
bool IsPowerOn(const std::string &dev_name) const
Check if a device is powered on.
void StartLogStream(const std::function< void(const std::vector< Log > &)> &cb, double rate)
Start log stream callback.
bool HasEstablishedTimeSync()
Check if time synchronization has been established.
bool SetBatteryConfig(double cutoff_voltage, double fully_charged_voltage, const std::array< double, 4 > &coefficients)
Set battery configuration.
bool DisableControlManager() const
Disable the control manager.
bool WaitForControlReady(long timeout_ms) const
Wait until the robot is ready to accept control commands.
std::vector< std::pair< std::string, int > > GetParameterList() const
Get list of available parameters.
bool IsConnected() const
Checks whether the robot is currently connected.
bool SetPresetPosition(const std::string &joint_name) const
Set preset position for a joint (only available for PVL-based motors).
bool SetToolFlangeDigitalOutputDual(const std::string &name, bool state_0, bool state_1) const
Set the digital output states of two channels on the tool flange simultaneously.
std::unique_ptr< RobotCommandHandler< T > > SendCommand(const RobotCommandBuilder &builder, int priority=1)
Send a command to the robot.
std::vector< rb::PIDGain > GetTorsoPositionPIDGains() const
Get torso position PID gains.
std::unique_ptr< SerialStream > OpenSerialStream(const std::string &device_path, int buadrate, int bytesize, char parity, int stopbits) const
Open a serial stream.
std::vector< rb::PIDGain > GetRightArmPositionPIDGains() const
Get right arm position PID gains.
bool SetPositionPIDGain(const std::string &dev_name, const rb::PIDGain &pid_gain) const
Set position PID gains for a device.
bool FactoryResetParameter(const std::string &name) const
Factory reset a parameter to its default value.
bool ServoOn(const std::string &dev_name) const
Enable servo control for a device.
double SetTimeScale(double time_scale) const
Set the time scale for motion execution.
bool SetSystemTime(struct timespec utc_time, std::optional< std::string > time_zone=std::nullopt) const
Set robot system time.
bool BrakeRelease(const std::string &dev_name) const
Release the brake for a device.
bool HomeOffsetReset(const std::string &dev_name) const
Reset home offset for a device.
std::vector< rb::PIDGain > GetHeadPositionPIDGains() const
Get head position PID gains.
Definition robot.h:25
Serial communication stream interface.
Definition robot.h:969
bool IsOpened() const
Check if the serial port is opened.
bool IsCancelled() const
Check if the serial stream has been cancelled.
bool Write(const char *data, int n)
Write binary data to the serial port.
bool Write(const char *data)
Write null-terminated string data to the serial port.
bool WriteByte(char ch)
Write a single byte to the serial port.
bool Write(const std::string &data)
Write string data to the serial port.
void SetReadCallback(const std::function< void(const std::string &)> &cb)
Set callback function for incoming data.
bool WaitFor(int timeout_ms) const
Wait for the serial stream to complete with timeout.
void Wait() const
Wait for the serial stream to complete.
bool IsDone() const
Check if the serial stream is done.
void Disconnect() const
Disconnect from the serial device.
bool Connect(bool verbose) const
Connect to the serial device.
RGB color representation.
Definition robot.h:928
Color()
Construct a Color instance with default values (0, 0, 0).
Definition robot.h:932
Color(uint8_t _r, uint8_t _g, uint8_t _b)
Construct a Color instance with specified RGB values.
Definition robot.h:941
Robot control input parameters.
Definition robot.h:873
Definition control_manager_state.h:7
Robot control state information.
Definition robot.h:894
PID gain configuration.
Definition robot.h:914
uint16_t i_gain
Integral gain value.
Definition robot.h:918
uint16_t d_gain
Derivative gain value.
Definition robot.h:920
uint16_t p_gain
Proportional gain value.
Definition robot.h:916
Definition robot_info.h:27
Definition robot_state.h:94
Serial device information.
Definition robot.h:956
std::string path
Device path (e.g., "/dev/ttyUSB0").
Definition robot.h:958
std::string description
Device description.
Definition robot.h:960