B1 Software
===========

.. image:: media/gifs/b1_movearound.gif

B1 edu is intended for academic and research community. Unitree Robotics has
provided an `SDK <https://github.com/unitreerobotics/unitree_legged_sdk>`__ for
their robots in C++. A demo is available to the robots, however, it is recommended 
to directly contact ``support@mybotshop.de`` for the latest driver as the old ones are 
known to have issues. 

Requirements
------------

Please follow the pre-requisite software section.

-  :doc:`Pre-requisite Software <dependency>`
-  `Gazebo9 <https://classic.gazebosim.org/tutorials?cat=install&tut=install_ubuntu&ver=9.0>`__ (melodic)
-  `Gazebo11 <https://classic.gazebosim.org/tutorials?tut=install_ubuntu>`__ (noetic)

This driver requires a system setup with ROS. It is recommended to use
Ubuntu 18.04/Ubuntu 20.04 with ROS melodic/noetic.

To make sure that robot control isn't affected by system latencies, it
is highly recommended to connect the robot via `LAN` of via 5G connection.

Building
--------

A script can be found below for automatic installation of all the required dependencies mentioned above and the provided software installation.
After downloading and changing directory to the downloaded folder, installation script can be run by the following command:

.. note:: 
    
    For the B1 installation package please email ``support@mybotshop.de``

You first have to give the script root permission via:

.. code:: bash

    sudo chmod +x installer.bash

.. code:: bash

    ./installer.bash


Now everything should be installed and built (assuming ROS and Gazebo installed prior to following this tutorial). 
If there is any issues please contact ``support@mybotshop.de``. 

B1 Base
=======

.. image:: media/gifs/b1_base_driver.gif
   :width: 100%
   :align: center

B1 work in two modes:

1. High level mode
2. Low level mode

User can be in one of these modes and they can not be switched after
running.

High level mode
---------------

High level mode is mainly for walking and running. This mode too has two
modes normal and motion mode which can be distinguished from their
ips (For more details see Quadruped software guide). At the moment this
driver is working with default configuration.

In high level mode the robot can walk. To start the driver please run:

.. code:: bash

    sudo su
    source ~/catkin_ws/devel/setup.bash
    roslaunch b1_base base.launch
   

To launch the b1_viz:

.. code:: bash

   roslaunch b1_viz view_robot.launch



User can be in one of these modes and they can not be switched after
running. With ``base.launch``  launches a communication channel between the robot and remote PC. Then
later in */base_node/cmd_vel* a ROS node is run.
 
::

    Node [high_level_driver]

    Publications: state [b1_legged_msgs/HighState]

    Subscriptions: cmd_vel [geometry_msgs/Twist]

    Services: set_body_pose [b1_legged_msgs/SetMode]

The robot pose can be set using the robot ``set_body_pose`` service.
Robot state is published in ``state`` topic. This message contains a lot
of information. For more information see 
`high state message <https://github.com/unitreerobotics/unitree_ros_to_real/blob/master/unitree_legged_msgs/msg/HighState.msg>`__.


Using the driver
~~~~~~~~~~~~~~~~

The node subscribes to ``base_cmd_vel`` topic. Here special attention needs
to be paid. As the robot is holonimic so different ``Twist`` message
fields determine different movement. A nice tool here would be
`teleop\_twist\_keyboard <http://wiki.ros.org/teleop_twist_keyboard>`__.
Here in the teleop holonomic and non holonomic modes can be used (Be aware key strokes and their usage).


Low level mode
~~~~~~~~~~~~~~

In development ...

.. In this mode all the motors can be controlled directly. For that one
.. needs to first change the operation mode either to Servo mode or
.. Electronic brake mode. 

.. When using the lowlevel it is necessary to switch the robot to normal mode:.
.. When the robot is turned on and stands up on its own:

.. 1. L2+B (Together) robot will get down
.. 2. L1+L2+START (Together)

.. At this point you can run the routine

.. .. important: 
    
..    The robot will fall over when running the low-level routine, please suspend the robot before using it!

.. Low level mode has again three control modes:

.. 1. Position
.. 2. Velocity
.. 3. Torque

