112
Domo Arigato, Mr(uby) Roboto 2015-11-17 RubyConf 2015 Yurie Yamane, Masayoshi Takahashi Team Yamanekko

Domo Arigato, Mr(uby) Roboto

Embed Size (px)

Citation preview

Page 1: Domo Arigato, Mr(uby) Roboto

Domo Arigato, Mr(uby) Roboto

2015-11-17 RubyConf 2015 Yurie Yamane, Masayoshi Takahashi

Team Yamanekko

Page 2: Domo Arigato, Mr(uby) Roboto

ドモ アリガト ミスター ロボット

おかげで今年もここに立つことができたよ!

DOMO ARIGATO, MR ROBOTO WE ARE HAPPY TO MEET YOU AT RUBYCONF 2015!

Unfortunately, we are not kilroy and don’t sing a song.Kilroy was not here.

But we would like to say heartily, we are very happy to talk at rubyconf again, and say thank you to give us a chance.

どうもありがとう!

Page 3: Domo Arigato, Mr(uby) Roboto

Robots and Us

Page 4: Domo Arigato, Mr(uby) Roboto

http://www.amazon.co.jp/dp/B00H6V3BWA/

Page 5: Domo Arigato, Mr(uby) Roboto

http://global.yamaha-motor.com/jp/showroom/event/2015tokyomotorshow/sp/exhibitionmodels/mgp/

“There are probably some things in this world that only I can do.”

Page 6: Domo Arigato, Mr(uby) Roboto

https://robohon.com/special/

Page 7: Domo Arigato, Mr(uby) Roboto

About Us

•Yurie Yamane @yuri_at_earth

•Masayoshi Takahashi @takahashim

Page 8: Domo Arigato, Mr(uby) Roboto

Yamanekko

Page 9: Domo Arigato, Mr(uby) Roboto

PART 1: Self-balancing Robot using LEGO EV3

Page 10: Domo Arigato, Mr(uby) Roboto

Self Balancing Robot

Page 11: Domo Arigato, Mr(uby) Roboto
Page 12: Domo Arigato, Mr(uby) Roboto

http://www.mext.go.jp/english/relatedsites/1303116.htm

Page 13: Domo Arigato, Mr(uby) Roboto

http://www.etrobo.jp/2015/

Page 14: Domo Arigato, Mr(uby) Roboto

ET Robot Contest• aka “ET ROBOCON”

• ET means “Embedded Technology”

• To develop the advanced skills of embedded technology such as modeling, designing, and developing embedded systems

• “one-make racing” style (all robots have same designs)

Page 15: Domo Arigato, Mr(uby) Roboto

EV3way• Self-Balancing Two-

Wheeled Robot

• have a light sensor and track a line on the floor

• Hardware:LEGO Mindstorms EV3

• Old EV3way used Mindstorms NXT

Page 16: Domo Arigato, Mr(uby) Roboto

EV3way: SoftwareSystem OS Language

TOPPERS/EV3RT TOPPERS RTOS C, C++

MonoBrick Linux C#

leJOS EV3 Linux Java

Page 17: Domo Arigato, Mr(uby) Roboto

EV3way: SoftwareSystem OS Language

TOPPERS/EV3RT TOPPERS RTOS C + mruby

Page 18: Domo Arigato, Mr(uby) Roboto

mruby-ev3rt

Page 19: Domo Arigato, Mr(uby) Roboto

mruby-ev3rt demoSep. 20th, 2015 at Fukuyama, Hiroshima Pref.

Movie by A. Hirai with ET Robocon Staff@chu-shikoku

Page 20: Domo Arigato, Mr(uby) Roboto

• TOPPERS(Toyohashi OPen Platform for Embedded Real-time Systems)

• TOPPERS’ Products

• RTOS Kernel

• TINET (TCP/IP Protocol Stack)

• SafeG (dual-OS monitor)

• TOPPERS + TSUBAME(swallow) -> TOPAME

TOPPERS Project

