RubyKaigi2015 making robots-with-mruby

103
Making Robots with mruby 2015-12-12 RubyKaigi 2015 Yurie Yamane Team Yamanekko

Transcript of RubyKaigi2015 making robots-with-mruby

Page 1: RubyKaigi2015 making robots-with-mruby

Making Robots with mruby

2015-12-12 RubyKaigi 2015 Yurie Yamane

Team Yamanekko

Page 2: RubyKaigi2015 making robots-with-mruby

Who am I?

やまね ゆりえ(Yurie Yamane)     @yuri_at_earth(only in Japanese)

a member of TOPPERS project ET robocon staff たいやき部(taiyaki-bu)

Page 3: RubyKaigi2015 making robots-with-mruby

Yamanekko

https://github.com/yamanekko

Page 4: RubyKaigi2015 making robots-with-mruby

2012

Page 5: RubyKaigi2015 making robots-with-mruby

2013

Page 6: RubyKaigi2015 making robots-with-mruby

2014

nomitory

Page 7: RubyKaigi2015 making robots-with-mruby

2015

Page 8: RubyKaigi2015 making robots-with-mruby

Robots and Us

Page 9: RubyKaigi2015 making robots-with-mruby

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.”https://www.youtube.com/watch?v=EzgJWwAx8Mo

Page 10: RubyKaigi2015 making robots-with-mruby

PART 1: Self-balancing Robot using LEGO EV3

Page 11: RubyKaigi2015 making robots-with-mruby

闇RubyKaigi(2011)

http://gihyo.jp/news/report/01/rubykaigi2011/0001?page=7

Page 12: RubyKaigi2015 making robots-with-mruby

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

Page 13: RubyKaigi2015 making robots-with-mruby

ETロボコンAdvent Calendar 2015

http://www.adventar.org/calendars/734

Page 14: RubyKaigi2015 making robots-with-mruby

ET Robot Contest• aka ET ロボコン(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: RubyKaigi2015 making robots-with-mruby

EV3way: SoftwareSystem OS Language

TOPPERS/EV3RT TOPPERS RTOS C, C++

MonoBrick Linux C#

leJOS EV3 Linux Java

Page 16: RubyKaigi2015 making robots-with-mruby

EV3way: Software

• mruby on EV3RT + TECS

• mrby Forum ver.

• yamanekko ver.

System OS Language

TOPPERS/EV3RT TOPPERS RTOS C + mruby

Page 17: RubyKaigi2015 making robots-with-mruby

mruby-ev3rt

Page 18: RubyKaigi2015 making robots-with-mruby

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

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

Page 19: RubyKaigi2015 making robots-with-mruby

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

• TOPPERS’ Products

• Realtime Kernel

• Automotive Kernel

• Tools

• TOPPERS + TSUBAME(swallow) -> TOPAME

TOPPERS Project

とぱめ(TOPAME)https://www.toppers.jp/

Page 20: RubyKaigi2015 making robots-with-mruby

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 21: RubyKaigi2015 making robots-with-mruby

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 22: RubyKaigi2015 making robots-with-mruby

TOPPERS EV3RTunprivileged mode (user mode)

privileged mode (kernel mode)

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

Page 23: RubyKaigi2015 making robots-with-mruby

Self Balancing Robot

Page 24: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 25: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 26: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 27: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 28: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 29: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 30: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 31: RubyKaigi2015 making robots-with-mruby

How Robot Works

Target BoardSensorsActuator

Page 32: RubyKaigi2015 making robots-with-mruby

Gyro Sensor

Ultrasonic Sensor

Motor

Light Sensor

Page 33: RubyKaigi2015 making robots-with-mruby

How Robot Works

Target BoardSensorsActuator

Read

Page 34: RubyKaigi2015 making robots-with-mruby

How Robot Works

Target BoardSensorsActuator

Move

Page 35: RubyKaigi2015 making robots-with-mruby

How Robot Works

sense

calculate

control motor

execute periodically

Page 36: RubyKaigi2015 making robots-with-mruby

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 37: RubyKaigi2015 making robots-with-mruby

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 38: RubyKaigi2015 making robots-with-mruby

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 39: RubyKaigi2015 making robots-with-mruby

All tasks are mruby

main_task

balance_task

watch_task

HIGH priority

LOW priority

balancer.rb

watch.rb

app_ruby.rb

注)バランス制御に重点を置いた例。   これが正しい方法というわけではない

Page 40: RubyKaigi2015 making robots-with-mruby

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 41: RubyKaigi2015 making robots-with-mruby

watch.rb

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

Page 42: RubyKaigi2015 making robots-with-mruby

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 43: RubyKaigi2015 making robots-with-mruby

Classes in mruby-ev3rtMotor

TailMotor

Sensor

GyroSensorColorSensor

TouchSensor

LED LCD Clock Serial Task

Balancer

Button

Page 44: RubyKaigi2015 making robots-with-mruby

PART 2:

DIY Self-balancing Robot

Page 45: RubyKaigi2015 making robots-with-mruby

DIY Self-balancing Robot

Can we make our own robots from scratch?

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

Page 46: RubyKaigi2015 making robots-with-mruby

DIY Self-balancing Robot

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

