using lego mindstorms nxt in the classroom gabriel j. ferrer hendrix college [email protected]...

63
Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College [email protected] http:// ozark.hendrix.edu/ ~ferrer/

Upload: betty-bond

Post on 18-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Using Lego Mindstorms NXT in the Classroom

Gabriel J. Ferrer

Hendrix College

[email protected]

http://ozark.hendrix.edu/~ferrer/

Page 2: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Outline

• NXT capabilities

• Software development options

• Introductory programming projects

• Advanced programming projects

Page 3: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Purchasing NXT Kits

• Two options (same price; $250/kit)– Standard commercial kit– Lego Education kit

• http://www.lego.com/eng/education/mindstorms/

• Advantages of education kit– Includes rechargeable battery ($50 value)– Plastic box superior to cardboard– Extra touch sensor (2 total)

• Standard commercial kit– Includes NXT-G visual language

Page 4: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

NXT Brick Features

• 64K RAM, 256K Flash

• 32-bit ARM7 microcontroller

• 100 x 64 pixel LCD graphical display

• Sound channel with 8-bit resolution

• Bluetooth radio

• Stores multiple programs– Programs selectable using buttons

Page 5: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Sensors and Motors

• Four sensor ports– Sonar– Sound– Light– Touch

• Three motor ports– Each motor includes rotation counter

Page 6: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Touch Sensors

• Education kit includes two sensors

• Much more robust than old RCX touch sensors

Page 7: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Light Sensor

• Reports light intensity as percentage

• Two modes– Active– Passive

• Practical uses– Identify intensity on paper– Identify lit objects in dark room– Detect shadows

Page 8: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Sound Sensor

• Analogous to light sensor– Reports intensity– Reputed to identify tones

• I haven’t experimented with this

• Practical uses– “Clap” to signal robot

Page 9: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Ultrasonic (Sonar) Sensor

• Reports distances– Range: about 5 cm to 250 cm– In practice:

• Longer distances result in more missed “pings”

• Mostly reliable– Occasionally gets “stuck”– Moving to a new location helps in receiving a

sonar “ping”

Page 10: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Motors

• Configured in terms of percentage of available power

• Built-in rotation sensors– 360 counts/rotation

Page 11: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Software development options

• Onboard programs– RobotC– leJOS– NXC/NBC

• Remote control– iCommand– NXT_Python

Page 12: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

RobotC

• Commercially supported– http://www.robotc.net/

• Not entirely free of bugs• Poor static type checking• Nice IDE• Custom firmware• Costly

– $50 single license– $250/12 classroom computers

Page 13: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Example RobotC Program

void forward() { motor[motorA] = 100; motor[motorB] = 100;}

void spin() { motor[motorA] = 100; motor[motorB] = -100;}

Page 14: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Example RobotC Program

task main() {

SensorType[S4] = sensorSONAR;

forward();

while(true) {

if (SensorValue[S4] < 25) spin();

else forward();

}

}

Page 15: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

leJOS

• Implementation of JVM for NXT• Reasonably functional

– Threads– Some data structures– Garbage collection added (January 2008)– Eclipse plug-in just released (March 2008)

• Custom firmware• Freely available

– http://lejos.sourceforge.net/

Page 16: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

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(); } }

Page 17: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven Control in leJOS

• The Behavior interface– boolean takeControl()– void action()– void suppress()

• Arbitrator class– Constructor gets an array of Behavior objects

• takeControl() checked for highest index first

– start() method begins event loop

Page 18: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven example

class Go implements Behavior { private Ultrasonic sonar = new Ultrasonic(SensorPort.S4);

public boolean takeControl() { return sonar.getDistance() > 25; }

Page 19: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven example

public void action() { Motor.A.forward(); Motor.B.forward(); }

public void suppress() { Motor.A.stop(); Motor.B.stop(); }}

Page 20: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven example

class Spin implements Behavior {

private Ultrasonic sonar =

new Ultrasonic(SensorPort.S4);

public boolean takeControl() {

return sonar.getDistance() <= 25;

}

Page 21: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven example

public void action() { Motor.A.forward(); Motor.B.backward(); }

public void suppress() { Motor.A.stop(); Motor.B.stop(); }}

Page 22: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Event-driven example

