lab_autobalance

advertisement
ECE243
LEGO Autobalance LAB
1
But first: LOGICAL SHIFTS
• SLL/SLLI: shift left logical
– like ‘<<’ in C
• SRL/SRLI: shift right logical
– like ‘>>’ in C
• NOTE: new bits shifted in are zeros
• Ex: SLLI r8, r9, 0x4
• Ex: SRLI r8, r9, 0x3
2
Bit Fields and Masking
• Use a word, hword, or byte to hold multiple values
– ex, packing ctrl & data signals into a connector (GPIO)
– ex, packing small fields to save memory
• Ex: Course info:
– department: (ECE, CIV, etc) 12 total =>
– Course code: 0..2000
=>
– Fall, spring: F, S
=>
• Better than one halfword each:
– 3 * 16 bits = 48bits
3
Course info Example
#put dept in r8:
#Put course code in r8:
#Put F/S in r8:
4
Next Lab: You will Build a Segway!
5
Actually, a LEGO Autobalancer
6
LEGO Setup
7
LEGO Controller
F
0xF means
low light
0
0x0 means
full light
8
LEGO Breakout Box
9
Autobalancer with Breakout Box
10
Using Sensors
Fixed
Distance:
Sensor
0x0 (full light)
Sensor
0x7 (some light)
Sensor
0xF (low light)
Varying
Distance:
Sensor
0x0 (full light)
Sensor
0xD (low light)
11
Talking to LEGO Controler via GPIO
• Bits0..9
– configure/control 5 motors
– enable/disable; clockwise/counter-clockwise
• Bits10..19
– configure/control 5 sensors
• Bits27..30
– read data value from sensors
• 2 ways to use sensors:
– polling mode (default mode, recommended)
– state mode (more advanced, see documentation)
12
Motor control
0(JP1):
(DR):
9
8
7
6
5
4
3
2
1
0
for/
rev
on/
off
for/
rev
on/
off
for/
rev
on/
off
for/
rev
on/
off
for/
rev
on/
off
motor2
motor3
motor4
motor1
motor0
• for/rev: 0 = forwards/clockwise, 1 = reverse/ctr-clk-wise
• on/off: 0 = on, 1 = off
4(JP1):
(DIR)
9
8
7
6
5
4
3
2
1
0
1
1
1
1
1
1
1
1
1
1
motor4
motor3
motor2
motor1
motor0
• set to all 1’s, since they are all outputs
13
Sensor control
19
0(JP1):
(DR)
18
17
16
15
14
13
12
11
10
rdy? on/
off
rdy? on/
off
rdy? on/
off
rdy? on/
off
rdy? on/
off
sensor4
sensor3
sensor2
sensor1
sensor0
• on/off: 0 = on, 1 = off
• rdy (ready): 0 = ready/valid, 1 = not-ready/invalid
4(JP1):
(DIR)
19
18
17
16
15
14
13
12
11
10
0
1
0
1
0
1
0
1
0
1
sensor4
sensor3
sensor2
sensor1
sensor0
• set to 1’s for on/off bits (outputs)
• set to 0’s for ready bits (inputs)
14
Sensor values
0(JP1):
(DR)
30
29
28
27
-
-
-
-
Sensor value
• 4-bit sensor value
– can only read one sensor value at a time
– first enable one sensor, then await ‘ready/valid’, then read
4(JP1):
(DIR)
30
29
28
27
0
0
0
0
Sensor value
• set direction for sensor value bits to 0’s
• since they are inputs
15
Direction Register DIR
• Typical use of DIR:
– set to 1(output):
• bits0..9 (motor enables), bits10,12,14,16,18 (sensor enables)
– set to 0 (input):
• bits 11,13,15,17,19 (sensor readys), bits27..30 (sensor value)
– Set to 0bxx00 00xx xxxx 0101 0101 0111 1111 1111
– X’s must also be set to certain values (see docs)
• Magic number for typical use: (set DIR to this)
– 0b0000 0111 1111 0101 0101 0111 1111 1111
– 0x07f557ff
16
Ex: enable motor1, forward
.equ ADDR_JP1, 0x10000060
movia r8,ADDR_JP1
movia r9, 0x07f557ff
stwio r9, 4(r8)
# set DIR to typical-use value
movia r9, 0xfffffff3
# 0b... 11110011
# bit2=0 for enabled, bit3=0 for forward
stwio r9,0(r8)
17
Ex: read sensor2
.equ ADDR_JP1, 0x10000060
movia r8,ADDR_JP1
movia r9, 0x07f557ff # set DIR to typical-use value
stwio r9, 4(r8)
movia r9, 0xffffbfff # bit14=0 to enable sensor2
stwio r9,0(r8)
POLL
ldwio r9,0(r8) # read DR
srli r10,r9,15 # shift bit-15 to the bit-0 position
andi r10,r10,0x1 # mask it to isolate it
bne r10,r0,POLL # ready if r10==0, try again otherwise
srli r9,r9,27 # shift-right-logical-immed by 27 bits
# sensor value is now in lowest 4 bits of r9
andi r9,r9,0x0000000f # mask it to isolate it
# can now use sensor value in r9
18
Some Hints
• Do not use delay counters, must use timer
• Not all sensors are equal
– sensitivities vary, you must calibrate them
• motors will stall if you switch directions fast
• motors can be too fast/jerky:
– if you give it full voltage:
– instead give it part voltage:
– called Pulse Width Modulation (PWM)
• See DESL for full details!
– page on Lego Controller (quick reference)
– full Lego manual (pdf)
19
Download