Title: CS 102B Robot Design Thursday, 111104
1CS 102B Robot DesignThursday, 11/11/04
- Todays Class
- Return Project 3
- Questions on Proximity Lab, Project 4?
- RCX Arithmetic
- Overflow and roundoff
- Boxcar averaging
- Lab work
- Modify proximity sensor code from Overmars, Chap.
9, p. 30, to compare 4th reading with average of
last 3 sensor readings (see Ferrari, p. 220) - Time to work on Project 4
- Reading
- Ferrari Chap. 12, pp. 214-220
2An arithmetic example related to the RCX
- Suppose we have built a robot with the following
properties (similar to Ferrari, pp. 72-73) - Motor is connected to wheel with 13 ratio (so
wheel rotates at 1/3 the rate of motor), - Rotation sensor attached to motor axle (16 ticks
per axle turn) - Wheel diameter is 42.5 mm (Lego, from Denmark, is
metric) - Goal Write a subprogram that will make the robot
go forward a specified distance. - How is the distance traveled by the wheel related
to the sensor reading? - Distance traveled ?? (Wheel circumference)
( wheel axle turns)
3Example Solution
- Introduce some letters to represent quantities we
know - G gear ratio from wheel to motor 3
- R sensor ticks per turn 16
- S sensor reading
- D Wheel diameter 42.5
- Quantities we want to know
- T Traveled distance
- C Wheel circumference
- W wheel axle turns
- Also useful
- M motor axle turns
- The formula we need then is
- T C W
- How do we compute C?
- How do we compute W?
- Useful observations
- M S / R
- W M / G
- C ? D
- Do the math to solve for S!
4The function GoFwd(int T)
- //D diameter of wheel, G 1/(gear ratio),
- //R rotation sensor resolution
- define D 82
- define G 3
- define R 16
- task main()
- SetSensorType (SENSOR_1, SENSOR_TYPE_ROTATION)
- GoFwd(300)
- Wait(400)
- GoFwd(500)
void GoFwd(int T) int S (G R T) / (3
D) ClearSensor (SENSOR_1) OnFwd
(OUT_AOUT_C) until (abs(SENSOR_1) gt S)
Off (OUT_AOUT_C)
5Doing arithmetic with the RCX
- RCX uses integers only
- Numeric range -32768 to 32767 (16 bits)
- Overflow Any numbers that fall outside this
range will interpreted incorrectly by the RCX - Roundoff Errors that occur because all stored
numbers are integers no fractions. - GoFwd() example How close to correct are the RCX
computations? - Compute exact value of S and compare with RCX
answer
6Avoiding Overflow and Roundoff Error
- Avoiding Overflow
- Avoid computing AB when A and B both large
- 182 182 33,124
- Avoiding Roundoff Error
- Dont divide smaller A by larger B
- A / B 0 if A lt B
- When computing A B / C, do multiplication
first - A (B / C) multiplies B/C remainder (error lt C)
by A - (A B) / C has error lt C
- Exception Dont multiply A B if product causes
overflow! - Which is better (A B) / C, or (A / C) (B / C)
7New math topic Boxcar Averaging
- Example Proximity Sensing
- Use IR reflection
- Repeatedly take sensor readings
- Compare latest to previous
- Turn if difference great enough
- What if we want to repeatedly compare latest to
average of last three sensor readings - Called boxcar averaging or moving average
8Computing averages
- Basic average rule to average values V1 to Vn
- A (V1 V2 ... Vn) / n
- When looking for change in sensor readings (to
detect proximity), may want to compare to average
of several previous readings to get sense of
ambient values
9Moving (boxcar) averages
int ave, v1, v2, v3 v2 SENSOR_1 v3
SENSOR_1 while (true) v1 v2 v2 v3
v3 SENSOR_1 ave (v1v2v3) / 3 //
other instructions...
- A moving average is a continually updated average
of the most recent values - Trace code at right, from Ferrari, p. 220