Lego Controller

 

The University of Toronto lego Controller consists of two parts, a Breakout box

 (figure 1) and a Lego controller (figure2)

 

 

 

 

 

The 16 pin header on the lego controller connects the Breakout box to the lego controller. The 40 pin header connects the lego controller to the DE2 board. The full setup looks like (figure 3)

 

 

I) Breakout box

 

The Breakout box has 5 connections for sensors and 5 connections for the motors. Each 2X2 black lego block represents one connection to either a sensor or a motor. The 2X2 black blocks along the blue 1x8 plate and yellow 1x2 plate are used to connect to the sensors. The black 2X2 block beside the short yellow plate represents sensor 1. The others follow in numerical order. The 2X2 black blocks that are along the gray 1x10 plate are for the motors. The 2X2 block directly opposite the yellow 1X2 plate is motor 1. The others follow in numerical order. Due to limitation on the NIOS processor GPIO ports only the first 4 motors and sensors are used. The fifth motor and sensor will be used in future development.

 

 

II) Lego controller

 

The lego controller is an intelligent interface that communicates information between the NIOS processor on the DE2 board and the lego motors and sensors. There are 5 HEX displays on the lego controller. Each HEX display is used to represent a sensor value that is being read from the Breakout box. A HEX value of “F” represents no light and a HEX value of “0” represents full light. Each HEX display is labelled 1 through 5.

 

There are two ways to read the sensor values:

 

1) The sensor can be kept at a fixed distance and the surface colour/shade can be changed. A bright surface like yellow or white, will display a low HEX value. A darker surface colour like black or brown, will display a high HEX value. See (figure 4)

 

 

2) Keep a fixed light source on the sensor and move the sensor to and from the light source.  See (figure 5)

 

 

 

 

 

 

 

 

III)  40 pin header

 

The lego controller communicates with the DE2 board via the 40 pin header. There are three ports on the lego controller: port A  port B and port C. These ports align with the ports on the NIOS processor GPIO ports. Port A is used to drive the motors. Port B is used to enable reading the sensors values from the lego controller and also does control functions. Port C is used to send and receive data from the lego controller, as well as trigger interrupts. Table 1 shows a break down of what each bit on the port does.

 

Table 1

Port A

description

Bit = ‘1’

Bit = ‘0’

PA0

Motor1

Disabled

Enabled

PA1

Motor1

Counter Clockwise

Clockwise

PA2

Motor2

Disabled

Enabled

PA3

Motor2

Counter Clockwise

Clockwise

PA4

Motor3

Disabled

Enabled

PA5

Motor3

Counter Clockwise

Clockwise

PA6

Motor4

Disabled

Enabled

PA7

Motor4

Counter Clockwise

Clockwise

Port B

description

Bit = ’1’

Bit = ‘0’

PB0

Sensor1

Disabled

Enabled

PB1

Sensor2

Disabled

Enabled

PB2

Sensor3

Disabled

Enabled

PB3

Sensor4

Disabled

Enabled

PB4

Control

Polling

State

PB5

Control

Disabled

Load threshold value

PB6

N/A

N/A

N/A

PB7

N/A

N/A

N/A

Port C

description

Bit = ’1’

Bit = ‘0’

PC0

Control

Threshold data

Threshold data

PC1

Control

Threshold data

Threshold data

PC2

Control

Threshold data

Threshold data

PC3

Control

Threshold data

Threshold data

PC4

Control

Polling/State  data

Polling /State  data

PC5

Control

Polling /State  data

Polling /State  data

PC6

Control

Polling /State  data

Polling /State  data

PC7

Control

Polling /State  data

Polling /State  data

 

 

 

 

 

 

3.1) Port A

 

Motors are controlled by paired bits. To drive a motor the enable bit must be “0” and depending on the value in the direction bit the motor will either go clockwise or counter clockwise.

 

3.2) Port B

 

Port B is multi functioning.

 

PB0-3 are used to select sensors. PB0 selects sensor 1, PB1 selects sensor 2, PB2 selects sensor 3 and PB3 selects sensor 4.

 

