2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Jetbot, RCカーにラインフォローを追加

Last updated at Posted at 2020-12-20

前回無事にラインフォローまで動きましたので、仕上げに単純なRCカー機能とラインフォロー機能の入り切りスイッチを付けました。
こちらが動画です。
https://www.youtube.com/watch?v=n6vvEuCAGPk
コードは「Jetbot, CNNでラインフォローから停止まで」のcreate a function(camera's value changes)を改造しています。
追加したコントローラー、basic_motion.ipynbを参考にしました。
controller.PNG
(コントローラ部分を追加しました)

live_demo.ipynb
import ipywidgets.widgets as widgets

# create buttons
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)

automode = 0
manualR = 0
manualL = 0

def stop(change):
    global manualR, manualL
    manualR = 0
    manualL = 0
    
def step_forward(change):
    global manualR, manualL
    manualR = 0.2
    manualL = 0.2

def step_backward(change):
    global manualR, manualL
    manualR = -0.2
    manualL = -0.2
    
def step_left(change):
    global manualR, manualL
    manualR = 0.2
    manualL = -0.2

def step_right(change):
    global manualR, manualL
    manualR = -0.2
    manualL = 0.2
    
# 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)



#set automode
from ipywidgets import ToggleButton


def sos(change):
    global automode
    automode = 1
    
def sos0(change):
    global automode
    automode = 0


toggle_button_x = ToggleButton(description='auto')
toggle_button_y = ToggleButton(description='manual')
toggle_button_x.observe(sos, names='value')
toggle_button_y.observe(sos0, names='value')
display(toggle_button_x, toggle_button_y)

(変更あり)

live_demo.ipynb
import time
angle = 0.0
angle_last = 0.0

def execute(change):
    global angle, angle_last, automode, manualR, manualL

    if automode ==1:
        image = change['new']

        image1 = preprocess(image)

        xy = Steer_net(image1).detach().float().cpu().numpy().flatten()
        x = xy[0]
        y = (0.5 - xy[1]) / 2.0

        xy0_slider.value = x
        xy1_slider.value = y

        speed_slider.value = speed_gain_slider.value

        angle = x
        #pid = (angle + angle_last)/2 * steering_gain_slider.value
        pid =angle * steering_gain_slider.value
        #angle_last = x

        steering_slider.value = steering_bias_slider.value +     pid

        if y > 0.2 :
            robot.forward(0.0)

        else:
            robot.left_motor.value = max(min(speed_slider.value + steering_slider.value, 1.0), 0.025)
            robot.right_motor.value = max(min(speed_slider.value - steering_slider.value, 1.0), 0.025)

    else:
        robot.left_motor.value = manualL
        robot.right_motor.value = manualR

       
    time.sleep(0.001)
    
execute({'new': camera.value})

#よかったら、LGTMお願いします!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?