Page 47: RubyKaigi2015 making robots-with-mruby
Page 48: RubyKaigi2015 making robots-with-mruby

Collecting Parts

Target BoardSensors Actuator

Page 49: RubyKaigi2015 making robots-with-mruby

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 50: RubyKaigi2015 making robots-with-mruby

Raspberry Pi A+

😿

Page 51: RubyKaigi2015 making robots-with-mruby

ARM Family

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

Pi1 Pi2

EV3STM32F4

Page 52: RubyKaigi2015 making robots-with-mruby

Inverted Pendulum

Page 53: RubyKaigi2015 making robots-with-mruby

How to measure an angle

Page 54: RubyKaigi2015 making robots-with-mruby

Gyro Sensor

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

Page 55: RubyKaigi2015 making robots-with-mruby

Gyro Sensor

• ST Micro L3GD20

• 3-axis digital gyroscope

• use 8 pin DIP module by Akizuki Denshi

Page 56: RubyKaigi2015 making robots-with-mruby

Gyro Sensor

😿

Page 57: RubyKaigi2015 making robots-with-mruby

Gyro Sensor

🙀

Page 58: RubyKaigi2015 making robots-with-mruby

Measuring the Angle

!1 !2

Page 59: RubyKaigi2015 making robots-with-mruby
Page 60: RubyKaigi2015 making robots-with-mruby

How to rotate wheels

Page 61: RubyKaigi2015 making robots-with-mruby

DC Motor

• TAMIYA FA-130 Motor

• speed control by voltage

• IO cannot change voltage

• 3.3 V (fixed)

• → use PWM

Page 62: RubyKaigi2015 making robots-with-mruby

DC Motor

😿

Page 63: RubyKaigi2015 making robots-with-mruby
Page 64: RubyKaigi2015 making robots-with-mruby

(Motor + Gear) x 2

Page 65: RubyKaigi2015 making robots-with-mruby
Page 66: RubyKaigi2015 making robots-with-mruby

Wheel x 2

Page 67: RubyKaigi2015 making robots-with-mruby

Body

Tamiya 70157 Universal Plate Set

Page 68: RubyKaigi2015 making robots-with-mruby
Page 69: RubyKaigi2015 making robots-with-mruby

Batteries (for motors)

Page 70: RubyKaigi2015 making robots-with-mruby

Batteries (for motors)

🙀

Page 71: RubyKaigi2015 making robots-with-mruby

Battery (for Pi A+)

Page 72: RubyKaigi2015 making robots-with-mruby
Page 73: RubyKaigi2015 making robots-with-mruby

connect a sensor

?

Page 74: RubyKaigi2015 making robots-with-mruby

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 75: RubyKaigi2015 making robots-with-mruby

Serial Peripheral Interface (SPI)

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

Page 76: RubyKaigi2015 making robots-with-mruby

Demo

Page 77: RubyKaigi2015 making robots-with-mruby

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 78: RubyKaigi2015 making robots-with-mruby

connect motors

?

Page 79: RubyKaigi2015 making robots-with-mruby

PWM• Pulse-Width Modulation

• pseudo-analog signal output

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

Page 80: RubyKaigi2015 making robots-with-mruby

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 81: RubyKaigi2015 making robots-with-mruby

Motor Driver

ST Micro L298N

Page 82: RubyKaigi2015 making robots-with-mruby

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 83: RubyKaigi2015 making robots-with-mruby

Demo

Page 84: RubyKaigi2015 making robots-with-mruby

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 85: RubyKaigi2015 making robots-with-mruby

JTAG• For Debugger

• Doesn’t have to use it

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

Page 86: RubyKaigi2015 making robots-with-mruby

Pinout

Page 87: RubyKaigi2015 making robots-with-mruby

Making Balancer class

Page 88: RubyKaigi2015 making robots-with-mruby

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 89: RubyKaigi2015 making robots-with-mruby

Angle

✓ ✓

Page 90: RubyKaigi2015 making robots-with-mruby

Angular Velocity! !(fast) (slow)

Page 91: RubyKaigi2015 making robots-with-mruby

Velocity(fast) (slow)vv

Page 92: RubyKaigi2015 making robots-with-mruby

Position(far) (near)y asixsy asixs

Page 93: RubyKaigi2015 making robots-with-mruby

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 94: RubyKaigi2015 making robots-with-mruby

Balancing Equation

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

a ≒ Power v ≒ ∫ Power x ≒ ∬ Power

P = K

!

! +K

✓ +K

v

v +K

x

x

Page 95: RubyKaigi2015 making robots-with-mruby

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 96: RubyKaigi2015 making robots-with-mruby

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 97: RubyKaigi2015 making robots-with-mruby

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 98: RubyKaigi2015 making robots-with-mruby

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 99: RubyKaigi2015 making robots-with-mruby

demo

Page 100: RubyKaigi2015 making robots-with-mruby
Page 101: RubyKaigi2015 making robots-with-mruby

まずは何か作ってみよう!

Page 102: RubyKaigi2015 making robots-with-mruby

Source code

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

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

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

Page 103: RubyKaigi2015 making robots-with-mruby

Thank you & Domo Arigato!

ZZZ...

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