Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RubyKaigi2015 making robots-with-mruby

12,303 views

Published on

Making Robots with mruby at Rubykaigi2015(en)

Published in: Technology
  • Be the first to comment

RubyKaigi2015 making robots-with-mruby

  1. 1. Making Robots with mruby 2015-12-12 RubyKaigi 2015 Yurie Yamane Team Yamanekko
  2. 2. Who am I? やまね ゆりえ(Yurie Yamane)     @yuri_at_earth(only in Japanese) a member of TOPPERS project ET robocon staff たいやき部(taiyaki-bu)
  3. 3. Yamanekko https://github.com/yamanekko
  4. 4. 2012
  5. 5. 2013
  6. 6. 2014 nomitory
  7. 7. 2015
  8. 8. Robots and Us
  9. 9. 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
  10. 10. PART 1: Self- balancing Robot using LEGO EV3
  11. 11. 闇RubyKaigi(2011) http://gihyo.jp/news/report/01/rubykaigi2011/0001?page=7
  12. 12. http://www.etrobo.jp/2015/
  13. 13. ETロボコンAdvent Calendar 2015 http://www.adventar.org/calendars/734
  14. 14. 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)
  15. 15. EV3way: Software System OS Language TOPPERS/EV3RT TOPPERS RTOS C, C++ MonoBrick Linux C# leJOS EV3 Linux Java
  16. 16. EV3way: Software • mruby on EV3RT + TECS • mrby Forum ver. • yamanekko ver. System OS Language TOPPERS/EV3RT TOPPERS RTOS C + mruby
  17. 17. mruby-ev3rt
  18. 18. mruby-ev3rt demo Sep. 20th, 2015 at Fukuyama, Hiroshima Pref. Movie by A. Hirai with ET Robocon Staff@chu-shikoku
  19. 19. • 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/
  20. 20. 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
  21. 21. 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
  22. 22. TOPPERS EV3RT unprivileged mode (user mode) privileged mode (kernel mode) http://dev.toppers.jp/trac_user/ev3pf/wiki/WhatsEV3RT
  23. 23. Self Balancing Robot
  24. 24. Inverted Pendulum
  25. 25. Inverted Pendulum
  26. 26. Inverted Pendulum
  27. 27. Inverted Pendulum
  28. 28. Inverted Pendulum
  29. 29. Inverted Pendulum
  30. 30. Inverted Pendulum
  31. 31. How Robot Works Target Board Sensors Actuator
  32. 32. Gyro Sensor Ultrasonic Sensor Motor Light Sensor
  33. 33. How Robot Works Target Board Sensors Actuator Read
  34. 34. How Robot Works Target Board Sensors Actuator Move
  35. 35. How Robot Works sense calculate control motor execute periodically
  36. 36. 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”
  37. 37. 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/
  38. 38. 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)
  39. 39. All tasks are mruby main_task balance_task watch_task HIGH priority LOW priority balancer.rb watch.rb app_ruby.rb 注)バランス制御に重点を置いた例。   これが正しい方法というわけではない
  40. 40. 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
  41. 41. watch.rb loop do break if back_button.pressed? EV3RT::Task.sleep end
  42. 42. 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
  43. 43. Classes in mruby-ev3rt Motor TailMotor Sensor GyroSensorColorSensor TouchSensor LED LCD Clock Serial Task Balancer Button
  44. 44. PART 2: DIY Self- balancing Robot
  45. 45. DIY Self-balancing Robot Can we make our own robots from scratch? https://www.flickr.com/photos/84906483@N08/8747337118/
  46. 46. DIY Self-balancing Robot 😿🙀😸 http://www.instructables.com/id/Another-Easier-Inverted-Pendulum/
  47. 47. Collecting Parts Target Board Sensors Actuator
  48. 48. 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
  49. 49. Raspberry Pi A+ 😿
  50. 50. ARM Family http://www.emcu.it/CortexFamily/CortexFamily.html Pi1 Pi2 EV3 STM32F4
  51. 51. Inverted Pendulum
  52. 52. How to measure an angle
  53. 53. Gyro Sensor http://akizukidenshi.com/catalog/g/gK-06779/
  54. 54. Gyro Sensor • ST Micro L3GD20 • 3-axis digital gyroscope • use 8 pin DIP module by Akizuki Denshi
  55. 55. Gyro Sensor 😿
  56. 56. Gyro Sensor 🙀
  57. 57. Measuring the Angle !1 !2
  58. 58. How to rotate wheels
  59. 59. DC Motor • TAMIYA FA-130 Motor • speed control by voltage • IO cannot change voltage • 3.3 V (fixed) • → use PWM
  60. 60. DC Motor 😿
  61. 61. (Motor + Gear) x 2
  62. 62. Wheel x 2
  63. 63. Body Tamiya 70157 Universal Plate Set
  64. 64. Batteries (for motors)
  65. 65. Batteries (for motors) 🙀
  66. 66. Battery (for Pi A+)
  67. 67. connect a sensor ?
  68. 68. 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
  69. 69. Serial Peripheral Interface (SPI) • Master and Slave model • In our case, Raspberry Pi is master • 4 pins (Input, Output, Clock, Select)
  70. 70. Demo
  71. 71. 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
  72. 72. connect motors ?
  73. 73. PWM • Pulse-Width Modulation • pseudo-analog signal output http://www.electronics-tutorials.ws/blog/pulse-width-modulation.html
  74. 74. 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)
  75. 75. Motor Driver ST Micro L298N
  76. 76. 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
  77. 77. Demo
  78. 78. 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
  79. 79. JTAG • For Debugger • Doesn’t have to use it • but with JTAG, we can use GDB, so it’s very helpful to debug
  80. 80. Pinout
  81. 81. Making Balancer class
  82. 82. Balancing Equation power = k_1 * (Angular Velocity) + k_2 * (Angle) + k_3 * (Velocity) + k_4 * (Position) P = K!! + K✓✓ + Kvv + Kxx
  83. 83. Angle ✓ ✓
  84. 84. Angular Velocity ! !(fast) (slow)
  85. 85. Velocity (fast) (slow)vv
  86. 86. Position (far) (near)y asixsy asixs
  87. 87. Balancing Equation power = k_1 * omega_i + k_2 * theta_i + k_3 * v_i + k_4 * x_i P = K!! + K✓✓ + Kvv + Kxx
  88. 88. 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
  89. 89. 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
  90. 90. 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
  91. 91. 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
  92. 92. 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
  93. 93. demo
  94. 94. まずは何か作ってみよう!
  95. 95. Source code •https://github.com/yamanekko/mruby-ev3rt •https://github.com/yamanekko/mruby-rs-robot •https://github.com/yamanekko/raspi_robot
  96. 96. 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

×