programming lego mindstorms nxt using ada · lab 2: programming lego mindstorms using ada lab...

32
Introduction to Lab 2 Programming LEGO Mindstorms NXT using Ada Jakaria Abdullah 12 September 2016 Jakaria Abdullah Lab 2: LEGO 12 September 2016 1 / 25

Upload: others

Post on 02-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Introduction to Lab 2Programming LEGO Mindstorms NXT using Ada

Jakaria Abdullah

12 September 2016

Jakaria Abdullah Lab 2: LEGO 12 September 2016 1 / 25

Page 2: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab 2: Programming LEGO Mindstorms using Ada

Lab goals:I Real-time programming on an embedded deviceI Using the Ada feature for concurrent tasks to solve a problem

Lab preparation:I Work in your groupsI Get LEGO box (next slide), charge batteryI Possibly refresh your Ada knowledgeI Lab will be done on Thu, 15.9. and Mon, 19.9. (both in 2315)I Have a look at the lab homepage

http://www.it.uu.se/edu/course/homepage/realtid/ht16/lab2

Lab report:I Ada code to all 3 parts, well commentedI Descriptions of what you did and whyI To submission page in studentportalen; Deadline: Mon, 26.9. at 12:59

Further:I Demonstrate a working vehicle, participate in demonstration on 26.9.

in 2315I Return all hardware you get to Karl (see next slide)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 2 / 25

Page 3: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab 2: LEGO Mindstorms Boxes

Each group gets one box

All hardware issues are handled by Karl Marklund

Office: 1440, mail: [email protected]

Jakaria Abdullah Lab 2: LEGO 12 September 2016 3 / 25

Page 4: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab 2: Working At Home

You may work at home (using Windows/Linux/Mac?)

Toolchain installation is non-trivialI I can’t give support for thatI Firmware upload, program compile, program uploadI Windows: Need Cygwin

An instruction file is available at lab homepage

Default: Work in the Windows lab (2315)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 4 / 25

Page 5: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

LEGO Mindstorms

Programmable LEGO brick with sensors and motors

Comes in several generations:

RCX generation (1998) NXT generation (2006)

We will use the NXT platform

Jakaria Abdullah Lab 2: LEGO 12 September 2016 5 / 25

Page 6: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

LEGO Mindstorms: Components

Package contents:

NXT unit:I LCD matrix displayI Sensor inputs 1 to 4I Motor outputs A, B, CI SpeakerI USB, Bluetooth

Three motors

Sensors:I LightI Distance (Ultrasound)I Touch (2x)I SoundI (More from 3rd party vendors)

NXT Brick Internals:

32-bit ARM7 main processor, 8-bit AVR co-processor, 64k RAM, 256k Flash,48MHz clock

Jakaria Abdullah Lab 2: LEGO 12 September 2016 6 / 25

Page 7: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Programming Environment: Ada NXT

We don’t use the standard firmware

Instead: Ada runtime system for NXTI Not a real-time operating systemI Based on Ravenscar Small footprint profile (restricted subset of Ada

suitable for static analysis)I Can run without OS, only 4186 lines!I Support for (concurrent) tasks, priorities, protected object and fixed

priority preemptive schedulingI Drivers for low-level I/O accessesI Program, drivers and runtime system can run together inside the RAM!

Jakaria Abdullah Lab 2: LEGO 12 September 2016 7 / 25

Page 8: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Ada NXT Runtime: Quick Look

GNARL: Ada runtime library which imposes Ravenscar restrictions

GNULL: Low-level library that needs to be ported for differentarchitecture

Drivers for communicating with the sensor and actuators

Jakaria Abdullah Lab 2: LEGO 12 September 2016 8 / 25

Page 9: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Ravenscar Profile

Background:I Conceived at IRTW 1997 at Ravenscar, ScotlandI Main idea is to restrict Ada features for predictability

Restrictions:I No locally declared task objects or task types and only locally declared

protected objects or protected typesI No dynamic allocation of task objects or protected objectsI At most one entry for each protected object/typeI Each entry barrier expression must be a single Boolean variableI At most one task at a time may be queued on an entryI No task termination and no use of dynamic prioritiesI No requeue and abort statements or Asynchronous Transfer of ControlI No select statements and no task entries (thus no accept statements)I No relative delay (delay) statementsI No references to package Ada.CalendarI No user defined task attributesI No use of dynamic prioritiesI No ’Image and ’Value attribute