TOPAME

Page 21: Domo Arigato, Mr(uby) Roboto

http://toppers.jp/en/index.html

Page 22: Domo Arigato, Mr(uby) Roboto

Who uses TOPPERS?

http://ev.nissan.co.jp/LEAF/?pfa=01http://www.ricoh.co.jp/printer/sg/3100ke/

http://www.korg.com/jp/products/dj/electribe/http://music.casio.com/ja/products/digital_pianos/ghs/products/

https://en.wikipedia.org/wiki/H-IIBfrom: http://toppers.jp/applications.html

Page 23: Domo Arigato, Mr(uby) Roboto

TOPPERS Kernel Roadmap

ATK1 ATK2 ATK3

FMP3

HRP3

ASP3

2000 2010 2020

1st generation 3rd generation2nd generation

ITRON

Automotive

FDMP

HRP

FI4

JSP

Dynamicgen.

ASP Safety

HRP2

FMP

ASPSSP

http://toppers.jp/docs/intro-invite.pdf

Page 24: Domo Arigato, Mr(uby) Roboto

TOPPERS EV3RTunprivileged mode (user mode)

privileged mode (kernel mode)

http://dev.toppers.jp/trac_user/ev3pf/wiki/WhatsEV3RT

Page 25: Domo Arigato, Mr(uby) Roboto

TOPPERS EV3RT• uses Newlib API https://sourceware.org/

newlib/

• File systems are limited (FatFs)

• no STDIO

• Not POSIX API

MRI

Page 26: Domo Arigato, Mr(uby) Roboto

https://github.com/h2so5/mruby-logo

Page 27: Domo Arigato, Mr(uby) Roboto

🎉mruby 1.2.0 released today!👏

Page 28: Domo Arigato, Mr(uby) Roboto

mruby in server-side

Page 29: Domo Arigato, Mr(uby) Roboto

mruby for CLI

Page 30: Domo Arigato, Mr(uby) Roboto

mruby-ev3rt

Page 31: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 32: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 33: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 34: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 35: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 36: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 37: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 38: Domo Arigato, Mr(uby) Roboto

How Robot Works

Target BoardSensorsActuator

Page 39: Domo Arigato, Mr(uby) Roboto

Gyro Sensor

Ultrasonic Sensor

Motor

Light Sensor

Page 40: Domo Arigato, Mr(uby) Roboto

How Robot Works

Target BoardSensorsActuator

Read

Page 41: Domo Arigato, Mr(uby) Roboto

How Robot Works

Target BoardSensorsActuator

Move

Page 42: Domo Arigato, Mr(uby) Roboto

How Robot Works

sense

calculate

control motor

execute periodically

Page 43: Domo Arigato, Mr(uby) Roboto

What’s RTOS• OS for Real Time System

• Real time system: to make much of Deadlines

• NOTICE: “Real-Time” has several meanings

• “Real-Time” in RTOS is completely different from such as “Real-time Web”

Page 44: Domo Arigato, Mr(uby) Roboto

Time is Resource• In real time

system, Tasks should be done in a given time (deadline)

• RTOS manage “Time” as resources (like memories)

https://www.flickr.com/photos/arjanrichter/3886579525/

Page 45: Domo Arigato, Mr(uby) Roboto

Priority of tasks

• Every tasks have their own priority

• Lower-priority tasks never execute when higher-priority tasks alive

• While higher-priority task is running, lower-priority tasks are not running (into READY status)

Page 46: Domo Arigato, Mr(uby) Roboto

RTOS Tasks

main_task

balance_task

watch_task

controller

observe button

initialize -> sleep

HIGH priority

LOW priority

Page 47: Domo Arigato, Mr(uby) Roboto

All tasks are mruby

main_task

balance_task

watch_task

HIGH priority

LOW priority

balancer.rb

watch.rb

app_ruby.rb

Page 48: Domo Arigato, Mr(uby) Roboto

