190 likes | 342 Vues
ECE243. LEGO Autobalance LAB. 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 . Bit Fields and Masking.
E N D
ECE243 LEGO Autobalance LAB
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
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
Course info Example #put dept in r8: #Put course code in r8: #Put F/S in r8:
LEGO Controller 0 F 0x0 means full light 0xF means low light
Using Sensors Sensor Sensor Sensor Sensor Sensor 0x0 (full light) 0xF (low light) 0x0 (full light) 0xD (low light) Fixed Distance: 0x7 (some light) Varying Distance:
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)
Motor control motor4 motor2 motor1 motor0 motor3 motor2 motor1 motor0 motor3 motor4 • for/rev: 0 = forwards/clockwise, 1 = reverse/ctr-clk-wise • on/off: 0 = on, 1 = off 0(JP1): (DR): 4(JP1): (DIR) • set to all 1’s, since they are all outputs
Sensor control sensor4 sensor2 sensor1 sensor0 sensor3 sensor3 sensor2 sensor1 sensor0 sensor4 • on/off: 0 = on, 1 = off • rdy (ready): 0 = ready/valid, 1 = not-ready/invalid 0(JP1): (DR) 4(JP1): (DIR) • set to 1’s for on/off bits (outputs) • set to 0’s for ready bits (inputs)
Sensor values Sensor value 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 0(JP1): (DR) 4(JP1): (DIR) • set direction for sensor value bits to 0’s • since they are inputs
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
Ex: enable motor1, forward .equ ADDR_JP1, 0x10000060 movia r8,ADDR_JP1 movia r9, 0x07f557ff # set DIR to typical-use value stwio r9, 4(r8) movia r9, 0xfffffff3 # 0b... 11110011 # bit2=0 for enabled, bit3=0 for forward stwio r9,0(r8)
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
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)