Jakaria Abdullah Lab 2: LEGO 12 September 2016 9 / 25

Page 10: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Ravenscar Profile

Allowed Ada Features:I Task type and task object declarations in library level packagesI Protected type and protected object declarations in library level

packagesI Absolute delay (delay until) statementsI References to package Ada.RealTimeI Protected procedures as interrupt handlersI FIFO Within Priority dispatching policyI Ceiling Locking locking policyI Pragmas Atomic and Volatile for shared dataI Count attribute (but not in a barrier expression)I Discriminants for protected types and task types

Jakaria Abdullah Lab 2: LEGO 12 September 2016 10 / 25

Page 11: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Compile

1 Use cygwin terminal to compile programs in Windows2 May need to adjust the provided Makefile3 Compile program using make all4 Clean up compilation by make clean

Example Run: Program compile via Cygwin terminal

$ cd /cygdrive/c/gnat /2012/ test -folder

$ make all

..

A lot of compiler messages will be generated ,

a successful compilation will have no error

...

$ make clean # optional , but useful

...

$

Jakaria Abdullah Lab 2: LEGO 12 September 2016 11 / 25

Page 12: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Upload

1 Connect NXT unit to USB port

2 Power up NXT unit

3 Put NXT into reset mode

4 Ticking sound means NXT is ready to upload

5 Upload program using samba

Example Run: Program upload via Cygwin

$ cd /cygdrive/c/gnat /2012/ test -folder

$ samba_run event_driven

...

Image download complete.

Image started at 0x0020235c

.....

$

Jakaria Abdullah Lab 2: LEGO 12 September 2016 12 / 25

Page 13: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Upload

1 Connect NXT unit to USB port

2 Power up NXT unit

3 Put NXT into reset mode

4 Ticking sound means NXT is ready to upload

5 Upload program using samba

Example Run: Program upload via Cygwin

$ cd /cygdrive/c/gnat /2012/ test -folder

$ samba_run event_driven

...

Image download complete.

Image started at 0x0020235c

.....

$

Jakaria Abdullah Lab 2: LEGO 12 September 2016 12 / 25

Page 14: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Structure

You “program” three files:1 A main procedure file : ads source file2 Task specification package file: ads source file3 Task implementation file: adb source file

Main Procedure File:I Staring point of your programI This procedure name will be the name of your compiled program

Task specifications:I Define global variables, task periods, etcI Initialize sensors and motors with proper ports

Task implementations:I Task body code for implementation

Will do a short walk-through now

See the lab web page for example codes

Jakaria Abdullah Lab 2: LEGO 12 September 2016 13 / 25

Page 15: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Structure

You “program” three files:1 A main procedure file : ads source file2 Task specification package file: ads source file3 Task implementation file: adb source file

Main Procedure File:I Staring point of your programI This procedure name will be the name of your compiled program

Task specifications:I Define global variables, task periods, etcI Initialize sensors and motors with proper ports

Task implementations:I Task body code for implementation

Will do a short walk-through now

See the lab web page for example codes

Jakaria Abdullah Lab 2: LEGO 12 September 2016 13 / 25

Page 16: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Structure

You “program” three files:1 A main procedure file : ads source file2 Task specification package file: ads source file3 Task implementation file: adb source file

Main Procedure File:I Staring point of your programI This procedure name will be the name of your compiled program

Task specifications:I Define global variables, task periods, etcI Initialize sensors and motors with proper ports

Task implementations:I Task body code for implementation

Will do a short walk-through now

See the lab web page for example codes

Jakaria Abdullah Lab 2: LEGO 12 September 2016 13 / 25

Page 17: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Program Structure

You “program” three files:1 A main procedure file : ads source file2 Task specification package file: ads source file3 Task implementation file: adb source file

Main Procedure File:I Staring point of your programI This procedure name will be the name of your compiled program

Task specifications:I Define global variables, task periods, etcI Initialize sensors and motors with proper ports

Task implementations:I Task body code for implementation

Will do a short walk-through now

