SlideShare a Scribd company logo
Making Robots
with mruby
2015-12-12 RubyKaigi 2015
Yurie Yamane
Team Yamanekko
Who am I?
やまね ゆりえ(Yurie Yamane)  
  @yuri_at_earth(only in Japanese)
a member of TOPPERS project
ET robocon staff
たいやき部(taiyaki-bu)
Yamanekko
https://github.com/yamanekko
2012
2013
2014
nomitory
2015
Robots
and Us
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
PART 1:
Self-
balancing
Robot
using
LEGO EV3
闇RubyKaigi(2011)
http://gihyo.jp/news/report/01/rubykaigi2011/0001?page=7
http://www.etrobo.jp/2015/
ETロボコンAdvent Calendar 2015
http://www.adventar.org/calendars/734
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)
EV3way: Software
System OS Language
TOPPERS/EV3RT TOPPERS RTOS C, C++
MonoBrick Linux C#
leJOS EV3 Linux Java
EV3way: Software
• mruby on EV3RT + TECS
• mrby Forum ver.
• yamanekko ver.
System OS Language
TOPPERS/EV3RT TOPPERS RTOS C + mruby
mruby-ev3rt
mruby-ev3rt demo
Sep. 20th, 2015 at Fukuyama, Hiroshima Pref.
Movie by A. Hirai with ET Robocon Staff@chu-shikoku
• 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/
Who uses TOPPERS?
http://ev.nissan.co.jp/LEAF/?pfa=01
http://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-IIB
from: http://toppers.jp/applications.html
TOPPERS Kernel Roadmap
ATK1 ATK2 ATK3
FMP3
HRP3
ASP3
2000 2010 2020
1st generation 3rd generation2nd generation
ITRON
Automotive
FDMP
HRP
FI4
JSP
Dynamic

gen.
ASP Safety
HRP2
FMP
ASP
SSP
http://toppers.jp/docs/intro-invite.pdf
TOPPERS EV3RT
unprivileged
mode
(user mode)
privileged
mode
(kernel mode)
http://dev.toppers.jp/trac_user/ev3pf/wiki/WhatsEV3RT
Self Balancing Robot
Inverted Pendulum
Inverted Pendulum
Inverted Pendulum
Inverted Pendulum
Inverted Pendulum
Inverted Pendulum
Inverted Pendulum
How Robot Works
Target Board
Sensors
Actuator
Gyro Sensor
Ultrasonic Sensor
Motor
Light Sensor
How Robot Works
Target Board
Sensors
Actuator
Read
How Robot Works
Target Board
Sensors
Actuator
Move
How Robot Works
sense
calculate
control motor
execute
periodically
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”
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/
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)
All tasks are mruby
main_task
balance_task
watch_task
HIGH priority
LOW priority
balancer.rb
watch.rb
app_ruby.rb
注)バランス制御に重点を置いた例。
  これが正しい方法というわけではない
balancer.rb
loop 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.sleep

end
watch.rb
loop do

break if back_button.pressed?

EV3RT::Task.sleep

end
app_ruby.rb
## initialize sensors, motors, clock and tasks

touchSensor = 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 balancer

EV3RT::Task.sleep ## do other tasks
Classes in mruby-ev3rt
Motor
TailMotor
Sensor
GyroSensorColorSensor
TouchSensor
LED LCD Clock Serial Task
Balancer
Button
PART 2:
DIY
Self-
balancing
Robot
DIY Self-balancing Robot
Can we make our own robots
from scratch?
https://www.flickr.com/photos/84906483@N08/8747337118/
DIY Self-balancing Robot
😿🙀😸
http://www.instructables.com/id/Another-Easier-Inverted-Pendulum/
RubyKaigi2015 making robots-with-mruby
Collecting Parts
Target Board
Sensors Actuator
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
Raspberry Pi A+
😿
ARM Family
http://www.emcu.it/CortexFamily/CortexFamily.html
Pi1 Pi2
EV3
STM32F4
Inverted Pendulum
How to measure an angle
Gyro Sensor
http://akizukidenshi.com/catalog/g/gK-06779/
Gyro Sensor
• ST Micro L3GD20
• 3-axis digital
gyroscope
• use 8 pin DIP module
by Akizuki Denshi
Gyro Sensor
😿
Gyro Sensor
🙀
Measuring the Angle
!1 !2
RubyKaigi2015 making robots-with-mruby
How to rotate wheels
DC Motor
• TAMIYA FA-130 Motor
• speed control by voltage
• IO cannot change voltage
• 3.3 V (fixed)
• → use PWM
DC Motor
😿
RubyKaigi2015 making robots-with-mruby
(Motor + Gear) x 2
RubyKaigi2015 making robots-with-mruby
Wheel x 2
Body
Tamiya 70157 Universal Plate Set
RubyKaigi2015 making robots-with-mruby
Batteries (for motors)
Batteries (for motors)
🙀
Battery (for Pi A+)
RubyKaigi2015 making robots-with-mruby
connect a sensor
?
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
Serial Peripheral Interface (SPI)
• Master and Slave model
• In our case, Raspberry Pi is master
• 4 pins (Input, Output, Clock, Select)
Demo
Gyro Test
serial = 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
connect motors
?
PWM
• Pulse-Width Modulation
• pseudo-analog signal output
http://www.electronics-tutorials.ws/blog/pulse-width-modulation.html
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)
Motor Driver
ST Micro L298N
Motor Driver
PWMIN1 IN2
OUT1
OUT2
MOTOR
Motor Driver
Raspberry Pi
IN1 IN2 OUT
0 0 STOP
0 1 Forward
1 0 Reverse
1 1 Brake
Demo
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
JTAG
• For Debugger
• Doesn’t have to
use it
• but with JTAG, we
can use GDB, so
it’s very helpful to
debug
Pinout
Making
Balancer
class
Balancing Equation
power = k_1 * (Angular Velocity) +
k_2 * (Angle) +
k_3 * (Velocity) +
k_4 * (Position)
P = K!! + K✓✓ + Kvv + Kxx
Angle
✓ ✓
Angular Velocity
! !(fast) (slow)
Velocity
(fast) (slow)vv
Position
(far) (near)y asixsy asixs
Balancing Equation
power = k_1 * omega_i +
k_2 * theta_i +
k_3 * v_i +
k_4 * x_i
P = K!! + K✓✓ + Kvv + Kxx
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✓✓ + Kvv + Kxx
Balancing Equation
P = K!! + K✓✓ + Kvv + Kxx
= K!! + K✓
X
! + Kv
X
P + Kx
X X
P
power = k_1 * omega_i +
k_2 * theta_i +
k_3 * v_i +
k_4 * x_i
a ≒ Power
v ≒ ∫ Power
x ≒ ∬ Power
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✓✓ + Kvv + Kxx
= K!! + K✓
X
! + Kv
X
P + Kx
X X
P
Balancer class
class 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
main loop
loop 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
demo
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
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

More Related Content

RubyKaigi2015 making robots-with-mruby