ECE 243 LEGO Autobalance LAB 1 But first
ECE 243 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 r 8, r 9, 0 x 4 • Ex: SRLI r 8, r 9, 0 x 3 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 = 48 bits 3
Course info Example #put dept in r 8: #Put course code in r 8: #Put F/S in r 8: 4
Next Lab: You will Build a Segway! 5
Actually, a LEGO Autobalancer 6
LEGO Setup 7
LEGO Controller F 0 x. F means low light 0 0 x 0 means full light 8
LEGO Breakout Box 9
Autobalancer with Breakout Box 10
Using Sensors Fixed Distance: Sensor Varying Distance: Sensor 0 x 0 (full light) 0 x 7 (some light) 0 x. F (low light) 0 x 0 (full light) 0 x. D (low light) 11
Talking to LEGO Controler via GPIO • Bits 0. . 9 – configure/control 5 motors – enable/disable; clockwise/counter-clockwise • Bits 10. . 19 – configure/control 5 sensors • Bits 27. . 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(JP 1): (DR): 9 8 7 6 5 4 3 2 1 0 for/ rev on/ off for/ rev on/ off motor 2 motor 3 motor 4 motor 1 motor 0 • for/rev: 0 = forwards/clockwise, 1 = reverse/ctr-clk-wise • on/off: 0 = on, 1 = off 4(JP 1): (DIR) 9 8 7 6 5 4 3 2 1 0 1 1 1 1 1 motor 4 motor 3 motor 2 motor 1 motor 0 • set to all 1’s, since they are all outputs 13
Sensor control 19 0(JP 1): (DR) 18 17 16 15 14 13 12 11 10 rdy? on/ off rdy? on/ off sensor 4 sensor 3 sensor 2 sensor 1 sensor 0 • on/off: 0 = on, 1 = off • rdy (ready): 0 = ready/valid, 1 = not-ready/invalid 4(JP 1): (DIR) 19 18 17 16 15 14 13 12 11 10 0 1 0 1 0 1 sensor 4 sensor 3 sensor 2 sensor 1 sensor 0 • set to 1’s for on/off bits (outputs) • set to 0’s for ready bits (inputs) 14
Sensor values 0(JP 1): (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(JP 1): (DIR) 30 29 28 27 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): • bits 0. . 9 (motor enables), bits 10, 12, 14, 16, 18 (sensor enables) – set to 0 (input): • bits 11, 13, 15, 17, 19 (sensor readys), bits 27. . 30 (sensor value) – Set to 0 bxx 00 00 xx xxxx 0101 0111 1111 – X’s must also be set to certain values (see docs) • Magic number for typical use: (set DIR to this) – 0 b 0000 0111 1111 0101 0111 1111 – 0 x 07 f 557 ff 16
Ex: enable motor 1, forward. equ ADDR_JP 1, 0 x 10000060 movia r 8, ADDR_JP 1 movia r 9, 0 x 07 f 557 ff stwio r 9, 4(r 8) # set DIR to typical-use value movia r 9, 0 xfffffff 3 # 0 b. . . 11110011 # bit 2=0 for enabled, bit 3=0 forward stwio r 9, 0(r 8) 17
Ex: read sensor 2. equ ADDR_JP 1, 0 x 10000060 movia r 8, ADDR_JP 1 movia r 9, 0 x 07 f 557 ff # set DIR to typical-use value stwio r 9, 4(r 8) movia r 9, 0 xffffbfff # bit 14=0 to enable sensor 2 stwio r 9, 0(r 8) POLL ldwio r 9, 0(r 8) # read DR srli r 10, r 9, 15 # shift bit-15 to the bit-0 position andi r 10, 0 x 1 # mask it to isolate it bne r 10, r 0, POLL # ready if r 10==0, try again otherwise srli r 9, 27 # shift-right-logical-immed by 27 bits # sensor value is now in lowest 4 bits of r 9 andi r 9, 0 x 0000000 f # mask it to isolate it # can now use sensor value in r 9 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
- Slides: 19