balancer.rbloop do forward = 30 color = colorSensor.brightness if color >= (LIGHT_WHITE + LIGHT_BLACK)/2 turn = 20 ## turn left else turn = -20 ## turn right end

pwm_left, pwm_right, *args = balancer.calculate_auto(forward, turn, GYRO_OFFSET) leftMotor.pwm = pwm_left rightMotor.pwm = pwm_right EV3RT::Task.sleepend

Page 49: Domo Arigato, Mr(uby) Roboto

watch.rb

loop do break if back_button.pressed? EV3RT::Task.sleepend

Page 50: Domo Arigato, Mr(uby) Roboto

app_ruby.rb## initialize sensors, motors, clock and taskstouchSensor = EV3RT::TouchSensor.new(EV3RT::PORT_1)gyroSensor = EV3RT::GyroSensor.new(EV3RT::PORT_4)leftMotor = EV3RT::Motor.new(EV3RT::PORT_C, EV3RT::LARGE_MOTOR)rightMotor = EV3RT::Motor.new(EV3RT::PORT_B, EV3RT::LARGE_MOTOR)clock = EV3RT::Clock.new()EV3RT::Task.active(EV3RT::BALANCE_TASK_ID) ## initialize balancer

loop do ## waiting button pushed break if touchSensor.pressed? clock.sleep(10)end

EV3RT::Task.start_cyclic(EV3RT::BALANCE_CYC_ID) ## start balancerEV3RT::Task.sleep ## do other tasks

Page 51: Domo Arigato, Mr(uby) Roboto

alarm and cyclic handler

main_task

balance_task

watch_task

500msec cycle

4msec cycle

balance_handler

watch_handler

Page 52: Domo Arigato, Mr(uby) Roboto

Sleep and Wake-up

balance

watch

main

Page 53: Domo Arigato, Mr(uby) Roboto

Classes in mruby-ev3rtMotor

TailMotor

Sensor

GyroSensorColorSensor

TouchSensor

LED LCD Clock Serial Task

Balancer

Button

Page 54: Domo Arigato, Mr(uby) Roboto

PART 2:

DIY Self-balancing Robot

Page 55: Domo Arigato, Mr(uby) Roboto

DIY Self-balancing Robot

Can we make our own robots from scratch?

https://www.flickr.com/photos/84906483@N08/8747337118/

Page 56: Domo Arigato, Mr(uby) Roboto

DIY Self-balancing Robot

😿🙀😸http://www.instructables.com/id/Another-Easier-Inverted-Pendulum/

Page 57: Domo Arigato, Mr(uby) Roboto

Collecting Parts

Target BoardSensors Actuator

Page 58: Domo Arigato, Mr(uby) Roboto

Raspberry Pi A+• Lower battery usage

than Pi B+ and Pi 2

• Works with small USB battery

• Cheeper (< $30) than Pi B+ and Pi 2

Page 59: Domo Arigato, Mr(uby) Roboto

Raspberry Pi A+

😿

Page 60: Domo Arigato, Mr(uby) Roboto

ARM Family

http://www.emcu.it/CortexFamily/CortexFamily.html

Pi1 Pi2

EV3STM32F4

Page 61: Domo Arigato, Mr(uby) Roboto

Inverted Pendulum

Page 62: Domo Arigato, Mr(uby) Roboto

How to measure an angle

Page 63: Domo Arigato, Mr(uby) Roboto

Gyro Sensor

http://akizukidenshi.com/catalog/g/gK-06779/

Page 64: Domo Arigato, Mr(uby) Roboto

Gyro Sensor

• ST Micro L3GD20

• 3-axis digital gyroscope

• use 8 pin DIP module by Akizuki Denshi

Page 65: Domo Arigato, Mr(uby) Roboto

Gyro Sensor

😿

Page 66: Domo Arigato, Mr(uby) Roboto
Page 67: Domo Arigato, Mr(uby) Roboto

Gyro Sensor

🙀

Page 68: Domo Arigato, Mr(uby) Roboto