See the lab web page for example codes

Jakaria Abdullah Lab 2: LEGO 12 September 2016 13 / 25

Page 18: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

I/O Interfacing

Input via orange button and sensorsI Initialize sensors before use

Output via LCD (strings, integers), sound and motors

Sensor and motor access via ports: Motor A, ..., Sensor 1, ..

See examples!

Example: Initializing sensors and motors

1 Touch_Sensor_Id : Touch_Sensor (Sensor_1);

2

3 Light_Sensor1 : Light_Sensor := Make(Sensor_2 , True);

4

5

6 Right_Motor_Id : constant Motor_Id := Motor_A;

7 Left_Motor_Id : constant Motor_Id := Motor_B;

Jakaria Abdullah Lab 2: LEGO 12 September 2016 14 / 25

Page 19: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Motor control

Motor control API provide high level functions for motor movements

Example: Using motor control API

1 procedure Forwards is

2 begin

3 Control_Motor (Right_Motor_Id , Speed_Full , Forward);

4 Control_Motor (Left_Motor_Id , Speed_Full , Forward);

5 end Forwards;

6

7 procedure Turn_Left is

8 begin

9 Control_Motor (Right_Motor_Id , Speed_Full , Backward);

10 Control_Motor (Left_Motor_Id , Speed_Half , Backward);

11 end Turn_Left;

Jakaria Abdullah Lab 2: LEGO 12 September 2016 15 / 25

Page 20: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Motor control

More precise control of motor operation can be done by usingnxt-motors-simple and nxt-motors-encoder drivers

Example: Testing motor encoders

1 procedure Test_Motor_Encoder is use NXT;

2

3 Engine : Simple_Motor := Make (Motor_A);

4

5 begin

6 Engine.Set_Power (50);

7 Engine.Forward;

8

9 loop

10 Put_Noupdate (Encoder_Count (Motor_A)); New_Line;

11 exit when Current_Button /= No_Button;

12 delay until clock + milliseconds (100);

13 end loop;

14 ....

Jakaria Abdullah Lab 2: LEGO 12 September 2016 16 / 25

Page 21: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Display Functions

nxt-display driver provide API for LCD display

Example: API for LCD display

1 procedure Clear_Screen_Noupdate;

2 procedure Clear_Screen;

3 procedure Set_Pos (Column : Char_Columns; Row :

Char_Rows);

4 -- Set current position.

5

6 procedure Put_Noupdate (C : Character);

7 procedure Put_Noupdate (S : String);

8 procedure Put_Noupdate (V : Integer);

9 procedure Put_Noupdate (V : Long_Long_Integer);

10 -- Write a character , a string and an integer.

11 -- Only CR and LF control characters are handled.

12 -- Note that the min and max values for

Long_Long_Integer will wrap around the display.

Jakaria Abdullah Lab 2: LEGO 12 September 2016 17 / 25

Page 22: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Display Functions

Example: More API for LCD display

1 procedure Put (C : Character);

2 procedure Put (S : String);

3 procedure Put_Line (S : String);

4 -- Like in Ada.Text_IO.

5

6 procedure Newline_Noupdate;

7 procedure Newline;

8 procedure New_Line renames Newline;

9 procedure New_Line_Noupdate renames Newline_Noupdate;

10 -- Like in Ada.Text_IO.

11

12 procedure Screen_Update;

13 -- Synchronize the LCD with the internal buffer.

Jakaria Abdullah Lab 2: LEGO 12 September 2016 18 / 25

Page 23: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Synchronization Features

Synchronization between tasks can done by protected objects

Ravenscar restriction: only one entry per protected object

Protected object itself should have priority at least as high as themaximum priority of the user tasks

Access to this protected object is controlled by Immediate CeilingLocking Protocol (ICPP)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 19 / 25

Page 24: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Synchronization Features

Example: Ravenscar-compliant Protected object

1 protected Event is

2

3 entry Wait(D : out Integer);

4 procedure Signal(D : in Integer);

5

6 private

7 -- protected object priority , ceiling of the user

priorities

8 pragma Priority(System.Priority ’First + 3);

9

10 -- protected object data declaration

11 Current : Integer;

12

13 -- barrier variable for the entry

14 Signalled : Boolean := False;

