lego mindstorms & arduino - home.et.utwente.nl · chaotic challenge …for most of you too much...
TRANSCRIPT
LEGO MINDSTORMS & ARDUINOPRACTICAL SESSION 2
Part of SmartProducts
▪ Lego sensors advanced
▪ Driving a two-wheeled robot
▪ Arduino programming basics
▪ Assignment
PRACTICAL SESSION 2
LEGO MINDSTORMS & ARDUINO
6/8/2018AppDev 2slides @ vanslooten.com/appdev
Fjodor van Slooten
W241 (Horst-wing West)
▪ Chaotic
▪ Challenge …for most of you too much work/difficult
▪ 5 groups managed to get it working
▪ We all learned…
6/8/2018AppDev 3
LAST WEEK
Changes today:
Easier building
6 challenges: easy > hard, try to do as many as you can
One extra assistant
If your kit is missing pieces/materials, you
can get replacements from teacher!
6/8/2018AppDev 4
ACCESS EVSHIELD LIBRARY REFERENCE
scroll down until
“EVShieldBank”, click that
Motor… commands?
6/8/2018AppDev 5
EVSHIELD MOTOR COMMANDSACCESS EVSHIELD LIBRARY REFERENCE
Methods of EVSHieldBank
Class, a lot motor-related
This is also available on your own computer:
Documents\Arduino\libraries\EVShield\html\
6/8/2018AppDev 6
EXAMPLESNXT LIGHT SENSOR
6/8/2018AppDev 7
CALCULATE MOTOR DEGREESTO DRIVE A GIVEN DISTANCE
Let’s drive 1m
#define WHEEL_DIAM 4.96 // wheel diameter in cm
double circumference = WHEEL_DIAM * PI; // value of PI is build-in definition
unsigned int distance = 100; // distance to travel in cm
unsigned long degrees = (distance / circumference) * 360;
evshield.bank_a.motorRunDegrees(SH_Motor_Both, SH_Direction_Reverse, SH_Speed_Medium,degrees, SH_Completion_Wait_For, SH_Next_Action_Brake );
Run the motor for
the calculated
amount of degrees Download example evshield_drive_1m.ino
𝑑𝑒𝑔𝑟𝑒𝑒𝑠 =𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒
𝐶𝑤× 360𝐶𝑤 = 𝐷𝑤 × 𝜋
6/8/2018AppDev 8
MAKE 90-DEGREE TURNSPIN WHEELS IN OPPOSITE DIRECTION AT THE SAME TIME
Serial.println("go left");evshield.bank_a.motorRunDegrees(SH_Motor_2, SH_Direction_Forward, SH_Speed_Medium, 254, SH_Completion_Dont_Wait, SH_Next_Action_Float);
evshield.bank_a.motorRunDegrees(SH_Motor_1, SH_Direction_Reverse, SH_Speed_Medium, 254, SH_Completion_Wait_For, SH_Next_Action_Float);
= point turn
swing turn: rotate one wheel and stop (or slow) the other
Important:
commands
should run at
same time!
How? For first
command use
SH_Completion_Dont_Wait
Serial.println("go right");evshield.bank_a.motorRunDegrees(SH_Motor_1, SH_Direction_Forward, SH_Speed_Medium, 254, SH_Completion_Dont_Wait, SH_Next_Action_Float);
evshield.bank_a.motorRunDegrees(SH_Motor_2, SH_Direction_Reverse, SH_Speed_Medium, 254, SH_Completion_Wait_For, SH_Next_Action_Float);
Degrees (in
this example:
254) depends
on wheel
diameter and
trackwidth of
your robot!
See next slide
6/8/2018AppDev 9
CALCULATE DEGREES POINT TURN
𝑅𝑜𝑡 = 𝑛 ×𝑊𝑡
𝐷𝑤
Where
Rot = rotate motor in degrees
n = degree of turn
𝑊𝑡 = track width
𝐷𝑤 = diameter of wheel
Source for calculation
𝑅𝑜𝑡 = 90 ×14
4.96= 254
Make a 90 degrees turn: wheel
diameter=4.96, trackwidth=14:
Tip: try to make a function turn(), similar to
the function driveDistance() in example
evshield_drive_1m.ino
track width
▪ With 3 motors, the EVShield might sometimes behave
strange/appears buggy
▪ Try:
▪ Disconnect all power
▪ Test on batteries only (no USB cable connected)
▪ (temporary) remove all sensors
▪ Switch ports eg. connect motor on M1 > to M2
6/8/2018AppDev 10
EVSHIELD TIPS&TRICKS
6/8/2018AppDev 11
COLOR SENSORS
HiTechnic Color Sensor
NXT Color Sensor
EV3 Light/Color Sensor
Works, read this
info on how to
use it
color = sensor.readColor();
Method readColor() returns color-index, eg.
1 = black, 2 = red …
6/8/2018AppDev 12
ULTRASONIC SENSOR
▪ Uses ultrasonic sound waves to
determine range of object (echo-
location)
▪ Range 5-250cm… or more
▪ Send a ‘ping’… wait for return,
measure time to get distance
NXT-version
cannot be used
with EVShield
EV3 versionBasic Arduino module
#include <NewPing.h>
// define to which pins the sensor is connected:#define TRIGGER_PIN 3#define ECHO_PIN 5#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters).
//Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
unsigned int distance = sonar.ping_cm(); // read distance from ultrasonic sensorif ( distance > 0 && distance < 30 ) { // is it larger than 0 and smaller than 30 (cm)?
Serial.println("object detected");}
6/8/2018AppDev 13
ULTRASONIC SENSORCODE EXAMPLE
Basic Arduino module
A valid distance
must be > 0
We use the
‘NewPing’ library
EV3 version
See EVShield examples
▪ Motor has build-in rotation sensor
6/8/2018AppDev 14
ROTATION SENSOR
degrees = evshield.bank_a.motorGetEncoderPosition(SH_Motor_1);
Download example evshield_motorGetEncoderPosition.ino
6/8/2018AppDev 15
GYRO SENSOR
See EVShield examples
Gyro Boy, can be build with EV3 kit,which can be borrowed
youtube.com/watch
▪ Combination of detection of angle and acceleration
▪ Can be used to create balancing robots
6/8/2018AppDev 16
ARDUINO PROGRAMMING BASICS
// constants won't change. They're used here to set pin numbers:const int buttonPin = 2; // number of pushbutton pinconst int ledPin = 13; // number of onboard LED pin
// variables will change:int buttonState = 0; // variable for reading pushbutton status
void setup() {// initialize the LED pin as an output:pinMode(ledPin, OUTPUT);// initialize the pushbutton pin as an input:pinMode(buttonPin, INPUT);
}
arduino.cc/en/Tutorial/Button
File > Examples > 02.Digital > Button
6/8/2018AppDev 17
ARDUINO PROGRAMMING BASICS
void loop() {// read the state of the pushbutton value:buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.// If it is, the buttonState is HIGH:if (buttonState == HIGH) {// turn LED on:digitalWrite(ledPin, HIGH);
} else {// turn LED off:digitalWrite(ledPin, LOW);
}}
arduino.cc/en/Tutorial/Button
arduino.cc/en/Tutorial/Debounce
Does not work as expected...?
Check out next example: 'debounce'
▪ Libraries extend functionality
▪ Documents\Arduino\libraries contains folders
with libraries
6/8/2018AppDev 18
USING LIBRARIESMake
programming
easier
Browse through available
libraries (and install it)
Include a library by
selecting one
Add a new library by
selecting its .zip file (you
downloaded)
arduino.cc/en/Main/Libraries
▪ Libraries are nice, as they allow easy programming
▪ But libraries usually use a lot of memory
▪ Combining multiple (large) libraries might result in to much memory
usage
6/8/2018AppDev 19
ARDUINO MEMORY
learn.adafruit.com/memories-of-an-arduino/optimizing-sram
Example: combination of EVShield library and Blynk 'does not fit'
into 1 Arduino
Solution: add an Arduino Nano which acts as a (remote) controller
See few slides further
▪ loop(): check multiple ‘events’
▪ timing of loop()
▪ Look at example given for
spike (spike1.ino)
6/8/2018AppDev 20
BUMPERFile > Examples > EVShield > EVShield_tests > nxt_touch
if (drive && forward) { // if we are driving forward...if (myTouch.isPressed()) { // if we bump into something
// stop and back-upSerial.println("bump");reverseTurn();
}}
▪ Example in pseudo code:
6/8/2018AppDev 21
BUMPED… BACKUP AND TURN
void reverseTurn() {// stopevshield.bank_a.motorStop(SH_Motor_Both, SH_Next_Action_Brake);
// drive backward a littleevshield.bank_a.motorRunUnlimited( SH_Motor_Both, SH_Direction_Forward, SH_Speed_Medium );
// make a 90 degree turnturn(90);
// continue drivingevshield.bank_a.motorRunUnlimited( SH_Motor_Both, SH_Direction_Reverse, SH_Speed_Medium );
}
void reverseTurn() {}
Create a function that
will make the robot go
backwards and turn
▪ Example in pseudo code:
6/8/2018AppDev 22
SEE AN OBSTACLE… FIND A WAY OUT?
void findWay() {// stopevshield.bank_a.motorStop(SH_Motor_Both, SH_Next_Action_Brake);
// look left:// Use evshield.bank_b.motorRunDegrees(…) to turn the motor with the ultrasonic sensor 90 degrees// take a reading of the ultrasonic sensorunsigned int left = sonar.ping_cm(); // read distance from ultrasonic sensor
// look to the right (turn 180 degrees in other direction)// take another readingunsigned int right = sonar.ping_cm(); // read distance from ultrasonic sensor
// rotate the motor back in original position// decide where to go:if (left>right) { // go left
// turn 90 degrees to left}else { // go right
// turn 90 degrees to right}// continue driving
}
void findWay() {}
Create a function that
will find a way out
▪ Tutorial: vanslooten.com/2018/03/17/connect-
esp8266-control-blynk
▪ Wifi: EDUROAM does not work, use a (mobile)
hotspot
6/8/2018AppDev 23
CONNECT TO WIFI
ESP8266 module
Wifi: connect to
“mspot”, password
available
next lectures
▪ Control your robot with an App
▪ Robot needs Wifi connection
6/8/2018AppDev 24
BLYNK
blynk.cc
ESP8266 module
next lectures
▪ Connect ESP-module directly to Arduino… sadly,
does not work: too much memory usage by libraries
▪ Alternative: add Arduino Nano with ESP-module &
Blynk library > use that as remote controller
6/8/2018AppDev 25
WIFI?ESP8266 module
▪ Follow tutorial until step “Control the LED with Blynk”
▪ Connect power:▪ 5V of Uno > Vin of Nano
▪ 3.3V of Uno > Powerline on breadboard (for ESP8266)
▪ Connect GND
▪ Go (Drive) button:
▪ Add a button to your Blynk App, set to PIN D4
▪ Connect D4 of Nano to D4 of Uno (EVShield)
▪ On Uno if D4 pin is high…:
6/8/2018AppDev 26
REMOTE CONTROL WITH APP
int go = digitalRead(REM_CONTROL_PIN); // read the input pinif ( evshield.getButtonState(BTN_GO) || go==HIGH ) { // start/stop}
▪ Create a vehicle with two driven
wheels (two motors that drive
two wheels) that is capable of
avoiding obstacles
▪ It must be able to drive
measured distances (eg. drive
exactly 2m) and make precise
turns (eg. make a 90 degree
turn around its own axis)
6/8/2018AppDev 27
ASSIGNMENTFOR TODAY’S PRACTICAL SESSION 1/4
Warning: you might want to keep this Lego
model, as we will use it again for
Assignment 4 of Application Development
slides @ vanslooten.com/appdev
This assignment consists of 4 slides:
What vehicle?
You are free to
choose. 2 options
are shown on
next slides
Form groups of 4 again
▪ To speed building, you may want to keep the chassis
of the spike
▪ Add 2 wheels
▪ Make rear wheel of explorer (step 7-9 of this guide)
▪ Add bumper
▪ Add motor on top with ultrasonic sensor
6/8/2018AppDev 28
ASSIGNMENT: LEGO BUILDINGOPTION 1: RE-USE SPIKE CHASSIS 2/4
This assignment consists of 4 slides:
Tutorial for this: “Use Spike chassis to build Explorer robot”
If your kit is missing pieces/materials, you
can get replacements from teacher!
▪ Build Explorer robot from scratch,
eg. from this guide:
nxtprograms.com/explorer
6/8/2018AppDev 29
ASSIGNMENT: LEGO BUILDINGOPTION 2: BUILD EXPLORER 3/4
This assignment consists of 4 slides:
building guide: nxtprograms.com/explorer
If your kit is missing pieces/materials, you
can get replacements from teacher!
1. Make it drive exactly one meter (from given distance, calculate
rotations/degrees to run motor). How accurate is it?
2. Have it make exact 90-degree turns
3. Drive a simple path: e.g. travel 2m forward, make a 90 degree turn,
travel 1m forward
4. Add a bumper: make it reverse & turn when it bumps into something
5. Add an ultrasonic sensor: when it detects an obstacle, find an alternate
direction (for this you will have to mount the sensor to a motor)
6. Add Wifi and control it with your phone
6/8/2018AppDev 30
ASSIGNMENT: CHALLENGESGOAL: REALIZE AS MANY AS YOU CAN, STOP AT 17H
drive 1m
This assignment is a mandatory part of Application Development.
Show at least 3 challenges to receive a ‘pass’.
Only students actually participating will receive a 'pass'.
4/4This assignment consists of 4 slides:
Wifi: connect to
“mspot”, password
available
If your kit is missing pieces/materials, you can get
replacements from teacher!
Form groups of 4 again
Use example on slide
7, you do not have to
program anything!