2019-09-07 01:10:33 +02:00
# Robot 2 Lesson
2019-09-10 01:59:12 +02:00
## Step 1 - Build Your Driving Base Robot @unplugged
2019-09-07 01:10:33 +02:00
Build the robot driving base:
[![EV3 Driving Base ](/static/lessons/common/ev3-driving-base.jpg )](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-rem-driving-base-79bebfc16bd491186ea9c9069842155e.pdf)
2019-09-10 01:59:12 +02:00
If clicking on the image above doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
## Step 2 - Show an image and move @fullscreen
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
Add blocks to the ``||loops:on start||`` block to show an image and move the motors **B+C** for ``1`` rotation.
2019-09-07 01:10:33 +02:00
```blocks
brick.showMood(moods.neutral)
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
```
2019-09-10 01:59:12 +02:00
## Step 3 - Brick button @fullscreen
2019-09-07 01:10:33 +02:00
Let's change the code so that your robot moves when the **UP** button is pressed.
2019-09-10 01:59:12 +02:00
Add an ``||brick:on button up||`` block and move ``||motors:steer motors||`` inside of it.
2019-09-07 01:10:33 +02:00
After downloading your code, press **UP** to move the robot.
```blocks
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.awake)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
})
brick.showMood(moods.neutral)
```
2019-09-10 01:59:12 +02:00
## Step 4 - Braking @fullscreen
2019-09-07 01:10:33 +02:00
When the motors are done turning, the robot keeps on moving for a short distance.
2019-09-10 01:59:12 +02:00
Turn on the **brakes** so that your robot stops immediately.
Drag a ``||motors:set brake||`` block into the ``||loops:on start||`` and set it to **ON** for the the **BC** motors.
2019-09-07 01:10:33 +02:00
```blocks
brick.buttonUp.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.awake)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(0, 50, 1, MoveUnit.Rotations)
})
brick.showMood(moods.neutral)
motors.largeBC.setBrake(true)
```
2019-09-10 01:59:12 +02:00
## Step 5 - Left and Right turn @fullscreen
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
Let's make the robot turn to the left when the **LEFT** button is pressed on the brick.
Find an ``||brick:on button||`` block and put a ``||motors:steer motors||`` in it. Make the turn ratio drive the motor to left.
Get another ``||brick:on button||`` and set it to run when the **RIGHT** is pressed.
Put a ``||motors:steer motors||`` in for that button and set the turn ratio to drive to the right.
2019-09-07 01:10:33 +02:00
```blocks
brick.buttonLeft.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.middleLeft)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(-50, 50, 1, MoveUnit.Rotations)
})
brick.buttonRight.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.middleRight)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(50, 50, 1, MoveUnit.Rotations)
})
```
2019-09-10 01:59:12 +02:00
## Step 6 - Backwards @fullscreen
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
Let's make the robot go backwards when the **DOWN** button is pressed on the brick.
Add a ``||motors:steer motors||`` to an ``||brick:on button||`` block and change the speed to be negative. This will make the motor go backwards.
2019-09-07 01:10:33 +02:00
```blocks
brick.buttonDown.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.knockedOut)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(0, -50, 1, MoveUnit.Rotations)
})
```
2019-09-10 01:59:12 +02:00
## Step 7 - Add an Ultrasonic sensor @fullscreen
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
Add an Ultrasonic sensor to your driving base.
2019-09-07 01:10:33 +02:00
[![EV3 Driving Base with Ultrasonic Sensor ](/static/lessons/common/ev3-ultrasonic-sensor-driving-base.jpg )](https://le-www-live-s.legocdn.com/sc/media/lessons/mindstorms-ev3/building-instructions/ev3-ultrasonic-sensor-driving-base-61ffdfa461aee2470b8ddbeab16e2070.pdf)
2019-09-10 01:59:12 +02:00
If clicking on the image above doesn't open the instructions, right-click on the image and choose "Save link as..." to download the PDF.
2019-09-07 01:10:33 +02:00
2019-09-10 01:59:12 +02:00
## Step 8 - Stopping distance @fullscreen
2019-09-07 01:10:33 +02:00
Create a program that moves the Driving Base and makes it stop 6 cm from the Cuboid.
```blocks
brick.buttonEnter.onEvent(ButtonEvent.Pressed, function () {
2019-09-26 06:12:45 +02:00
brick.showMood(moods.dizzy)
2019-09-07 01:10:33 +02:00
motors.largeBC.steer(0, 50)
pauseUntil(() => sensors.ultrasonic4.distance() < 6 )
motors.largeBC.stop()
})
```
Try sending your robot towards a wall!