Basic Motion
Start Jetbot
方法一
import time
from jetbot import Robot
robot = Robot()
robot.left(speed=0.3)
time.sleep(0.5)
robot.stop()
方法二
import time
from jetbot import Robot
robot.set_motors(0.3, 0.6)
time.sleep(1.0)
robot.stop()
方法三
import time
from jetbot import Robot
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
Controll Jetbot with Sliders
from jetbot import Robot
from IPython.display import display
import time
import traitlets
import ipywidgets.widgets as widgets
# 建立robot物件
robot = Robot()
# create two sliders with range [-1.0, 1.0]
left_slider = widgets.FloatSlider(description='left', min=-1.0, max=1.0, step=0.01, orientation='vertical')
right_slider = widgets.FloatSlider(description='right', min=-1.0, max=1.0, step=0.01, orientation='vertical')
# create a horizontal box container to place the sliders next to each other
slider_container = widgets.HBox([left_slider, right_slider])
# display the container in this cell's output
display(slider_container)
#將兩個變數雙向連結
left_link = traitlets.link((left_slider, 'value'), (robot.left_motor, 'value'))
right_link = traitlets.link((right_slider, 'value'), (robot.right_motor, 'value'))
robot.forward(0.3)
time.sleep(10.0)
robot.stop()
#解除連結
#left_link.unlink()
#right_link.unlink()
#單向連結,僅顯示馬達數值,無法透過滑桿控制馬達
#left_link = traitlets.dlink((robot.left_motor, 'value'), (left_slider, 'value'))
#right_link = traitlets.dlink((robot.right_motor, 'value'), (right_slider, 'value'))
#停止
#robot.stop()
Controll Jetbot with Buttons
import time
from jetbot import Robot
import ipywidgets.widgets as widgets
from IPython.display import display
# 建立robot物件
robot = Robot()
# 建立按鈕物件
button_layout = widgets.Layout(width='100px', height='80px', align_self='center')
stop_button = widgets.Button(description='stop', button_style='danger', layout=button_layout)
forward_button = widgets.Button(description='forward', layout=button_layout)
backward_button = widgets.Button(description='backward', layout=button_layout)
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], layout=widgets.Layout(align_self='center'))
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()
# 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)
Check Jetbot Online
from jetbot import Heartbeat
from jetbot import Robot
import ipywidgets.widgets as widgets
from IPython.display import display
import traitlets
heartbeat = Heartbeat()
robot = Robot()
# 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=0.5, step=0.01, value=0.5)
traitlets.dlink((period_slider, 'value'), (heartbeat, 'period'))
display(period_slider, heartbeat.pulseout)
Teleoperation
Snapshot
from jetbot import Camera
from jetbot import bgr8_to_jpeg
import ipywidgets.widgets as widgets
import traitlets
camera = Camera.instance()
image = widgets.Image(format='jpeg', width=300, height=300)
display(image)
camera_link = traitlets.dlink((camera, 'value'), (image, 'value'), transform=bgr8_to_jpeg)
import uuid
import subprocess
#建立
button_layout = widgets.Layout(width='100px', height='80px', align_self='center')
snapshot_button = widgets.Button(description='shot' , layout=button_layout)
display(snapshot_button)
#建立snapshots資料夾
subprocess.call(['mkdir', '-p', 'snapshots'])
snapshot_image = widgets.Image(format='jpeg', width=300, height=300)
def save_snapshot(change):
# save snapshot when button is pressed down
file_path = 'snapshots/' + str(uuid.uuid1()) + '.jpg'
# write snapshot to file (we use image value instead of camera because it's already in JPEG format)
with open(file_path, 'wb') as f:
f.write(image.value)
# display snapshot that was saved
snapshot_image.value = image.value
snapshot_button.on_click(save_snapshot)