Lego Mindstorms and NXT-G

advertisement
Lego Mindstorms and NXT-G
Magnus Eriksson, Mid Sweden University 2012
Based on a presentations by National Instruments and Pavel Petrovič
Först några påminnelser…
• Bråttom att registrera sig! (Portal.miun.se)
• Kursmaterial finns i Lärplattformen 2
(Moodle), som du hittar via portal.miun.se
• Projektgrupperna kommer att ändras lite högst 7 studenter i varje
• Kursens deadlines framgår av schemat
• Börja fundera på en intelligent produkt med
realistisk marknad redan nu!
Lego Mindstorms can be used for rapid
prototyping of intelligent and sensorbased systems
Rubik’s cube
solver
3
Pen-Drawer Robot
The NXT2.0 unit
(Motor ports)
(Sensor ports)
Motors and Sensors
NXT Motors
• Built-in rotation sensors (“servo”)
7
The NXT2.0 Unit – an embedded
system
• 64K RAM, 256K Flash
• 32-bit ARM7 microcontroller
• 100 x 64 pixel LCD graphical
display
• Sound channel with 8-bit
resolution
• Bluetooth wireless
communications
• Stores multiple programs
– Programs selectable using buttons
NXT-G graphical programming
language
Based on the LabVIEW programming language G
Program by drawing a flow chart
LEGO Mindstorms for NXT
(NXT-G)
NXT-G PC program interface
Tutorials
Toolbar
Workspace
Sequence Beam
Palettes
Controller
Configuration
Panel
Help & Navigation
Web
Portal
NXT-G tools
• Pointer tool: move blocks, select blocks
• Pan tool: move within workspace
• Comment tool: add notes to your code
Intro to NXT-G: Palettes
Common:
Complete:
Most
commonly
used blocks
All blocks
Custom:
Downloa Own blocks
Writing a Basic Program
• Drag and drop block(s) on sequence beam
• Manipulate Configuration Panel & Download
Intro to NXT-G: Controller
Download
& Run
Selection
NXT Window
Stop
Download
Download & Run
NXT Rotation Sensor
• Built in to motors
• Measure degrees
or rotations
• Reads + and • Degrees: accuracy
+/- 1
• 1 rotation =
360 degrees
Viewing Sensors
•
•
•
•
•
Connect sensor
Turn on NXT
Choose “View”
Select sensor type
Select port
NXT Sound Sensor
• Sound sensor can measure in dB and dBA
– dB: in detecting standard [unadjusted]
decibels, all sounds are measured with
equal sensitivity. Thus, these sounds may
include some that are too high or too low
for the human ear to hear.
– dBA: in detecting adjusted decibels, the
sensitivity of the sensor is adapted to the
sensitivity of the human ear. In other words,
these are the sounds that your ears are able
to hear.
• Sound Sensor readings on the NXT are
displayed in percent [%]. The lower the percent
the quieter the sound.
http://mindstorms.lego.com/Overview/Sound_Sensor.aspx
Challenge: Stop with sound
• Program your NXT car to drive until you yell
“Stop”
Example Program: Stop with Sound
NXT Ultrasonic/Distance Sensor
• Measures
distance/proximity
• Range: 0-255 cm
• Precision: +/- 3cm
• Can report in
centimeters or
inches
http://mindstorms.lego.com/Overview/Ultrasonic_Sensor.aspx
Challenge: Parking NXT
• Program your NXT car to drive forward until
you “find a parking spot” (hit a touch sensor)
• The car should then turn into the spot, and
stop when it gets close enough to wall.
Example Program: Parking NXT
A note about NXT memory
• Memory space is limited!
• To clear space, use NXT window
• Show and delete system files for more space
NXT Structure: Loops
Challenge: Stay on Table
• Program your NXT to stay on the table
Example Program: Stay on Table
NXT Structure: Switch
Challenge: Remote Control
• Program car to drive while the touch sensor is
pushed and stop while the touch sensor is
released
Example Program: Remote Control
NXT Sound
• NXT can play sound files or notes
• Sounds take up more memory space
•
•
•
•
NXT Display
Display images, text, or drawings
Choose location on display screen
100 x 64 pixels (up to 7 lines of text)
Graphics take up memory space
Challenge: Say Hello
• Have the NXT say hello using sound and the
display screen
• Don’t forget – the program ends after the last
block. You may need a wait for to see the
display.
• Caution: one of the options in the
configuration panel is Clear – use it to clear
screen. Otherwise, uncheck the box!
Example Program: Say Hello
My Blocks
• Create custom blocks
• Saved in custom palette
• Download blocks from the web
Creating My Blocks
• Write the code for the My Block
Creating My Blocks
• Select all the blocks to be used for My Block
Creating My Blocks
• Choose “Create My Block” on toolbar
• Or, go to the Edit menu and select “Make a
new My Block”
Creating My Blocks
• Name My
Block
• Add a
description
Creating My Blocks
• Create a
custom icon
Creating My Blocks
• My Block appears in Custom Palette
Signal flows and variables
An advanced program
42
Multi-treading
43
LEGO Mindstorms 3.0 EV3
Getting started with mindstorms
EV3
https://www.youtube.com/watch?v=vv5X3zzkBk4
NXT BlueTooth Protocol
•
•
•
•
•
Master-slave: only master initiates communication
Master can connect up to 3 slaves
Optional acknowledgement, request reply
Mailboxes #1-10
Sending:
– Master: NXTCommBTWrite(slave, mailbox)
– Slave: NXTMessageWrite(mailbox)
• Receiving: both sides NXTMessageRead()
• Unreliable & Quite complex, but does not block
Silvian Toledo: http://www.tau.ac.il/~stoledo/lego/btperformance.html
46
LEGO Digital Designer ldd.lego.com
CAD-verktyg – Eventuellt för Civing Teknisk design
47
Pneumatics
För Civing Teknisk design
48
NXT Non-standard sensors:
HiTechnic.com
För Elektroniksystemstudenter
•
Compass
•
Gyroscope
•
Accellerometer/tilt sensor,
•
Color sensor
•
IRSeeker
•
Prototype board with A/D converter
49
for the I2C bus
Issues of the standard firmware







Only one data type
Unreliable bluetooth communication
Limited multi-tasking
Complex motor control
Simplistic memory management
Not suitable for large programs
Not suitable for development of own tools or blocks
För datateknikstudenter
50
Other programming languages and
environments
– Java leJOS
– Microsoft Robotics Studio
– RobotC
– NXC - Not eXactly C
– NXT Logo
– Lego NXT Open source firmware and software
development kit
För datateknikstudenter
leJOS
• A Java Virtual Machine for NXT
• Freely available
– http://lejos.sourceforge.net/
• Replaces the NXT-G firmware
• LeJOS plug-in is available for the Eclipse free
development environment
• Faster than NXT-G
För datateknikstudenter
Example leJOS Program
sonar = new UltrasonicSensor(SensorPort.S4);
Motor.A.forward();
Motor.B.forward();
while (true) {
if (sonar.getDistance() < 25) {
Motor.A.forward();
Motor.B.backward();
} else {
Motor.A.forward();
Motor.B.forward();
}
}
För datateknikstudenter
NXT Logo example: line-following
to "follow []
[
setsensor 3 5 128
motor 0 "onrev 40
while ["true]
[
while [ge? sensor 3 59] []
motor 0 "float 0
motor 2 "onrev 40
wait 50
while [lt? sensor 3 60] []
motor 2 "float 0
motor 0 "onrev 40
]
]
För datateknikstudenter
54
Gruppindelning
Download