public class FindFreespace {

public static void main(String[] a) {

Behavior[] b = new Behavior[]

{new Go(), new Stop()};

Arbitrator arb =

new Arbitrator(b);

arb.start();

}

}

Page 23: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

NXC/NBC

• NBC (NXT Byte Codes)– Assembly-like language with libraries– http://bricxcc.sourceforge.net/nbc/

• NXC (Not eXactly C)– Built upon NBC– Successor to NQC project for RCX

• Compatible with standard firmware– http://mindstorms.lego.com/Support/Updates/

Page 24: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

iCommand

• Java program runs on host computer• Controls NXT via Bluetooth• Same API as leJOS

– Originally developed as an interim project while leJOS NXT was under development

– http://lejos.sourceforge.net/

• Big problems with latency– Each Bluetooth transmission: 30 ms– Sonar alone requires three transmissions– Decent program: 1-2 Hz

Page 25: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

NXT_Python

• Remote control via Python– http://home.comcast.net/~dplau/nxt_python/

• Similar pros/cons with iCommand

Page 26: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Developing a Remote Control API

• Bluetooth library for Java– http://code.google.com/p/bluecove/

• Opening a Bluetooth connection– Typical address: 00:16:53:02:e5:75

• Bluetooth URL– btspp://00165302e575:1; authenticate=false;encrypt=false

Page 27: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Opening the Connection

import javax.microedition.io.*;import java.io.*;

StreamConnection con = (StreamConnection) Connector.open(“btspp:…”);

InputStream is = con.openInputStream();

OutputStream os = con.openOutputStream();

Page 28: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

NXT Protocol

• Key files to read from iCommand:– NXTCommand.java– NXTProtocol.java

Page 29: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

An Interesting Possibility

• Programmable cell phones with cameras are available

• Camera-equipped cell phone could provide computer vision for the NXT

Page 30: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Introductory programming projects

• Developed for a zero-prerequisite course

• Most students are not CS majors

• 4 hours per week– 2 meeting times– 2 hours each

• Not much work outside of class– Lab reports– Essays

Page 31: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

First Project (1)

• Introduce motors– Drive with both motors forward for a fixed time– Drive with one motor to turn– Drive with opposing motors to spin

• Introduce subroutines– Low-level motor commands get tiresome

• Simple tasks– Program a path (using time delays) to drive

through the doorway

Page 32: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

First Project (2)

• Introduce the touch sensor– if statements

• Must touch the sensor at exactly the right time– while loops

• Sensor is constantly monitored

• Interesting problem– Students try to put code in the loop body

• e.g. set the motor power on each iteration

– Causes confusion rather than harm

Page 33: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

First Project (3)

• Combine infinite loops with conditionals

• Enables programming of alternating behaviors– Front touch sensor hit => go backward– Back touch sensor hit => go forward

Page 34: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Second Project (1)

• Physics of rotational motion

• Introduction of the rotation sensors– Built into the motors

• Balance wheel power– If left counts < right counts

• Increase left wheel power

• Race through obstacle course

Page 35: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Second Project (2)

if (/* Write a condition to put here */) { nxtDisplayTextLine(2, "Drifting left");

} else if (/* Write a condition to put here */) { nxtDisplayTextLine(2, "Drifting right");

} else {

nxtDisplayTextLine(2, "Not drifting");

}

Page 36: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Third Project

• Pen-drawer– First project with an effector– Builds upon lessons from previous projects

• Limitations of rotation sensors– Slippage problematic– Most helpful with a limit switch

• Shapes (Square, Circle)• Word (“LEGO”)

– Arguably excessive

Page 37: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Pen-Drawer Robot

Page 38: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Pen-Drawer Robot

Page 39: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fourth Project (1)

• Finding objects

• Light sensor– Find a line

• Sonar sensor– Find an object– Find freespace

Page 40: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fourth Project (2)

• Begin with following a line edge– Robot follows a circular track– Always turns right when track lost– Traversal is one-way

• Alternative strategy– Robot scans both directions when track lost– Each pair of scans increases in size

Page 41: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fourth Project (3)

• Once scanning works, replace light sensor reading with sonar reading

• Scan when distance is short– Finds freespace

• Scan when distance is long– Follow a moving object

Page 42: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Light Sensor/Sonar Robot

Page 43: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Other Projects

• “Theseus”– Store path (from line following) in an array– Backtrack when array fills