Measuring the Angle

!1 !2

Page 69: Domo Arigato, Mr(uby) Roboto
Page 70: Domo Arigato, Mr(uby) Roboto

How to rotate wheels

Page 71: Domo Arigato, Mr(uby) Roboto

DC Motor

• TAMIYA FA-130 Motor

• speed control by voltage

• IO cannot change voltage

• 3.3 V (fixed)

• → use PWM

Page 72: Domo Arigato, Mr(uby) Roboto

DC Motor

😿

Page 73: Domo Arigato, Mr(uby) Roboto
Page 74: Domo Arigato, Mr(uby) Roboto

(Motor + Gear) x 2

Page 75: Domo Arigato, Mr(uby) Roboto
Page 76: Domo Arigato, Mr(uby) Roboto

Wheel x 2

Page 77: Domo Arigato, Mr(uby) Roboto

Body

Tamiya 70157 Universal Plate Set

Page 78: Domo Arigato, Mr(uby) Roboto
Page 79: Domo Arigato, Mr(uby) Roboto

Batteries (for motors)

Page 80: Domo Arigato, Mr(uby) Roboto

Batteries (for motors)

🙀

Page 81: Domo Arigato, Mr(uby) Roboto

Battery (for Pi A+)

Page 82: Domo Arigato, Mr(uby) Roboto
Page 83: Domo Arigato, Mr(uby) Roboto

connect a sensor

?

Page 84: Domo Arigato, Mr(uby) Roboto

SPI vs I2C• SPI and I2C are popular protocols

• Gyro sensor L3GD20 supports both

• SPI is more complex, but faster than I2C

• Raspberry Pi supports SPI (see “BCM2835 ARM Peripherals”) • https://www.raspberrypi.org/wp-content/uploads/

2012/02/BCM2835-ARM-Peripherals.pdf

Page 85: Domo Arigato, Mr(uby) Roboto

Serial Peripheral Interface (SPI)

• Master and Slave model • In our case, Raspberry Pi is master • 4 pins (Input, Output, Clock, Select)

Page 86: Domo Arigato, Mr(uby) Roboto

Demo

Page 87: Domo Arigato, Mr(uby) Roboto

Gyro Testserial = Serial.new timer = SystemTimer.new gyro = Gyro.new() serial.puts("time,gyro")

MESURE_COUNTS = 45 start_time = timer.now

cnt = 0 loop do mesure_sum = 0 MESURE_COUNTS.times{ mesure_sum += gyro.read(Gyro::Y) }

omega_i = mesure_sum * 0.00875 / MESURE_COUNTS; cnt +=1 now = ((timer.now - start_time) / 1000).floor if cnt == 10 cnt = 0 serial.puts("#{now},#{omega_i}") end end

Page 88: Domo Arigato, Mr(uby) Roboto

connect motors

?

Page 89: Domo Arigato, Mr(uby) Roboto

PWM• Pulse-Width Modulation

• pseudo-analog signal output

http://www.electronics-tutorials.ws/blog/pulse-width-modulation.html

Page 90: Domo Arigato, Mr(uby) Roboto

PWM• Raspberry Pi A+/B+/2 supports 2 PWMs

• Old Raspberry Pi B support only 1 PWM

• So we can control 2 motors (Right/Left)

Page 91: Domo Arigato, Mr(uby) Roboto

Motor Driver

ST Micro L298N

Page 92: Domo Arigato, Mr(uby) Roboto

Motor Driver

PWMIN1 IN2

OUT1 OUT2

MOTOR

Motor Driver

Raspberry Pi

IN1 IN2 OUT0 0 STOP

0 1 Forward

1 0 Reverse

1 1 Brake

Page 93: Domo Arigato, Mr(uby) Roboto

Demo

Page 94: Domo Arigato, Mr(uby) Roboto

Motor Test# in1, in2, enable, pwm0or1 motor_left = Motor.new(5,6,12,0) motor_right = Motor.new(16,20,19,1)

