Summer Sale: 5% OFF Over $49, Code: Elephant5; 10% OFF Over $99, Code:Elephant10

5.1 Basic movement of Jetbot

Posted by Fiona Su on

1. Create a robot instance

We created the Jetbot object in the previous course. We used some methods to control some of Jetbot's hardware, such as controlling the rotation of the left and right motors separately, controlling the LED light under the LOGO to produce breathing effects, etc. In fact, Jetbot is also packed with many methods of controlling Jetbot robot car directly .

Let's review some of the methods used in the previous lesson and discover more new ways to control Jetbot motion:

We can easily control the Jetbot by importing the Jetbot package and then creating a robot instance that controls the Jetbot robot car movement.

image.png After running the above code, we can see the Jetbot robot car pan/tilt rise, the pan/tilt down, the pan/tilt stop, the Jetbot robot car advance and back with a maximum speed 1, turn left and turn right with 0.75, stop. And we can see that brightness the LED light under LOGO of is 1.

If we want to move the robot over a period of time. At this point, we can use Python's time package.

The time module is often used to save system resources when the new thread handles some small tasks.

After we execute the following code, we will see that Jetbot robot car turns left and stop.

Code as shown below:

image.png About control the motor separately.

In addition to the robot.left_motor.value to set the value in the previous lesson, as well as robot.set_motors(Left_Motor_Value, Right_Motor_Value), they achieve the same effect.

However, the robot.set_motors(Left_Motor_Value, Right_Motor_Value) method is more convenient because the speed of the two sides of the motor can be set at the same time.

Code as shown below:

image.png The corresponding complete source code is located at:

/home/jetbot/Notebook/9.Basic movement of Jetbot/Basic movement of Jetbot.ipynb

2. Integrated interaction between ipywidgets control and traitlets

About Ipywidgets:

The ipywidgets widget is a GUI element, such as a button, drop-down menu, or text box, that resides in the browser, allowing us to control code and data by responding to events and calling the specified handler, each widget has its own specific The properties to handle some data information.

such as:



Progress Bar


 Text display component

image.png Picture display component


Data visualization of graphics plus slider


About traitlets:

Traitlets is a framework that allows Python classes to possess type checking, dynamic calculation defaults, and "on change" callback properties.

The package also includes a mechanism for configuring with traitlets to load values from file or command line arguments.

This is a unique layer at the top of the traitlet, so you can use traitlets in your code without using a configuration mechanism.

When we debug the Jetbot robot, we bind the components and data through ipywidgets and traitlets, and we can call back the refresh in real time in the background, which can greatly improve our debugging efficiency.

How to initial use them:

First import the relevant package, create two sliders, and define its properties when you create the slider.

Code as shown below:

image.png After running the above code, you can see two vertical sliders on it.

As shown below:

image.png Tips:

In the Jupyter Lab, you can actually pop the cell into another window, such as the two sliders. Although not in the same window, it is still connected to this notebook. The specific operation is to move the mouse to the right button of the cell (for example: slider), select "Creat new view for output", and then drag the window to the place you are satisfied.

Try clicking and dragging the slider up and down to see the change in value.

!!! Note:

Jetbot's motors are currently not responding when we move the sliders, because we have not connected them to the motor yet!

Below we will use the link function in the traitlets package to achieve. Code as shown below:


You can try dragging the slider (slowly drag it first to prevent your Jetbot from suddenly rushing out of the boundary and causing damage), and you can see that the servo attached to the camera is spinning.

The link function we created above actually creates a two-way link!

That means that if we set the motor value elsewhere, the slider will be updated!

For example, the code shown below:

image.png After running the above code, you can see that the slider has also changed, and the speed value of the motor will changed. If we want to disconnect this connection, we can call the unlink method to disconnect one by one.

Code shown below: image.png

After disconnecting, they return to the state of no contact.

However, if we don't want a two-way connection, for example, we only want to use the slider to display the speed value of the motor, and do not want to control it, then to achieve this function, we can use the dlink function, the source on the left, On the right is the target, (the data comes from the motor and then on the target).

The code is shown below: image.png

 Another method:

Another method to use traitlets is that attach a function to an event (for example, forward). As soon as the object changes, the function is called and some information that has changed is passed, such as the old and new values.

Create some buttons for controlling the robot to display on the notebook:



After executing the above code, you can see a set of robot control buttons shown above, but now you don't have any reaction when you click the button.

If we want to start control, we need to create some functions attached to the button on_click event.

The code as shown below:

image.png We have defined those functions, we will attach these functions to the on_click event of each button.

The code as shown below:

image.png After executing the above code, you will can control the Jetbot forward, backward, left turn, right turn, and stop by clicking the button component we created above.

In order to prevent the connected Jetbot signal from being disconnected, Jetbot is out of control and the damage is caused. Jetbot also provides a way to solve this problem, which is the heartbeat switch function.

The following code block is about how to use the 'heartbeat' package to stop Jetbot's movement.

This is a way to check if the connection between Jetbot and the browser still exists. You can adjust the heartbeat period (in seconds) by executing the slider after the code is executed. If the heartbeat cannot communicate back and forth between browsers, the heartbeat 'status' attribute value will be Will be set to dead, once the connection is restored, the status property will be set to alive.

image.png The corresponding complete source code is located at:

/home/jetbot/Notebook/9.Basic movement of Jetbot/Basic movement of Jetbot.ipynb


Buy Yahboom Jetbot AI robot with HD Camera Coding with Python for Jetson Nano


Leave a comment

Please note, comments must be approved before they are published