• Robotic forklift– Finds, retrieves, delivers an object

• Perimeter security robot– Implemented using RCX– 2 light sensors, 2 touch sensors

• Wall-following robot– Build a rotating mount for the sonar

Page 44: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Robot Forklift

Page 45: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Gearing the motors

Page 46: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Advanced programming projects

• From a 300-level AI course

• Fuzzy logic

• Reinforcement learning

Page 47: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fuzzy Logic

• Implement a fuzzy expert system for the robot to perform a task

• Students given code for using fuzzy logic to balance wheel encoder counts

• Students write fuzzy experts that:– Avoid an obstacle while wandering– Maintain a fixed distance from an object

Page 48: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fuzzy Rules for Balancing Rotation Counts

• Inference rules:– biasRight => leftSlow– biasLeft => rightSlow– biasNone => leftFast– biasNone => rightFast

• Inference is trivial for this case– Fuzzy membership/defuzzification is more

interesting

Page 49: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Fuzzy Membership Functions

• Disparity = leftCount - rightCount• biasLeft is

– 1.0 up to -100– Decreases linearly down to 0.0 at 0

• biasRight is the reverse• biasNone is

– 0.0 up to -50– 1.0 at 0– falls to 0.0 at 50

Page 50: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Defuzzification

• Use representative values:– Slow = 0– Fast = 100

• Left wheel:– (leftSlow * repSlow + leftFast * repFast) /

(leftSlow + leftFast)

• Right wheel is symmetric

• Defuzzified values are motor power levels

Page 51: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Q-Learning

• Discrete sets of states and actions– States form an N-dimensional array

• Unfolded into one dimension in practice

– Individual actions selected on each time step

• Q-values– 2D array (indexed by state and action)– Expected rewards for performing actions

Page 52: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Q-Learning Main Loop

• Select action• Change motor speeds• Inspect sensor values

– Calculate updated state– Calculate reward

• Update Q values• Set “old state” to be the updated state

Page 53: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Calculating the State (Motors)

• For each motor:– 100% power– 93.75% power– 87.5% power

• Six motor states

Page 54: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Calculating the State (Sensors)

• No disparity: STRAIGHT

• Left/Right disparity– 1-5: LEFT_1, RIGHT_1– 6-12: LEFT_2, RIGHT_2– 13+: LEFT_3, RIGHT_3

• Seven total sensor states

• 63 states overall

Page 55: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Action Set for Balancing Rotation Counts

• MAINTAIN– Both motors unchanged

• UP_LEFT, UP_RIGHT– Accelerate motor by one motor state

• DOWN_LEFT, DOWN_RIGHT– Decelerate motor by one motor state

• Five total actions

Page 56: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Action Selection

• Determine whether action is random– Determined with probability epsilon

• If random:– Select uniformly from action set

• If not:– Visit each array entry for the current state– Select action with maximum Q-value from

current state

Page 57: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Q-Learning Main Loop

• Select action• Change motor speeds• Inspect sensor values

– Calculate updated state– Calculate reward

• Update Q values• Set “old state” to be the updated state

Page 58: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Calculating Reward

• No disparity => highest value

• Reward decreases with increasing disparity

Page 59: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Updating Q-values

Q[oldState][action] =

Q[oldState][action] +

learningRate *

(reward + discount * maxQ(currentState) - Q[oldState][action])

Page 60: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Student Exercises

• Assess performance of wheel-balancer

• Experiment with different constants– Learning rate– Discount– Epsilon

• Alternative reward function– Based on change in disparity

Page 61: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Learning to Avoid Obstacles

• Robot equipped with sonar and touch sensor

• Hitting the touch sensor is penalized

• Most successful formulation:– Reward increases with speed– Big penalty for touch sensor

Page 62: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Other classroom possibilities

• Operating systems– Inspect, document, and modify firmware

• Programming languages– Develop interpreters/compilers– NBC an excellent target language

• Supplementary labs for CS1/CS2

Page 63: Using Lego Mindstorms NXT in the Classroom Gabriel J. Ferrer Hendrix College ferrer@hendrix.edu ferrer

Thanks for attending!

• Slides available on-line:– http://ozark.hendrix.edu/~ferrer/presentations/

• Currently writing lab textbook– Introductory and advanced exercises

[email protected]