---
# System prepended metadata

title: Turtlebot integration

---

# Turtlebot integration

:::warning
This documentation is still under development!
:::

# Update Ubuntu to TurtleBots
Use [balenaEtcher](https://www.balena.io/etcher/) to flash all SD card to have Ubuntu's update version 20.04.


**_Steps to take_** 

1. Download balenaEthcher
2. Insert SD card into the computer
3. Flash Ubuntu images to SD cards & USB drives
4. Safely remove SD card from computer and place it into the Raspberry Pi that is connected to the turtlebot

# Software

## ROS2
[ROS2](https://docs.ros.org/en/foxy/index.html) is a Robot operating System that has a set of software libraries and tools for applications. ROS2 Foxy must be installed on all raspberry pi's. To learning more about the installation, visit the documentation for [installing ROS2 Foxy](https://docs.ros.org/en/foxy/Installation.html). 

## Motive + OptiTrack
Using the software Motive, we want to be able to create rigidbodys for each of the turtlebot so that they are track inside of the OptiTrack area.  Each turlebot has tracker on then set with different configurations.  When setting turltebot rigid bodies, place in the OptiTrack one at a time.  You place marker where the marker postion will not change relative to eachother.

** Step to Creating Rigid Bodys **

1. Place turtlebot in the OptiTrack
2. Click `View` from the menu bar
3. `Assets Plans`
4. Search for marker on in Motive
5. Left click dragging mouse over markers
6. Right click: `Rigit Body` --> `Create From Selected Markers`

Learn more about [Motive](https://optitrack.com/software/motive/)

## Unity
Download Unity in Ubuntu

1. Go to [Unity](https://unity.com/download) website and download the personal hub.


# Files
## Ansible
First make sure that ansible is install on Linux: 
    
`sudo apt update`

`sudo apt install ansible`

Note that when creating a YAML file just like Python,  that is space sensitive.  Therefore, be sure to check that spaces line up. To execute any YAML file is as followed:

`ansible-playbook <yaml_file_name> -i <host>`

### 99 Net Confiuation


### Host file
Create a name host file that will map a connection between all of the IP address and the domain name.  

**_Steps to set up host_**

	[turtlebot]
	swarmbot1 ansible_ssh_192.168.0.{{ host_number + 1 }} host_number=#
	[all:vars]
	ansible_ssh_common_args='-o
	UserknownHostsFile=/dev/null'
	ansible_connection=ssh
	ansible_user=ubuntu
	ansible_ssh_pass=swarmbots

### Middleware
    
    ---
    - hosts: turtlebots
    tasks:
    
    - name: Change ROS Middleware on Turtlebots
      become: yes
      become_method: sudo
      apt:
        state: present
        update_cache: yes
        name: ros-foxy-rmw-cyclonedds-cpp
    
    - name: Echo ROS Middleware
      become: yes
      shell: echo "export RWM_IMPLEMENTATION-rmw-cyclonedds-cpp" >>.bashrc

### Pixel Ring LEDs

The next file that was worked on was the LED file that turns on all the Neo Pixel Ring that are install on all of the turtlebots. A color for the pixel ring can be set using the value 0 to 255 for -r (red), -g (green), and -b (blue).  Set Test LED YAML file up as such: 

**_Test LED_**

    ---
    - hosts: turtlebots
      tasks:
       - name: Test LEDs
         become: yes
         command: python3 single_color_test.py -r # -g # -b #



The set up swarm file will copy all need files, including the DCF, the launch file, Colcon build, then stop then restart ROS (Robot Operating System) on the turtlebot.  

**_Setup Swarm_**

    ---
    - hosts: turtlebots
      become_user: ubuntu

	  Copies DCF files to the turtlebot
      tasks:
        - name: {Title for instruction}
          synchronize:
            src: {Path that DCF is located in on your computer}
            dest: {Path to send to, which is on the turtlebot's raspberry pi}
           
            Copy lauch file
            recursive: yes

In order to execute the playbook, the command is: 

`ansible-playbook <yaml_file_name> -i <host_name>`


**_Install Pip_**

Creating a Yaml file for pip file will be useful for when it is time to install pip on all of the turlebot.


**_Bringup Swarm_**

Using the bring up will lauch the turtlebots all at once using the following command:

`ansible-playbook bringup_swarm.yaml -i name_host`


**_DCF Module_**


#  Simulation and Real


1. Launch Motive and power on motion cameras in the OptiTrack. 
Be sure that you are able to track the rigid bodies in Motive

2. On the computer that you are working on, open a terminal and source your workspace and run the node:
`ros2 run natnet client --ros-args -p use_timestamps:=false`

3. Launch Unity and project

4. In Unity, assure that you are using the path that will take you the the parameter config file that was created.

5. Press the play button at top of Unity to run

    If all was done correctly, there should be a turtlebot shown on the plateform where you will see normal color turtlebots and red turtlebots

6. Run the DCF node on the real robot through SSH first, then try ansible

7. Run the DCF node for the fake robot using:

    `ros2 launch dcf constructive.launch.py`

This command should move the real robot in the simulation.
Verify by seeing both robots move


# Connecting Turtlebots to Wifi 
**via Turtlebot**
- HDMI cable
- Keyboard
- Turtlebot Charger/Battery
- Monitor


1. Connect HDMI, Keyboard and Battery to the Turtlebot
2. Once Turtlebot fjnish loading up, login with *Username*: ubuntu,  *Password*: swarmbots (might need to press *Enter* after loading to login)
3. type **sudo emacs /etc/netplan/99-netcfg.yaml**
4. under wlan1 change dhcp4 to no
5. added the following below dhcp6

'''

	addresses:
       - 192.168.0.{turtlebot #}/24
    access-points:
	     "swarmweb":
         password:
         "APLswarming!"
'''

6.  *Ctrl* s and *Ctrl* x to save and exit
7. Sudo netplan apply #also check syntax error
8. Sudo shutdown now
9. Reboot turtlebot
10. **After a few minute**, run **Sudo nmap -sn 192.168.0.0/24** on the computer in the same network (install nmap if needed)
- [x] The turtlebot address should pop up in the network

**via Flashing SD card**
1. Follow Connecting Turtlebot to Wifi via Turtlebot
2. Download balenaEtcher to a computer
3. Make a copy of the SD card from the turtlebot you previously connected the Wifi to your computer using belenaEtcher
4. Use the copy to flash all other turtlebots SD cards. 
5. You will need to turn the turtlebots on one by one and use **Sudo nmap -sn 192.168.0.0/24** to find it IP address
6. SSH into the turtlebot in terminal using **ssh ubuntu@(the ip address of #5)  
7. Login with *Username*: ubuntu,  *Password*: swarmbots
8. type **sudo emacs /etc/netplan/99-netcfg.yaml**
9. change address IP to the turtlebots correct IP address(ex: if it's turtlebot 2, you would changed it's IP to 192.168.0.2)
10. Follow steps 5-9 till all the turtlbots is connected to the WIFI with their unique IP address.




# ROS Node for Xbox Controller

- Git Clone this file under foxy branch [link](https://github.com/ros2/teleop_twist_joy)
- Every changes to teleop_twist_joy should be made in this directory "opt/ros/foxy/share/teleop_twist_joy"
1. Replace xbox.config.yaml (might be different if you're not using a wired xbox-360 controller)
    - Use this [link](http://wiki.ros.org/joy) to find the buttons values according to their controller (This will help you find the axis_linear and axis_angular values)


```

teleop_twist_joy_node:
  ros__parameters:
    axis_linear:  # 1 = Left thumb stick vertical 
      x: 1
    scale_linear:  #Speed
      x: 0.22
    scale_linear_turbo:
      x: 1.5
    axis_angular:  # 3 = right thumb stick horizontal
      yaw: 3
    scale_angular:  #Speed
      yaw: 0.4
    require_enable_button: false
    enable_button: 2  # Left trigger button
    enable_turbo_button: 5  # Right trigger button 
    
```
2. Replace teleop-launch.py from the launch folder
```

import os
from ament_index_python.packages import get_package_share_directory
import launch
import launch_ros.actions
def generate_launch_description():
    joy_config = launch.substitutions.LaunchConfiguration('joy_config')
    joy_dev = launch.substitutions.LaunchConfiguration('joy_dev')
    config_filepath = launch.substitutions.LaunchConfiguration('config_filepath')
    swarmbot = launch.substitutions.LaunchConfiguration('swarmbot')
    #node_name = launch.substitutions.LaunchCongiguration('node_name')
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument('swarmbot', default_value='swarmbot'),
        launch.actions.DeclareLaunchArgument('joy_config', default_value='ps3'),
        launch.actions.DeclareLaunchArgument('joy_dev', default_value='/dev/input/js0'),
        launch.actions.DeclareLaunchArgument('config_filepath', default_value=[
            launch.substitutions.TextSubstitution(text=os.path.join(
                get_package_share_directory('teleop_twist_joy'), 'config', '')),
            joy_config, launch.substitutions.TextSubstitution(text='.config.yaml')]),
        launch_ros.actions.Node(
            package='joy', executable='joy_node', name='joy_node',
            parameters=[{
                'dev': joy_dev,
                'deadzone': 0.3,
                'autorepeat_rate': 20.0,
            }],
            remappings=[('/joy',[swarmbot,'/joy'])],
            ),
        launch_ros.actions.Node(
            package='teleop_twist_joy', executable='teleop_node',
            name='teleop_twist_joy_node', parameters=[config_filepath],
            remappings=[('/joy',[swarmbot,'/joy']),('/cmd_vel',[swarmbot,'/cmd_vel'])],)
    ])

```

3. You first need to launch the turtlebot
    1. Turn on the turtlebot
    2. In the terminal on a linux station conntected to swarmweb, Run "sudo nmap -sn 192.168.0.0/24"
    3. Identify the turtlebot ip address
    4. ssh ubuntu@(turtlebot ip address)
    5. You will need to login
    6. Run "export ROS_DOMAIN_ID=30"
    7. Run "export TURTLEBOT3_MODEL=burger" 
    8. Run "ros2 launch turtlebot3_bringup robot.launch.py"
4. In a new terminal, 


# DCF


