SlideShare a Scribd company logo
1 of 8
Download to read offline
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 1/8
Basic Motion
Welcome to JetBot's browser based programming interface! This document is
called a Jupyter
Notebook, which combines text, code, and graphic
display all in one! Pretty neat, huh? If you're
unfamiliar with Jupyter we suggest clicking the Help drop down menu in the top toolbar. This
has useful references for
programming with Jupyter.
In this notebook, we'll cover the basics of controlling JetBot.
Importing the Robot class
To get started programming JetBot, we'll need to import the Robot class. This class
allows us
to easily control the robot's motors! This is contained in the jetbot package.
If you're new to Python, a package is essentially a folder containing code files.
These code files are called modules.
To import the Robot class, highlight the cell below and press ctrl + enter or the play
icon above.
This will execute the code contained in the cell
In [1]:
Now that we've imported the Robot class we can initialize the class instance as follows.
In [2]:
Commanding the robot
Now that we've created our Robot instance we named "robot", we can use this instance
to
control the robot. To make the robot spin counterclockwise at 30% of it's max speed
we can call
the following
WARNING: This next command will make the robot move! Please make sure
the robot has clearance.
In [3]:
Cool, you should see the robot spin counterclockwise!
from jetbot import Robot
robot = Robot()
robot.left(speed=0.3)
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 2/8
If your robot didn't turn left, that means one of the motors is wired backwards!
Try powering down your
robot and swapping the terminals that the red and
black cables of the incorrect motor.
REMINDER: Always be careful to check your wiring, and don't change the
wiring on a running system!
Now, to stop the robot you can call the stop method.
In [4]:
Maybe we only want to run the robot for a set period of time. For that, we can use the Python
time package.
In [5]:
This package defines the sleep function, which causes the code execution to block for the
specified number of seconds
before running the next command. Try the following to make the
robot turn left only for half a second.
In [6]:
Great. You should see the robot turn left for a bit and then stop.
Wondering what happened to the speed= inside the left method? Python
allows us to set function parameters by either their name, or the order that they
are defined
(without specifying the name).
The BasicJetbot class also has the methods right , forward , and backward . Try
creating your own cell to make
the robot move forward at 50% speed for one second.
Create a new cell by highlighting an existing cell and pressing b or the + icon above. Once
you've done that, type in the code that you think will make the robot move forward at 50% speed
for one second.
Controlling motors individually
Above we saw how we can control the robot using commands like left , right , etc. But
what if we want to set each motor speed individually? Well, there are two ways you can do this
The first way is to call the set_motors method. For example, to turn along a left arch for a
second we could set the left motor to 30% and the right motor to 60% like follows.
robot.stop()
import time
robot.left(0.3)
time.sleep(0.5)
robot.stop()
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 3/8
In [7]:
Great! You should see the robot move along a left arch. But actually, there's another way that
we could accomplish the same thing.
The Robot class has two attributes named left_motor and right_motor that
represent each motor individually.
These attributes are Motor class instances, each which
contains a value attribute. This value attribute
is a traitlet
(https://github.com/ipython/traitlets) which generates events when assigned a new value. In
the motor
class, we attach a function that updates the motor commands whenever the value
changes.
So, to accomplish the exact same thing we did above, we could execute the following.
In [8]:
You should see the robot move in the same exact way!
Link motors to traitlets
A really cool feature about these traitlets (https://github.com/ipython/traitlets) is that we can also
link them to other traitlets! This is super handy because Jupyter Notebooks allow us
to make
graphical widgets that use traitlets under the hood. This means we can attach
our motors to
widgets to control them from the browser, or just visualize the value.
To show how to do this, let's create and display two sliders that we'll use to control our motors.
robot.set_motors(0.3, 0.6)
time.sleep(1.0)
robot.stop()
robot.left_motor.value = 0.3
robot.right_motor.value = 0.6
time.sleep(1.0)
robot.left_motor.value = 0.0
robot.right_motor.value = 0.0
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 4/8
In [9]:
You should see two vertical sliders displayed above.
HELPFUL TIP: In Jupyter Lab, you can actually "pop" the output of cells into
entirely separate window! It will still be connected to the notebook, but displayed
separately. This is helpful if we want to pin the output of code we executed
elsewhere.
To do this, right click the output of the cell and select Create New
View for Output . You can then drag the new window
to a location you find
pleasing.
Try clicking and dragging the sliders up and down. Notice nothing happens when we move the
sliders currently. That's because we haven't connected them to motors yet! We'll do that by
using the link function from the traitlets package.
In [10]:
Now try dragging the sliders (slowly at first). You should see the respective motor turn!
The link function that we created above actually creates a bi-directional link! That means,
if
we set the motor values elsewhere, the sliders will update! Try executing the code block below
left
0.00
right
0.00
import ipywidgets.widgets as widgets
from IPython.display import display
# create two sliders with range [-1.0, 1.0]
left_slider = widgets.FloatSlider(description='left', min=-1.0, max=1.0,
right_slider = widgets.FloatSlider(description='right', min=-1.0, max=1.
# create a horizontal box container to place the sliders next to each ot
slider_container = widgets.HBox([left_slider, right_slider])
# display the container in this cell's output
display(slider_container)
import traitlets
left_link = traitlets.link((left_slider, 'value'), (robot.left_motor, 'v
right_link = traitlets.link((right_slider, 'value'), (robot.right_motor,
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 5/8
In [11]:
You should see the sliders respond to the motor commands! If we want to remove this
connection we can call the
 unlink method of each link.
In [12]:
But what if we don't want a bi-directional link, let's say we only want to use the sliders to display
the motor values,
but not control them. For that we can use the dlink function. The left input
is the source and the right input is the target
In [13]:
Now try moving the sliders. You should see that the robot doesn't respond. But when set the
motors using a different method,
the sliders will update and display the value!
Attach functions to events
Another way to use traitlets, is by attaching functions (like forward ) to events. These
functions will get called whenever a change to the object occurs, and will be passed some
information about that change
like the old value and the new value.
Let's create and display some buttons that we'll use to control the robot.
robot.forward(0.3)
time.sleep(1.0)
robot.stop()
left_link.unlink()
right_link.unlink()
left_link = traitlets.dlink((robot.left_motor, 'value'), (left_slider, '
right_link = traitlets.dlink((robot.right_motor, 'value'), (right_slider
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 6/8
In [14]:
You should see a set of robot controls displayed above! But right now they wont do anything. To
do that
we'll need to create some functions that we'll attach to the button's on_click event.
In [15]:
forward
left stop right
backward
# create buttons
button_layout = widgets.Layout(width='100px', height='80px', align_self=
stop_button = widgets.Button(description='stop', button_style='danger',
forward_button = widgets.Button(description='forward', layout=button_lay
backward_button = widgets.Button(description='backward', layout=button_l
left_button = widgets.Button(description='left', layout=button_layout)
right_button = widgets.Button(description='right', layout=button_layout)
# display buttons
middle_box = widgets.HBox([left_button, stop_button, right_button], layo
controls_box = widgets.VBox([forward_button, middle_box, backward_button
display(controls_box)
def stop(change):
robot.stop()
def step_forward(change):
robot.forward(0.4)
time.sleep(0.5)
robot.stop()
def step_backward(change):
robot.backward(0.4)
time.sleep(0.5)
robot.stop()
def step_left(change):
robot.left(0.3)
time.sleep(0.5)
robot.stop()
def step_right(change):
robot.right(0.3)
time.sleep(0.5)
robot.stop()
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 7/8
Now that we've defined the functions, let's attach them to the on-click events of each button
In [16]:
Now when you click each button, you should see the robot move!
Heartbeat Killswitch
Here we show how to connect a 'heartbeat' to stop the robot from moving. This is a simple way
to detect if the robot connection is alive. You can lower the slider below to reduce the period (in
seconds) of the heartbeat. If a round-trip communication between browser cannot be made
within two heartbeats, the ' status ' attribute of the heartbeat will be set dead . As soon as
the connection is restored, the status attribute will return to alive .
In [18]:
Try executing the code below to start the motors, and then lower the slider to see what
happens. You can also try disconnecting your robot or PC.
In [19]:
Conclusion
period 0.50
1640311209.1356633
# link buttons to actions
stop_button.on_click(stop)
forward_button.on_click(step_forward)
backward_button.on_click(step_backward)
left_button.on_click(step_left)
right_button.on_click(step_right)
from jetbot import Heartbeat
heartbeat = Heartbeat()
# this function will be called when heartbeat 'alive' status changes
def handle_heartbeat_status(change):
if change['new'] == Heartbeat.Status.dead:
robot.stop()
heartbeat.observe(handle_heartbeat_status, names='status')
period_slider = widgets.FloatSlider(description='period', min=0.001, max
traitlets.dlink((period_slider, 'value'), (heartbeat, 'period'))
display(period_slider, heartbeat.pulseout)
robot.left(0.2)
# now lower the `period` slider above until the network heartbeat can't
23/12/2021, 18:00 basic_motion - Jupyter Notebook
localhost:8889/notebooks/basic_motion/basic_motion.ipynb 8/8
That's it for this example notebook! Hopefully you feel confident that you can program your
robot to move around now :)

More Related Content

What's hot

Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory MethodsYe Win
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hypeMagdiel Duarte
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingCodemotion
 
Introducing the New Prefab Workflow - Unite LA
Introducing the New Prefab Workflow - Unite LAIntroducing the New Prefab Workflow - Unite LA
Introducing the New Prefab Workflow - Unite LAUnity Technologies
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlinAdit Lal
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android developmentAdit Lal
 
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)Unity Technologies Japan K.K.
 
Android Architecture Components with Kotlin
Android Architecture Components with KotlinAndroid Architecture Components with Kotlin
Android Architecture Components with KotlinAdit Lal
 

What's hot (10)

Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
 
Java applet
Java appletJava applet
Java applet
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hype
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Swing
SwingSwing
Swing
 
Introducing the New Prefab Workflow - Unite LA
Introducing the New Prefab Workflow - Unite LAIntroducing the New Prefab Workflow - Unite LA
Introducing the New Prefab Workflow - Unite LA
 
Practical tips for building apps with kotlin
Practical tips for building apps with kotlinPractical tips for building apps with kotlin
Practical tips for building apps with kotlin
 
Save time with kotlin in android development
Save time with kotlin in android developmentSave time with kotlin in android development
Save time with kotlin in android development
 
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
【Unite 2017 Tokyo】パフォーマンス向上のためのスクリプトのベストプラクティス(note付き)
 
Android Architecture Components with Kotlin
Android Architecture Components with KotlinAndroid Architecture Components with Kotlin
Android Architecture Components with Kotlin
 

Similar to JetBot basic motion

Using prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesUsing prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesDavid Voyles
 
Bowtie: Interactive Dashboards
Bowtie: Interactive DashboardsBowtie: Interactive Dashboards
Bowtie: Interactive DashboardsJacques Kvam
 
Unity3d scripting tutorial
Unity3d scripting tutorialUnity3d scripting tutorial
Unity3d scripting tutorialhungnttg
 
The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...Katy Slemon
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3dDao Tung
 
TP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfTP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfkiiway01
 
Reactotron - A Debugging Agent
Reactotron -  A Debugging AgentReactotron -  A Debugging Agent
Reactotron - A Debugging AgentMatthieu Vachon
 
Real Time Robot Control System Software Description_3
Real Time Robot Control System Software Description_3Real Time Robot Control System Software Description_3
Real Time Robot Control System Software Description_3Jerry Sommerville
 
Gui builder
Gui builderGui builder
Gui builderlearnt
 
Goal The goal is to build a Maze-runner special edition of your .pdf
Goal The goal is to build a Maze-runner special edition of your .pdfGoal The goal is to build a Maze-runner special edition of your .pdf
Goal The goal is to build a Maze-runner special edition of your .pdfaucmistry
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Build UI of the Future with React 360
Build UI of the Future with React 360Build UI of the Future with React 360
Build UI of the Future with React 360RapidValue
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxShubhamJayswal6
 
Toy robot simulator
Toy robot simulatorToy robot simulator
Toy robot simulatorpauldeng
 
Getting Started.docx - Unreal Tournament 3 Bots for .NET
Getting Started.docx - Unreal Tournament 3 Bots for .NETGetting Started.docx - Unreal Tournament 3 Bots for .NET
Getting Started.docx - Unreal Tournament 3 Bots for .NETbutest
 
Desenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhoneDesenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhoneTiago Oliveira
 

Similar to JetBot basic motion (20)

Using prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesUsing prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile services
 
Bowtie: Interactive Dashboards
Bowtie: Interactive DashboardsBowtie: Interactive Dashboards
Bowtie: Interactive Dashboards
 
Unity3d scripting tutorial
Unity3d scripting tutorialUnity3d scripting tutorial
Unity3d scripting tutorial
 
The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3d
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
 
TP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfTP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdf
 
Reactotron - A Debugging Agent
Reactotron -  A Debugging AgentReactotron -  A Debugging Agent
Reactotron - A Debugging Agent
 
Real Time Robot Control System Software Description_3
Real Time Robot Control System Software Description_3Real Time Robot Control System Software Description_3
Real Time Robot Control System Software Description_3
 
6. Compile And Run
6. Compile And Run6. Compile And Run
6. Compile And Run
 
Gui builder
Gui builderGui builder
Gui builder
 
Goal The goal is to build a Maze-runner special edition of your .pdf
Goal The goal is to build a Maze-runner special edition of your .pdfGoal The goal is to build a Maze-runner special edition of your .pdf
Goal The goal is to build a Maze-runner special edition of your .pdf
 
RobotStudiopp.ppt
RobotStudiopp.pptRobotStudiopp.ppt
RobotStudiopp.ppt
 
ie450RobotStudio.ppt
ie450RobotStudio.pptie450RobotStudio.ppt
ie450RobotStudio.ppt
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Build UI of the Future with React 360
Build UI of the Future with React 360Build UI of the Future with React 360
Build UI of the Future with React 360
 
Streamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptxStreamlining React Component Development and Sharing with bit.pptx
Streamlining React Component Development and Sharing with bit.pptx
 
Toy robot simulator
Toy robot simulatorToy robot simulator
Toy robot simulator
 
Getting Started.docx - Unreal Tournament 3 Bots for .NET
Getting Started.docx - Unreal Tournament 3 Bots for .NETGetting Started.docx - Unreal Tournament 3 Bots for .NET
Getting Started.docx - Unreal Tournament 3 Bots for .NET
 
Desenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhoneDesenvolva um game para android ou iPhone
Desenvolva um game para android ou iPhone
 

Recently uploaded

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

JetBot basic motion

  • 1. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 1/8 Basic Motion Welcome to JetBot's browser based programming interface! This document is called a Jupyter Notebook, which combines text, code, and graphic display all in one! Pretty neat, huh? If you're unfamiliar with Jupyter we suggest clicking the Help drop down menu in the top toolbar. This has useful references for programming with Jupyter. In this notebook, we'll cover the basics of controlling JetBot. Importing the Robot class To get started programming JetBot, we'll need to import the Robot class. This class allows us to easily control the robot's motors! This is contained in the jetbot package. If you're new to Python, a package is essentially a folder containing code files. These code files are called modules. To import the Robot class, highlight the cell below and press ctrl + enter or the play icon above. This will execute the code contained in the cell In [1]: Now that we've imported the Robot class we can initialize the class instance as follows. In [2]: Commanding the robot Now that we've created our Robot instance we named "robot", we can use this instance to control the robot. To make the robot spin counterclockwise at 30% of it's max speed we can call the following WARNING: This next command will make the robot move! Please make sure the robot has clearance. In [3]: Cool, you should see the robot spin counterclockwise! from jetbot import Robot robot = Robot() robot.left(speed=0.3)
  • 2. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 2/8 If your robot didn't turn left, that means one of the motors is wired backwards! Try powering down your robot and swapping the terminals that the red and black cables of the incorrect motor. REMINDER: Always be careful to check your wiring, and don't change the wiring on a running system! Now, to stop the robot you can call the stop method. In [4]: Maybe we only want to run the robot for a set period of time. For that, we can use the Python time package. In [5]: This package defines the sleep function, which causes the code execution to block for the specified number of seconds before running the next command. Try the following to make the robot turn left only for half a second. In [6]: Great. You should see the robot turn left for a bit and then stop. Wondering what happened to the speed= inside the left method? Python allows us to set function parameters by either their name, or the order that they are defined (without specifying the name). The BasicJetbot class also has the methods right , forward , and backward . Try creating your own cell to make the robot move forward at 50% speed for one second. Create a new cell by highlighting an existing cell and pressing b or the + icon above. Once you've done that, type in the code that you think will make the robot move forward at 50% speed for one second. Controlling motors individually Above we saw how we can control the robot using commands like left , right , etc. But what if we want to set each motor speed individually? Well, there are two ways you can do this The first way is to call the set_motors method. For example, to turn along a left arch for a second we could set the left motor to 30% and the right motor to 60% like follows. robot.stop() import time robot.left(0.3) time.sleep(0.5) robot.stop()
  • 3. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 3/8 In [7]: Great! You should see the robot move along a left arch. But actually, there's another way that we could accomplish the same thing. The Robot class has two attributes named left_motor and right_motor that represent each motor individually. These attributes are Motor class instances, each which contains a value attribute. This value attribute is a traitlet (https://github.com/ipython/traitlets) which generates events when assigned a new value. In the motor class, we attach a function that updates the motor commands whenever the value changes. So, to accomplish the exact same thing we did above, we could execute the following. In [8]: You should see the robot move in the same exact way! Link motors to traitlets A really cool feature about these traitlets (https://github.com/ipython/traitlets) is that we can also link them to other traitlets! This is super handy because Jupyter Notebooks allow us to make graphical widgets that use traitlets under the hood. This means we can attach our motors to widgets to control them from the browser, or just visualize the value. To show how to do this, let's create and display two sliders that we'll use to control our motors. robot.set_motors(0.3, 0.6) time.sleep(1.0) robot.stop() robot.left_motor.value = 0.3 robot.right_motor.value = 0.6 time.sleep(1.0) robot.left_motor.value = 0.0 robot.right_motor.value = 0.0
  • 4. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 4/8 In [9]: You should see two vertical sliders displayed above. HELPFUL TIP: In Jupyter Lab, you can actually "pop" the output of cells into entirely separate window! It will still be connected to the notebook, but displayed separately. This is helpful if we want to pin the output of code we executed elsewhere. To do this, right click the output of the cell and select Create New View for Output . You can then drag the new window to a location you find pleasing. Try clicking and dragging the sliders up and down. Notice nothing happens when we move the sliders currently. That's because we haven't connected them to motors yet! We'll do that by using the link function from the traitlets package. In [10]: Now try dragging the sliders (slowly at first). You should see the respective motor turn! The link function that we created above actually creates a bi-directional link! That means, if we set the motor values elsewhere, the sliders will update! Try executing the code block below left 0.00 right 0.00 import ipywidgets.widgets as widgets from IPython.display import display # create two sliders with range [-1.0, 1.0] left_slider = widgets.FloatSlider(description='left', min=-1.0, max=1.0, right_slider = widgets.FloatSlider(description='right', min=-1.0, max=1. # create a horizontal box container to place the sliders next to each ot slider_container = widgets.HBox([left_slider, right_slider]) # display the container in this cell's output display(slider_container) import traitlets left_link = traitlets.link((left_slider, 'value'), (robot.left_motor, 'v right_link = traitlets.link((right_slider, 'value'), (robot.right_motor,
  • 5. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 5/8 In [11]: You should see the sliders respond to the motor commands! If we want to remove this connection we can call the unlink method of each link. In [12]: But what if we don't want a bi-directional link, let's say we only want to use the sliders to display the motor values, but not control them. For that we can use the dlink function. The left input is the source and the right input is the target In [13]: Now try moving the sliders. You should see that the robot doesn't respond. But when set the motors using a different method, the sliders will update and display the value! Attach functions to events Another way to use traitlets, is by attaching functions (like forward ) to events. These functions will get called whenever a change to the object occurs, and will be passed some information about that change like the old value and the new value. Let's create and display some buttons that we'll use to control the robot. robot.forward(0.3) time.sleep(1.0) robot.stop() left_link.unlink() right_link.unlink() left_link = traitlets.dlink((robot.left_motor, 'value'), (left_slider, ' right_link = traitlets.dlink((robot.right_motor, 'value'), (right_slider
  • 6. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 6/8 In [14]: You should see a set of robot controls displayed above! But right now they wont do anything. To do that we'll need to create some functions that we'll attach to the button's on_click event. In [15]: forward left stop right backward # create buttons button_layout = widgets.Layout(width='100px', height='80px', align_self= stop_button = widgets.Button(description='stop', button_style='danger', forward_button = widgets.Button(description='forward', layout=button_lay backward_button = widgets.Button(description='backward', layout=button_l left_button = widgets.Button(description='left', layout=button_layout) right_button = widgets.Button(description='right', layout=button_layout) # display buttons middle_box = widgets.HBox([left_button, stop_button, right_button], layo controls_box = widgets.VBox([forward_button, middle_box, backward_button display(controls_box) def stop(change): robot.stop() def step_forward(change): robot.forward(0.4) time.sleep(0.5) robot.stop() def step_backward(change): robot.backward(0.4) time.sleep(0.5) robot.stop() def step_left(change): robot.left(0.3) time.sleep(0.5) robot.stop() def step_right(change): robot.right(0.3) time.sleep(0.5) robot.stop()
  • 7. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 7/8 Now that we've defined the functions, let's attach them to the on-click events of each button In [16]: Now when you click each button, you should see the robot move! Heartbeat Killswitch Here we show how to connect a 'heartbeat' to stop the robot from moving. This is a simple way to detect if the robot connection is alive. You can lower the slider below to reduce the period (in seconds) of the heartbeat. If a round-trip communication between browser cannot be made within two heartbeats, the ' status ' attribute of the heartbeat will be set dead . As soon as the connection is restored, the status attribute will return to alive . In [18]: Try executing the code below to start the motors, and then lower the slider to see what happens. You can also try disconnecting your robot or PC. In [19]: Conclusion period 0.50 1640311209.1356633 # link buttons to actions stop_button.on_click(stop) forward_button.on_click(step_forward) backward_button.on_click(step_backward) left_button.on_click(step_left) right_button.on_click(step_right) from jetbot import Heartbeat heartbeat = Heartbeat() # this function will be called when heartbeat 'alive' status changes def handle_heartbeat_status(change): if change['new'] == Heartbeat.Status.dead: robot.stop() heartbeat.observe(handle_heartbeat_status, names='status') period_slider = widgets.FloatSlider(description='period', min=0.001, max traitlets.dlink((period_slider, 'value'), (heartbeat, 'period')) display(period_slider, heartbeat.pulseout) robot.left(0.2) # now lower the `period` slider above until the network heartbeat can't
  • 8. 23/12/2021, 18:00 basic_motion - Jupyter Notebook localhost:8889/notebooks/basic_motion/basic_motion.ipynb 8/8 That's it for this example notebook! Hopefully you feel confident that you can program your robot to move around now :)