.. In low level mode joint level control can be achieved. In Quadruped B1
.. this low level mode can be in three different levels i.e. Position,
.. Velocity, Torque. All three of them can be used with this driver by
.. publishing appropriate messages. Unlike high level mode robot
.. communication is achieved inside the driver node so no seperate nodeshould run.
.. After turning on the robot and connecting through lan run the followingcommand:

.. .. code:: bash

..     sudo su
..     source ~/catkin_ws/devel/setup.bash
..     roslaunch b1_base base.launch

.. .. Note:

..     The ``base.launch`` file the target ip needs to be changed to ``192.168.123.10`` when connect to B1 via LAN.
..     Similarly, target port should be ``8007`` and working mode will be high_level.


.. A node with follwoing information will be run:

.. :

..     Node [low_level_driver]

..     Publications: state [unitree_legged_msgs/LowState]
..                   joint_states [sensor_msgs/JointState]

..     Subscriptions: joint_cmd [qre_msgs/JointCMD]

..     Services: set_body_pose [qre_msgs/SetBodyPose] -> Not implemented yet
..               set_control [qre_msgs/SetControl]

.. The node subscribes to ``joint_cmd`` topic. Again here special attention
.. to be paid as well for the joints. For joint sequence see
.. `qre\_msgs <https://my.hidrive.com/share/6kxgj8nox2>`__
.. README. The driver publishes two topics state topic pubslihes the
.. information from the robot. For more details see `low state
.. .. message <https://github.com/unitreerobotics/unitree_ros_to_real/blob/master/unitree_legged_msgs/msg/LowState.msg>`__.
.. Joint state messages are also published. ``set_control`` service is can
.. change the robot three modes namely i.e. Position, Velocity, Torque(Case
.. sensitive). For every control level here similar joint messages need to
.. be filled in ``joint_cmd`` topic other fields are not considered.

.. :

..     Position -> JointCMD.q
..                 JointCMD.Kd
..                 JointCMD.Kd
..     Velocity -> JointCMD.dq
..                 JointCMD.Kp
..                 JointCMD.Kd
..     Torque   -> JointCMD.tau

B1 Legged Msgs
---------------

This ros package contains custom ros messages used by the B1 Base.

B1 Camera
==========

Coming soon ...

B1 Control
===========

This ros package enables teleop for the Logitech controller.

.. This ros package fuses the robots tracked wheels movement with the inertial measurment unit (IMU) using the extended kalman filter.
.. Moreover, it provides the transforms from the robots base link to the odometry frame.

.. code:: bash

   roslaunch b1_control controller.launch

Future updates will include localization files required for navigation.

B1 Description
===============

This is a ros package that contains the 3D models of the B1. Additionally, the seperate 3D models are stitched together into a single 
entity via the universal robot description format (URDF). It contains the models for auxiliary components such as lidars as well. Additions
to the robot's 3D model can be made in the ``xacro/robot.urdf.xacro``. To view the robot with simulated dummy drivers. You can
run:

.. code:: bash
   
   roslaunch b1_description check_joint.launch

.. note::

   It is ideal to run this on your own PC when the robot is not attached just to adjust the position of your
   auxiliary components.


The description is automatically launched with the base driver to show the perceived position of the legs with the robot.

.. code:: bash
   
   roslaunch b1_description description.launch

B1 Gazebo
==========

.. image:: media/gifs/b1_gazebo.gif
   :width: 100%
   :align: center

This ros package is used for controlling and operating the B1 in Gazebo simulation. The robot can be tele-operated 
and can be used further for slam and autonomous navigation. This package requires the `Champ <https://github.com/chvmp/champ>`__ ROS repository.

To launch `Gazebo` run the following command:

.. code:: bash

    roslaunch b1_gazebo bringup.launch rviz:=true

To tele-operate:

.. code:: bash

   roslaunch champ_teleop teleop.launch

This can be used to create and test low-level drivers as well, more information on this is available on the 
`Champ <https://github.com/chvmp/champ>`__ repository.

B1 Viz
======

.. image:: media/gifs/b1_ouster.gif
   :width: 100%
   :align: center

This ros package is used for visualization of the robots current state both in simulation and in the real-hardware.

.. code:: bash

   roslaunch b1_viz view_robot.launch