Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We...

31
Introduction to Lab 2 Programming LEGO Mindstorms NXT using Ada Jakaria Abdullah 19 September 2017 Jakaria Abdullah Lab 2: LEGO 19 September 2017 1 / 24

Transcript of Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We...

Page 1: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Introduction to Lab 2Programming LEGO Mindstorms NXT using Ada

Jakaria Abdullah

19 September 2017

Jakaria Abdullah Lab 2: LEGO 19 September 2017 1 / 24

Page 2: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Lab 2: Programming LEGO NXTs using Ada

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

Lab organization:I Work in your groupsI Get LEGO box after this lecture, charge batteryI Labs will be done on 2315 (building 2 floor 3)

F Thu, 21.9, 08:15-12:00F Thu, 28.9, 13:15-17:00

I Participate in demonstration on 02.10. in 2315I Have a look at the lab homepage

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

Lab report:I Ada code to all parts, well commentedI Submission page in studentportalen; Deadline: Mon, 02.10. at 23:59

Jakaria Abdullah Lab 2: LEGO 19 September 2017 2 / 24

Page 3: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Lab 2: LEGO NXT Boxes

Each group gets one box

All group members are responsible for box parts

For missing or faulty hardware issues, report to lab assistants

Return all hardware to assistants on Mon, 02.10

Check instructions in the box, to arrange items

Lab assistant will flash the default firmware

Jakaria Abdullah Lab 2: LEGO 19 September 2017 3 / 24

Page 4: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Lab 2: Working At Home

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

Toolchain installation is non-trivialI Firmware upload, program compile, program uploadI Windows: Need CygwinI I can’t give support for thatI An instruction file is available at lab homepage

External toolchain sources:I Linux toolchain from UPMI Telecom Paristech Lego corner

Default: Work in the Windows lab (2315)

Jakaria Abdullah Lab 2: LEGO 19 September 2017 4 / 24

Page 5: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 5 / 24

Page 6: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 6 / 24

Page 7: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Programming Environment: Ada NXT

We don’t use the standard firmware

Instead: Ada runtime system for NXTI Not a real-time operating systemI Supports

F Concurrent tasks, priorities,F Fixed priority preemptive schedulingF Resource sharing using protected objectsF Drivers for low-level I/O accesses

I Only 4186 lines!I Program, drivers and runtime system can run together inside the RAM!

Based on Ravenscar Small Footprint Profile (SFP)

Ravenscar is restricted subset of Ada suitable for static analysis

Jakaria Abdullah Lab 2: LEGO 19 September 2017 7 / 24

Page 8: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 8 / 24

Page 9: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Ravenscar Profile

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

Some restrictions:I Task type and task object declarations only in library level packagesI At most one entry for each protected object/typeI Each entry barrier expression must be single Boolean variableI At most one task at a time may be queued on an entryI No select statements and no task entriesI No relative delay (delay) statements (use delay until)I No references to package Ada.Calendar (use Ada.RealTime)I No ’Image and ’Value attribute

Jakaria Abdullah Lab 2: LEGO 19 September 2017 9 / 24

Page 10: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 10 / 24

Page 11: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 11 / 24

Page 12: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 11 / 24

Page 13: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 12 / 24

Page 14: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 12 / 24

Page 15: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 12 / 24

Page 16: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 12 / 24

Page 17: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 13 / 24

Page 18: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Motor control

Motor control API provides 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 19 September 2017 14 / 24

Page 19: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 15 / 24

Page 20: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Display Functions

nxt-display driver provides 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 19 September 2017 16 / 24

Page 21: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 17 / 24

Page 22: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Synchronization Features

Synchronization between tasks 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 19 September 2017 18 / 24

Page 23: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 19 / 24

Page 24: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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 19 September 2017 20 / 24

Page 25: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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: Sensor readings with periodic tasks

1 Drive backward if table edge is detected2 Drive forward when button is pressed and on table

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

Jakaria Abdullah Lab 2: LEGO 19 September 2017 21 / 24

Page 26: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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: Sensor readings with periodic tasks

1 Drive backward if table edge is detected2 Drive forward when button is pressed and on table

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

Jakaria Abdullah Lab 2: LEGO 19 September 2017 21 / 24

Page 27: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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: Sensor readings with periodic tasks

1 Drive backward if table edge is detected2 Drive forward when button is pressed and on table

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

Jakaria Abdullah Lab 2: LEGO 19 September 2017 21 / 24

Page 28: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

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: Sensor readings with periodic tasks

1 Drive backward if table edge is detected2 Drive forward when button is pressed and on table

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

Jakaria Abdullah Lab 2: LEGO 19 September 2017 21 / 24

Page 29: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

LEGO Line Tracker

Car demonstration takes place on Mon, 02.10.

Track looks roughly like this:

Objectives:

1. Follow the line and complete a lap2. Try to be fastest, finish the lap within maximum 1

minute

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

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

Jakaria Abdullah Lab 2: LEGO 19 September 2017 22 / 24

Page 30: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

Some Additional Pointers

Try to compile demo programs available in:/gnat/2012/share/examples/mindstorm-nxt/demos

Details of available drivers are in:/gnat/2012/lib/mindstorm-nxt/drivers

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 19 September 2017 23 / 24

Page 31: Programming LEGO Mindstorms NXT using Ada · 2017. 9. 19. · Programming Environment: Ada NXT We don’t use the standard rmware Instead: Ada runtime system for NXT I Not a real-time

The End

Questions?

Jakaria Abdullah Lab 2: LEGO 19 September 2017 24 / 24