PB4 is used to select between polling mode or state mode.

 

Polling mode (PB4 = ‘1’)

It is important to note that in polling mode you can only read one 4 bit sensor HEX value at a time from PC4-7. By setting one of the bits in PB0-3 to ‘0’, it will enable the NIOS processor to read the 4 bit sensor HEX value at PC4-7. For example to read sensor 1, PB0 must be ‘0’ then the 4 bit sensor HEX value can be read from PC4-7.

 

State mode (PB4 =’0’)

Depending on what the 4 bit threshold HEX value is, the state value of PC4-7 is either ‘1’ or ‘0’. For example, consider that the 4 bit threshold HEX value of sensor 1 has been preloaded to 9, if the input 4 bit sensor Hex value at sensor 1 is greater than or equal to 9 then  PC4 is ‘1’ otherwise it is ‘0’.  It is important to note that the 4 bit threshold HEX value for all sensors must be preloaded in order for state mode to work properly.

 

PB5 is used to load the 4 bit threshold HEX value PC0-3 into the lego controller register. If PB5 is ‘0’ and any of the sensor enable bits PB0-PB3 are ‘0’then the 4 bit threshold HEX value stored at PC0-3 is loaded into the 4 bit sensor threshold register on the lego controller. For example, to load the threshold value for sensor 1, the bits PB5 and PB0 must be ‘0’ and then the 4 bit threshold HEX value stored at PC0-3 on the NIOS processor is stored into the lego controller threshold register sensor 1.  A quick note that if the 4 bit threshold HEX value for all the sensors is the same then the 4 bit threshold HEX value can be loaded at the same time. See sample code below:

 

#load value into all sensors

 

 movia r2,ADDR_PORT2B

 movi  r3,0xd0          /* enable threshold register for all sensor*/

 stwio r3,0(r2)        

             

 

 movia r2,ADDR_PORT2C

 movi  r3,0x9           /* 4 bit threshold value */

 stwio r3,0(r2)

 

 movia r2,ADDR_PORT2B

 movi  r3,0xff          /* unselect sensor ports*/

 stwio r3,0(r2)  

 

 

 

3.3) Port C

 

Port C is multi functioning.

 

PC0-3 are used to store the 4 bit threshold value to be loaded into the lego controller register.

 

PC4-7 have a dual purpose.

 

In state mode (PB4 = ‘0’) if the value in the 4 bit threshold register is greater than the value in the 4 bit sensor input, then PC4-7 bits are ‘0’ otherwise they are ‘1’. In order to use interrupts with the lego controller you must run the lego controller in state mode. The bits at PC4-7 on the NIOS processor must be configured as interrupt inputs. To do this read the section in the NIOS handout that deals with setting up interrupts. .

 

In polling mode these 4 bits represent the HEX value that is displayed on the HEX display depending on which sensor is selected PB0-3.

 

 

 

IV) Slider Switch

 

The lego controller has a 3 position slider switch located at the bottom centre of the lego controller board.

 

When the switch is in the centre position “NIOS Mode” then the lego controller will act as a passive device and communicate with the NIOS GPIO ports JP1/2. *****NOTE***** The switch must be in centre position in order for the lego controller to properly communicate with the NIOS processor.

 

If the switch is in the right position “Motor Test Mode” then the lego controller will act as an active device and tests the lego motors. It is a quick test to see if all the motors are working. ****NOTE**** this test will only work when the mode switch is in the right position and the 40 pin ribbon cable is disconnected from the DE2 board. All motors are tested. The test goes through a repeating sequence where each motor is driven clockwise then counter clockwise then all the motors go clockwise and finally all the motors go counter clockwise.

 

If the switch is in the left position “Sensor Test Mode” then the lego controller will act as an active device and test the lego sensors. ****NOTE**** this test will only work when the mode switch is in the left position and the 40 pin ribbon cable is disconnected from the DE2 board. In this mode if the sensor value goes below HEX value ‘9’ then the motor will rotate. Sensor 1 turns motor1 on, sensor 2 turns motor 2 on and so on.