Spot Dog coding

This tutorial will guide you through starting up spot, using SPOT, and how to program SPOT using Python. Python is the backbone of this tutorial; it is best to prepare for this workshop by researching functions and classes prior to this tutorial.

What you will need

Step 1: Prepare your working Directory

Go to the Github link above and click the code button in green, and then click "Download ZIP". Note that this repository will change with new updates and features, so it is best to either use Github Desktop to get the most recent updates, or manually check before using SPOT.

Step 2: Install REQUIREMENTS AND PIP

To control SPOT, Boston Dynamics wrote a SDK which we use in our demonstrations. It is imperative that you not only download all requirements Boston Dynamics includes, but also what we include. To do this:

1: Open a terminal and navigate to your working directory (the directory where the github files are). Once you are there, verify that you have properly installed pip by typing pip. You should see something similar to the image below.

pip

If pip is NOT installed, follow the follwing documentation: https://pip.pypa.io/en/stable/installation/

Now that we can verify pip works, we can install the requirements.txt file. This can be done by typing pip install -r requirements.txt . If no errors pop up in the terminal, we can continue with the next step.

pip install -r requirements.txt

Step 3: Writing your Program

We will be covering how to use the file spot_helper.py to write code in the file called code_here.py. There are three ways you can write your code:

1: Use previously made functions in spot_helper.py in your main function (def main()) to control the dog

2: Create functions inside spot_helper.py (def your_function(self)) which can be called in your main code to remove repitious lines of code

3: Create another helper file with extra features that you want to use (will require you to import eg from newfile.py import class)

The following 3 examples demonstrate these implementations

Steps to running your code

1: Install your requirements (pip install -r requirements.txt)

2: Connect to SPOT's WiFi

3: Run your code (python3 code_here.py)

Example 1: Making SPOT Roll Over and Get back up

SPOT comes with a unique ability to flip over to make swapping its battery more accessable. In boston dynamics code, it is BatteryChangePoseCommand.Request.HINT_RIGHT. But now, we can utilize the helper functions written to make it work, specifically:

def battery_position(self):

"""Commands the robot to assume a position that facilitates battery changing."""

print("Assuming battery change position...")

# Using the dir_hint to specify the direction the robot should roll to for battery change

cmd = RobotCommandBuilder.battery_change_pose_command(

dir_hint=basic_command_pb2.BatteryChangePoseCommand.Request.HINT_RIGHT)

self.command_client.robot_command(cmd)

Since the demonstration file already references the class SpotRobotController which already has the function battery_position, we can just call battery position in the code_here.py file. BE SURE TO ADD time.sleep(5). The code executes faster than the SPOT robot and will cause crashing if you do not wait for the dog to be ready.

Now that the dog will roll on its back, we want to make sure it can also get up. We can use another function written in the class SpotRobotController called self_right. This will make spot try to get up on its own. Your code should look like this:

# WRITE YOUR CODE HERE

# make the robot stand

controller.stand()

time.sleep(4)

# make the robot flip over

controller.battery_position()

time.sleep(7)

# power the batteries back on

controller.power_on_robot()

time.sleep(5)

# make the robot get back up

controller.self_right()

time.sleep(7)

# END OF YOUR CODE

To run the code:

1: Connect to the SPOT Robot's WiFi (Ask makerspace staff for permission)

2: Run the code "python3 code_here.py"

3: Enter the dog's username and password in the terminal (Ask makerspace staff for details)


python3 code_here.py

Example 2: Making SPOT Look up

We have built commands to making the dog move, rotate, and much more. To let SPOT look up, we can utilize the function turn_body_pitch_yaw which is in the spot_helper.py . To use this, we will look at the arguments necessary.

We can use this in terms of our goal of writing a new function, which we can call def look_up()

def look_up(self, pitch=0.3):

        """

        Commands the robot to look up by adjusting its body pitch.

        Args:

            pitch (float): The pitch angle in radians. Positive values look up.

        """

        self.turn_body_pitch_yaw(-pitch, 0)

Lets look at a few things. Note that our argument says pitch=.3. This means that when we call the function, if we dont pass an argument (eg. look_up()) it will by default set the pitch to .3. If we switch it to look_up(.5) then that will be the set pitch. Be sure to add this to your spot_helper.py file under the class. Once you've done that, you can go back to code_here.py and add look_up into your main function.

To run look up example:

1: Connect to the SPOT Robot's WiFi (Ask makerspace staff for permission)

2: Run the code "python3 code_here.py"

3: Enter the dog's username and password in the terminal (Ask makerspace staff for details)

Example 3: Making new helper functions for more actions

Let's say we want SPOT to walk around on its own. We have two sets of functions built for letting spot move in spot_helper.py: def move, and def move_direction. We can create a new function called walk_procedure. We can use move_direction inside this new function to allow the dog to do a set of commands in one single function, and use nested functions.

def walk_procedure(self):

        print("Starting walk procedure in a small box area...")

        # Walk forward for a short duration

        self.move_direction("forward", 2)

        time.sleep(2.5)  # wait for the move to complete

        # Rotate left to turn around in the box

        self.rotate("left", 1.5)

        time.sleep(2)  # wait for the rotate to complete

        # Walk forward again to return to the starting point

        self.move_direction("forward", 2)

        time.sleep(2.5)  # wait for the move to complete

        # Rotate right to face the original direction

        self.rotate("right", 1.5)

        time.sleep(2)  # wait for the rotate to complete

By putting this inside spot_helper.py's main class, you can enter controller.walk_procedure().

To run look up example:

1: Connect to the SPOT Robot's WiFi (Ask makerspace staff for permission)

2: Run the code "python3 code_here.py"

3: Enter the dog's username and password in the terminal (Ask makerspace staff for details)

python3 code_here.py