motor_right.drive(50) motor_left.drive(50) RSRobot.delay(3000000) motor_left.drive(200) motor_right.drive(200) RSRobot.delay(3000000) motor_right.drive(50) motor_left.drive(50) RSRobot.delay(3000000) motor_right.drive(-100) motor_left.drive(-100) RSRobot.delay(3000000) motoy_right.stop motor_left.sop

Page 95: Domo Arigato, Mr(uby) Roboto

JTAG• For Debugger

• Doesn’t have to use it

• but with JTAG, we can use GDB, so it’s very helpful to debug

Page 96: Domo Arigato, Mr(uby) Roboto

Pinout

Page 97: Domo Arigato, Mr(uby) Roboto

Making Balancer class

Page 98: Domo Arigato, Mr(uby) Roboto

Balancing Equation

power = k_1 * (Angular Velocity) + k_2 * (Angle) + k_3 * (Velocity) + k_4 * (Position)

P = K

!

! +K

✓ +K

v

v +K

x

x

Page 99: Domo Arigato, Mr(uby) Roboto

Angle

✓ ✓

Page 100: Domo Arigato, Mr(uby) Roboto

Angular Velocity! !(fast) (slow)

Page 101: Domo Arigato, Mr(uby) Roboto

Velocity(fast) (slow)vv

Page 102: Domo Arigato, Mr(uby) Roboto

Position(far) (near)y asixsy asixs

Page 103: Domo Arigato, Mr(uby) Roboto

Balancing Equation

power = k_1 * omega_i + k_2 * theta_i + k_3 * v_i + k_4 * x_i

P = K

!

! +K

✓ +K

v

v +K

x

x

Page 104: Domo Arigato, Mr(uby) Roboto

Balancing Equation

power = k_1 * omega_i + k_2 * theta_i + k_3 * v_i + k_4 * x_i

P = K

!

! +K

✓ +K

v

v +K

x

x

a ≒ Power v ≒ ∫ Power x ≒ ∬ Power

Page 105: Domo Arigato, Mr(uby) Roboto

Balancing EquationP = K

!

! +K

✓ +K

v

v +K

x

x

= K

!

! +K

X! +K

v

XP +K

x

XXP

power = k_1 * omega_i + k_2 * theta_i + k_3 * v_i + k_4 * x_i

a ≒ Power v ≒ ∫ Power x ≒ ∬ Power

Page 106: Domo Arigato, Mr(uby) Roboto

Balancing Equation

theta_i += omega_i v_i += power x_i += v_i power = k_1 * theta_i + k_2 * omega_i + k_3 * v_i + k_4 * x_i

P = K

!

! +K

✓ +K

v

v +K

x

x

= K

!

! +K

X! +K

v

XP +K

x

XXP

Page 107: Domo Arigato, Mr(uby) Roboto

Balancer classclass Balancer

def calculate(omega_i) @theta_i += omega_i # … t = @k_angle * @theta_i o = @k_omega * omega_i # … power = t + o + … # … return power, power end

end

Page 108: Domo Arigato, Mr(uby) Roboto

main looploop do gyro_value = gyro.measure(Gyro::Y, MESURE_COUNTS)

pwm_left, pwm_right = balancer.calculate(gyro_value)

motor_left.pwm = pwm_left motor_right.pwm = pwm_right end

Page 109: Domo Arigato, Mr(uby) Roboto

demo

Page 110: Domo Arigato, Mr(uby) Roboto
Page 111: Domo Arigato, Mr(uby) Roboto

Source code

• https://github.com/yamanekko/mruby-ev3rt

• https://github.com/yamanekko/mruby-rs-robot

• https://github.com/yamanekko/raspi_robot

Page 112: Domo Arigato, Mr(uby) Roboto

Thank you & Domo Arigato!

ZZZ...

You can contact us on Twitter: @yuri_at_earth && @takahashim Special Thanks to: @tenderlove A. Hirai with ET Robocon Staff@chu-shikoku