15 end Event;

Jakaria Abdullah Lab 2: LEGO 12 September 2016 20 / 25

Page 25: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Synchronization Features

Example: Ravenscar-compliant Protected object

1

2 protected body Event is

3 entry Wait(D : out Integer) when Signalled is

4 begin

5 D := Current;

6 Signalled := False;

7 end Wait;

8

9 procedure Signal(D : in Integer) is

10 begin

11 Current := D;

12 Signalled := True;

13 end Signal;

14 end Event;

Jakaria Abdullah Lab 2: LEGO 12 September 2016 21 / 25

Page 26: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab Assignment

Part 1: Warm-UpI Attach only light sensorI Write light valuesI Nothing fancy, just to get a soft start

Part 2: Event-driven SchedulingI Communication between tasks using protected objectI Application: Four events with car on table

1 Touch sensor is pressed/released2 Table edge is sensed (light sensor)

Part 3: Periodic SchedulingI Define different periodic tasksI Application: Distance and touch sensor sensing

1 Drive (back off) while sensor pressed2 Otherwise, keep distance constant

Last Part: Line TrackerI Apply all you have learnedI (See next slide)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 22 / 25

Page 27: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab Assignment

Part 1: Warm-UpI Attach only light sensorI Write light valuesI Nothing fancy, just to get a soft start

Part 2: Event-driven SchedulingI Communication between tasks using protected objectI Application: Four events with car on table

1 Touch sensor is pressed/released2 Table edge is sensed (light sensor)

Part 3: Periodic SchedulingI Define different periodic tasksI Application: Distance and touch sensor sensing

1 Drive (back off) while sensor pressed2 Otherwise, keep distance constant

Last Part: Line TrackerI Apply all you have learnedI (See next slide)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 22 / 25

Page 28: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab Assignment

Part 1: Warm-UpI Attach only light sensorI Write light valuesI Nothing fancy, just to get a soft start

Part 2: Event-driven SchedulingI Communication between tasks using protected objectI Application: Four events with car on table

1 Touch sensor is pressed/released2 Table edge is sensed (light sensor)

Part 3: Periodic SchedulingI Define different periodic tasksI Application: Distance and touch sensor sensing

1 Drive (back off) while sensor pressed2 Otherwise, keep distance constant

Last Part: Line TrackerI Apply all you have learnedI (See next slide)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 22 / 25

Page 29: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Lab Assignment

Part 1: Warm-UpI Attach only light sensorI Write light valuesI Nothing fancy, just to get a soft start

Part 2: Event-driven SchedulingI Communication between tasks using protected objectI Application: Four events with car on table

1 Touch sensor is pressed/released2 Table edge is sensed (light sensor)

Part 3: Periodic SchedulingI Define different periodic tasksI Application: Distance and touch sensor sensing

1 Drive (back off) while sensor pressed2 Otherwise, keep distance constant

Last Part: Line TrackerI Apply all you have learnedI (See next slide)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 22 / 25

Page 30: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

LEGO Car Race

Car demonstration takes place on Mon, 26.9.

Track looks roughly like this:

Procedure for each team:

1st phase: Follow another car in constant distance (20cm) for 1 lap2nd phase: Be fastest on the next lap

Fastest team wins! (Prize award included)

3 tries per team (otherwise: assignment failed, fix car)

Keep in mind: Demo conditions might differ (different light etc.)

Jakaria Abdullah Lab 2: LEGO 12 September 2016 23 / 25

Page 31: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

Some Additional Pointers

More information about NXT motors:http://www.philohome.com/nxtmotor/nxtmotor.htm

Useful tutorials about line follower Lego Robot:http://www.nxtprograms.com/line_follower/steps.html

http://www.inpharmix.com/jps/PID_Controller_For_Lego_

Mindstorms_Robots.html

Jakaria Abdullah Lab 2: LEGO 12 September 2016 24 / 25

Page 32: Programming LEGO Mindstorms NXT using Ada · Lab 2: Programming LEGO Mindstorms using Ada Lab goals: I Real-time programming on an embedded device I Using the Ada feature for concurrent

The End

Questions?

Jakaria Abdullah Lab 2: LEGO 12 September 2016 25 / 25