Program Listing for File unitree_driver.hppī
â° Return to documentation for file (include/unitree_ros/unitree_driver.hpp)
/*
Copyright (c) 2023, University of Luxembourg
All rights reserved.
Redistributions and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*/
#ifndef UNITREE_DRIVER_H
#define UNITREE_DRIVER_H
#include <FaceLightClient.h>
#include <unitree_legged_sdk/unitree_legged_sdk.h>
#include <array>
#include <atomic>
#include <memory>
#include <thread>
#include <unitree_ros/common_defines.hpp>
#include "unitree_legged_sdk/comm.h"
/*
* @brief Class Unitree driver which is use for the main interaction with
* the Unitree Go1 robot.
*/
class UnitreeDriver {
// Unitree SDK related
private:
std::string ip_addr_ = "192.168.123.161";
int target_port_ = 8082;
int local_port_ = 8090;
UNITREE_LEGGED_SDK::UDP udp_connection_;
UNITREE_LEGGED_SDK::HighCmd high_cmd_ = {};
UNITREE_LEGGED_SDK::HighState high_state_ = {};
// Robot settings
mode_enum curr_mode_ = mode_enum::MODE_IDDLE;
gaitype_enum curr_gait_type_ = gaitype_enum::GAITYPE_IDDLE;
velocity_t curr_velocity_cmd_ = {0, 0, 0};
uint8_t speed_level_ = speed_level_enum::LOW_SPEED;
bool use_obstacle_avoidance_ = false;
// Faceled
FaceLightClient light_client_;
// Robot status
robot_status_e robot_status = robot_status_e::IDDLE;
// Threads
std::thread face_led_thread_;
std::atomic<bool> face_led_thread_stop_flag_;
std::thread recv_state_thread_;
std::atomic<bool> recv_state_thread_stop_flag_;
public:
UnitreeDriver(std::string ip_addr = "192.168.123.161", int target_port = 8082);
/*
* @brief Deconstructor for the class UnitreeDriver. When called, it makes
* the robot stand down and deactivate the motors.
*/
~UnitreeDriver();
void stand_up();
void stand_down();
void damping_mode();
void walk_w_vel(float x, float y, float yaw);
void walk_w_pos(position_t position, quarternion_t orientation);
odom_t get_odom();
UNITREE_LEGGED_SDK::IMU get_imu();
UNITREE_LEGGED_SDK::BmsState get_bms();
std::array<UNITREE_LEGGED_SDK::MotorState, 12> get_joint_states();
sensor_ranges_t get_radar_ranges();
uint8_t get_battery_percentage();
void set_mode(mode_enum mode);
void set_gaitype(gaitype_enum gaitype);
void enable_obstacle_avoidance(bool flag);
void stop();
void set_head_led(uint8_t r, uint8_t g, uint8_t b);
void set_head_led(UNITREE_LEGGED_SDK::LED led);
void update_robot_status();
private:
void send_high_cmd_();
void recv_high_state_();
bool is_connection_established_();
position_t get_position_();
quarternion_t get_quaternion_();
velocity_t get_velocity_();
void blink_face_led(uint8_t r, uint8_t g, uint8_t b);
void blink_face_led(UNITREE_LEGGED_SDK::LED led);
};
#endif // !#ifndef UNITREE_DRIVER_H