![]() |
MSPM0_ValvanoWare
1.0
ECE445L starter code
|
Software implementations of math functions. More...
Data Structures | |
| struct | complex |
| a complex number has a real and imaginary part More... | |
Macros | |
| #define | PI 3.14159265358979323846264338327950288 |
| PI is ratio of circumference to diameter of a circle. | |
| #define | N 360 |
| counts/rotation, just one edge of one tach | |
| #define | D 70000 |
| wheel diameter 0.0001cm | |
| #define | W 140000 |
| wheel base 0.0001 cm | |
| #define | C 219910 |
| wheel circumference 0.0001cm | |
| #define | PI 8192 |
| representation of pi radians | |
| #define | TWOPI (2*PI) |
| 6.28... | |
| #define | NORTH 4096 |
| direction that is north | |
| #define | EAST 0 |
| direction that is east | |
| #define | SOUTH -4096 |
| direction that is south | |
| #define | WEST -8192 |
| direction that is west | |
| #define | XYTOLERANCE 10000 |
| tolerance in x,y goal seeking, 1 cm | |
| #define | THETATOLERANCE (4096/90) |
| tolerance in angle goal seeking, 1 deg | |
Typedefs | |
| typedef struct complex | complex_t |
| typedef uint8_t | fuz_t |
| 8-bit fuzzy variable having values from true (255,254,253,...,2,1,0) false | |
Enumerations | |
| enum | RobotState { ISSTOPPED , GOFORWARD , HARDRIGHT , HARDLEFT , SOFTRIGHT , SOFTLEFT } |
| robot state More... | |
| enum | OdometryCommand { STOP , FORWARDTOX , FORWARDTOY , LEFTTOTH , RIGHTTOTH } |
| Odometry Commands. More... | |
Functions | |
| void | NoiseReject_Init (void) |
| Initialization of noise reject filter. More... | |
| int32_t | NoiseReject (int32_t x) |
| removes random noise from audio signal More... | |
| void | DFT_Init (void) |
| Initialization of discrete fourier transform. More... | |
| void | DFT (uint32_t i, int32_t x) |
| Run DFT. More... | |
| int32_t | Mag1 (void) |
| magnitude of the first frequency More... | |
| int32_t | Mag2 (void) |
| magnitude of the second frequency More... | |
| void | fft (complex_t *v, int n, complex_t *tmp) |
| fft More... | |
| void | ifft (complex_t *v, int n, complex_t *tmp) |
| Inverse fft. More... | |
| int16_t | fixed_sin (int32_t theta) |
| decimal fixed-point sin More... | |
| int16_t | fixed_cos (int32_t theta) |
| decimal fixed-point cos More... | |
| int32_t | fixed_sin2 (int32_t theta) |
| binary fixed-point sin More... | |
| int32_t | fixed_cos2 (int32_t theta) |
| binary fixed-point cos More... | |
| int32_t | sin540 (int32_t theta) |
| binary fixed-point sin More... | |
| int32_t | cos540 (int32_t theta) |
| binary fixed-point cos More... | |
| fuz_t | not (fuz_t u1) |
| Complement. More... | |
| fuz_t | or (fuz_t u1, fuz_t u2) |
| Fuzzy or. More... | |
| fuz_t | or3 (fuz_t u1, fuz_t u2, fuz_t u3) |
| Fuzzy or. More... | |
| fuz_t | and (fuz_t u1, fuz_t u2) |
| Fuzzy and. More... | |
| fuz_t | and3 (fuz_t u1, fuz_t u2, fuz_t u3) |
| Fuzzy and. More... | |
| uint32_t | umin32 (uint32_t u1, uint32_t u2) |
| minimum More... | |
| int32_t | min32 (int32_t n1, int32_t n2, int32_t n3) |
| minimum More... | |
| fuz_t | MinFuzzification (int32_t crisp, const int32_t MIN, const int32_t MAX) |
| Fuzzification. More... | |
| fuz_t | CenterFuzzification (int32_t crisp, const int32_t MIN, const int32_t CENTER, const int32_t MAX) |
| Fuzzification. More... | |
| fuz_t | LeftRightFuzzification (int32_t crisp, const int32_t MIN, const int32_t LEFT, const int32_t RIGHT, const int32_t MAX) |
| Fuzzification. More... | |
| fuz_t | MaxFuzzification (int32_t crisp, const int32_t MIN, const int32_t MAX) |
| Fuzzification. More... | |
| char | FuzzyChar (fuz_t fuz) |
| convert fuzzy variable to ASCII character More... | |
| uint32_t | median5 (uint32_t u[5]) |
| 5-wide median filter More... | |
| int8_t | Median (int8_t x) |
| 3-wide median filter More... | |
| int8_t | Median3 (int8_t u1, int8_t u2, int8_t u3) |
| 3-wide median filter More... | |
| int8_t | Median5 (int8_t x) |
| 5-wide median filter More... | |
| int8_t | Median7 (int8_t x) |
| 7-wide median filter More... | |
| void | Odometry_Init (int32_t initx, int32_t inity, int32_t initTheta) |
| Initialize odometry module. More... | |
| void | Odometry_Update (int32_t LCount, int32_t RCount) |
| Compute new position/angle of the robot. More... | |
| void | Odometry_SetPower (uint32_t fast, uint32_t slow) |
| Set power levels. More... | |
| int32_t | Odometry_GetX (void) |
| Get X position. More... | |
| int32_t | Odometry_GetY (void) |
| Get y position. More... | |
| int32_t | Odometry_GetAngle (void) |
| Get theta angle. More... | |
| void | Odometry_Get (int32_t *x, int32_t *y, int32_t *theta) |
| Get the current robot position. More... | |
| void | UpdatePosition (void) |
| update position More... | |
| void | Display (void) |
| OLED output. More... | |
| void | WaitUntilBumperTouched (void) |
| Wait until bumper touched. More... | |
| void | StopUntilBumperTouched (void) |
| Stop and wait until bumper touched. More... | |
| uint32_t | ForwardUntilCrash (void) |
| Go until bumper touched. More... | |
| uint32_t | ForwardUntilX (int32_t desiredX) |
| Forward Until X goal. More... | |
| uint32_t | ForwardUntilY (int32_t desiredY) |
| Forward Until Y goal. More... | |
| uint32_t | SoftLeftUntilTh (int32_t desiredTh) |
| Soft Left Until Theta. More... | |
| void | ForwardUntilXStart (int32_t thedesiredX) |
| start going Forward Until X More... | |
| uint32_t | ForwardUntilXStatus (void) |
| check status of Forward Until X command More... | |
| void | ForwardUntilYStart (int32_t thedesiredY) |
| start going Forward Until Y More... | |
| uint32_t | ForwardUntilYStatus (void) |
| check status of Forward Until Y command More... | |
| void | SoftLeftUntilThStart (int32_t thedesiredTh) |
| start going Forward Until Y More... | |
| uint32_t | ForwardUntilThStatus (void) |
| check status of soft left until theta command More... | |
| uint32_t | CheckGoal (void) |
| check status of odometry command More... | |
Variables | |
| float | Real |
| float | Imag |
Software implementations of math functions.
| enum OdometryCommand |
Odometry Commands.
These are possible odometry commands
| enum RobotState |
Fuzzy and.
Fuzzy logic and
| u1 | is a fuzzy variable |
| u2 | is a fuzzy variable |
Fuzzy and.
Fuzzy logic and
| u1 | is a fuzzy variable |
| u2 | is a fuzzy variable |
| u3 | is a fuzzy variable |
| fuz_t CenterFuzzification | ( | int32_t | crisp, |
| const int32_t | MIN, | ||
| const int32_t | CENTER, | ||
| const int32_t | MAX | ||
| ) |
Fuzzification.
Fuzzy logic fuzzification, converting crisp input to fuzzy variable.
If crisp is less than MIN, then fuzzy is false.
If crisp is between MIN and CENTER, then fuzzy is linear.
If crisp is CENTER, then fuzzy is true.
If crisp is between CENTER and max, then fuzzy is linear.
If crisp is greater than MAX, then fuzzy is false.
255
|.................... -
|................. / ...
|.............. / ........ \
|........... / .............. \
|........ / .................... \
|-----— ........................------—
0.....MIN ... CENTER ....... MAX
| crisp | is a crisp input |
| MIN | is threshold value for the crisp input |
| CENTER | is a second threshold value for the crisp input |
| MAX | is a third threshold value for the crisp input |
| uint32_t CheckGoal | ( | void | ) |
check status of odometry command
Odometry command to check status of any odometry command. Assumes ForwardUntilXStart ForwardUntilYStart or SoftLeftUntilThStart has been issued
| none |
| int32_t cos540 | ( | int32_t | theta | ) |
binary fixed-point cos
binaryfixed-point cos****************
| theta | 0 to 539, angle is in units 2*pi/540 = 0.011635528 radians (0 to 2pi) |
| void DFT | ( | uint32_t | i, |
| int32_t | x | ||
| ) |
Run DFT.
One point added to discrete fourier transform
| i | is 0 to 15 index |
| x | is input data at index i |
| void DFT_Init | ( | void | ) |
Initialization of discrete fourier transform.
Initialization of 16-point discrete fourier transform
| none |
| void Display | ( | void | ) |
OLED output.
Show current state on OLED
| none |
fft
discrete Fast Fourier Transform, Converts time to frequency domain
Assume the input is real data sampled at fs
Place n measurements into the real part of array v
Set the imaginary part of array v to 0
Call fft() and the results are returned back in array v
If the input is real only, then the transform will contain
Index 0 <= k < n/2 complex components of frequency f=k/fs
E.g., k=0 represents the DC component of the input
Index n/2 <= k < n are complex conjugates of the first half
| v | array of input data in complex form |
| n | size of the two arrays 2, 4, 8, ..., 2048 |
| tmp | temporary array of data in complex form |
| int16_t fixed_cos | ( | int32_t | theta | ) |
decimal fixed-point cos
decimal fixed-point cos****************
| theta | -3142 to 3142, angle is in units radians/1000 |
| int32_t fixed_cos2 | ( | int32_t | theta | ) |
binary fixed-point cos
binaryfixed-point cos****************
| theta | -8192 to 8191, angle is in units 2*pi/16384 radians (-pi to +pi) |
| int16_t fixed_sin | ( | int32_t | theta | ) |
decimal fixed-point sin
decimal fixed-point sin****************
| theta | -3142 to 3142, angle is in units radians/1000 |
| int32_t fixed_sin2 | ( | int32_t | theta | ) |
binary fixed-point sin
binary fixed-point sin****************
| theta | -8192 to 8191, angle is in units 2*pi/16384 radians (-pi to +pi) |
| uint32_t ForwardUntilCrash | ( | void | ) |
Go until bumper touched.
Go straight until bumper touched (crash)
| none |
| uint32_t ForwardUntilThStatus | ( | void | ) |
check status of soft left until theta command
Odometry command to check status of soft left until theta command. Assumes SoftLeftUntilThStart has been issued
| none |
| uint32_t ForwardUntilX | ( | int32_t | desiredX | ) |
Forward Until X goal.
Odometry command to Go straight until X goal has been reached. This routine will run until goal is met.
| desiredX | desired X position in units of 0.0001cm |
| void ForwardUntilXStart | ( | int32_t | thedesiredX | ) |
start going Forward Until X
Odometry command to Go straight until X goal has been reached. This routine will start the command, but return immediately
| thedesiredX | desired X position in units of 0.0001cm |
| uint32_t ForwardUntilXStatus | ( | void | ) |
check status of Forward Until X command
Odometry command to check status of Go straight until X command. Assumes ForwardUntilXStart has been issued
| none |
| uint32_t ForwardUntilY | ( | int32_t | desiredY | ) |
Forward Until Y goal.
Odometry command to Go straight until Y goal has been reached. This routine will run until goal is met.
| desiredY | desired Y position in units of 0.0001cm |
| void ForwardUntilYStart | ( | int32_t | thedesiredY | ) |
start going Forward Until Y
Odometry command to Go straight until Y goal has been reached. This routine will start the command, but return immediately
| thedesiredY | desired Y position in units of 0.0001cm |
| uint32_t ForwardUntilYStatus | ( | void | ) |
check status of Forward Until Y command
Odometry command to check status of Go straight until Y command. Assumes ForwardUntilYStart has been issued
| none |
| char FuzzyChar | ( | fuz_t | fuz | ) |
convert fuzzy variable to ASCII character
Fuzzy logic display helper function return 'a' + (25*fuz)/255;
| fuz | is a fuzzy variable having values from 0(false) to 255(true) |
Inverse fft.
discrete Inverse Fast Fourier Transform, Converts frequency to time domain
Assume the input is complex data with components k=0 to n-1
Index 0 <= k < n-1 complex components of frequency f=k/fs
E.g., k=0 represents the DC component of the input
Place n complex values into the array v
Typically the index n/2 <= k < n are complex conjugates of the first half
Call ifft() and the results are returned back in array v
If the input is real only, then the transform will contain
| v | array of input data in complex form |
| n | size of the two arrays 2, 4, 8, ..., 2048 |
| tmp | temporary array of data in complex form |
| fuz_t LeftRightFuzzification | ( | int32_t | crisp, |
| const int32_t | MIN, | ||
| const int32_t | LEFT, | ||
| const int32_t | RIGHT, | ||
| const int32_t | MAX | ||
| ) |
Fuzzification.
Fuzzy logic fuzzification, converting crisp input to fuzzy variable.
If crisp is less than MIN, then fuzzy is false.
If crisp is between MIN and LEFT, then fuzzy is linear.
If crisp is between LEFT and RIGHT, then fuzzy is true.
If crisp is between RIGHT and max, then fuzzy is linear.
If crisp is greater than MAX, then fuzzy is false.
255
|.................... -—
|................. / ......
|.............. / ........... \
|........... / ................. \
|........ / ....................... \
|-----— ...........................------—
0.....MIN ... LEFT RIGHT.... MAX
| crisp | is a crisp input |
| MIN | is threshold value for the crisp input |
| LEFT | is a second threshold value for the crisp input |
| RIGHT | is a third threshold value for the crisp input |
| MAX | is a fourth threshold value for the crisp input |
| int32_t Mag1 | ( | void | ) |
magnitude of the first frequency
Calculate magnitude of the first frequency
| none |
| int32_t Mag2 | ( | void | ) |
magnitude of the second frequency
Calculate magnitude of the second frequency
| none |
| fuz_t MaxFuzzification | ( | int32_t | crisp, |
| const int32_t | MIN, | ||
| const int32_t | MAX | ||
| ) |
Fuzzification.
Fuzzy logic fuzzification, converting crisp input to fuzzy variable.
If crisp is less than MIN, then fuzzy is false.
If crisp is between MIN and MAX, then fuzzy is linear.
If crisp is greater than MAX, then fuzzy is true.
255
|........................------—
|...................... /
|................... /
|................ /
|............. /
|---------—
0..........MIN.......MAX
| crisp | is a crisp input |
| MIN | is threshold value for the crisp input |
| MAX | is another threshold value for the crisp input |
| int8_t Median | ( | int8_t | x | ) |
3-wide median filter
3-wide median filter
Includes a 3-deep MACQ of last three samples
| x | new data sample |
| int8_t Median3 | ( | int8_t | u1, |
| int8_t | u2, | ||
| int8_t | u3 | ||
| ) |
3-wide median filter
3-wide median filter
| u1 | new data sample |
| u2 | new data sample |
| u3 | new data sample |
| int8_t Median5 | ( | int8_t | x | ) |
5-wide median filter
5-wide median filter
Includes a 5-deep MACQ of last five samples
| x | new data sample |
| uint32_t median5 | ( | uint32_t | u[5] | ) |
5-wide median filter
5-wide median filter
| u | an array of 5 samples |
| int8_t Median7 | ( | int8_t | x | ) |
7-wide median filter
7-wide median filter
Includes a 7-deep MACQ of last seven samples
| x | new data sample |
| int32_t min32 | ( | int32_t | n1, |
| int32_t | n2, | ||
| int32_t | n3 | ||
| ) |
minimum
32-bit signed minimum
| n1 | is a 32-bit signed number |
| n2 | is a 32-bit signed number |
| n3 | is a 32-bit signed number |
| fuz_t MinFuzzification | ( | int32_t | crisp, |
| const int32_t | MIN, | ||
| const int32_t | MAX | ||
| ) |
Fuzzification.
Fuzzy logic fuzzification, converting crisp input to fuzzy variable.
If crisp is less than MIN, then fuzzy is true.
If crisp is between MIN and MAX, then fuzzy is linear.
If crisp is greater than MAX, then fuzzy is false.
255
|-------—
|........... \
|.............. \
|................. \
|.................... \
|....................... -------—
0..........MIN.......MAX
| crisp | is a crisp input |
| MIN | is threshold value for the crisp input |
| MAX | is another threshold value for the crisp input |
| int32_t NoiseReject | ( | int32_t | x | ) |
removes random noise from audio signal
Run noise reject filter
Rxx2 is 0 if uncorrelated (noise)
Rxx2 is 128 if correlated (signal)
Rxx2 is -128 if correlated (signal); this does not occur with sound
| x | input to filter |
| void NoiseReject_Init | ( | void | ) |
Initialization of noise reject filter.
Initialization of noise reject filter
compare audio signal with itself two samples ago
Rxx(2) = (1/N) sum{x(n)x(n-2)} in limit as N-> infinity
Approximate infinite sum with IIR filter
Rxx2 = (127*Rxx2 +x(n)x(n-2) )/128
| none |
Complement.
Fuzzy logic complement
| u1 | is a fuzzy variable having values from 0(false) to 255(true) |
| void Odometry_Get | ( | int32_t * | x, |
| int32_t * | y, | ||
| int32_t * | theta | ||
| ) |
Get the current robot position.
Get the current robot position.
| x | is pointer to store x position of the robot (units of 0.0001cm) |
| y | is pointer to store y position of the robot (units of 0.0001cm) |
| theta | is pointer to store angle (or direction) of the robot (units of 2*pi/16384 radians) |
| int32_t Odometry_GetAngle | ( | void | ) |
Get theta angle.
Fetch value of the current theta angle of the robot
| none |
| int32_t Odometry_GetX | ( | void | ) |
Get X position.
Fetch value of the current X position of the robot
| none |
| int32_t Odometry_GetY | ( | void | ) |
Get y position.
Fetch value of the current y position of the robot
| none |
| void Odometry_Init | ( | int32_t | initx, |
| int32_t | inity, | ||
| int32_t | initTheta | ||
| ) |
Initialize odometry module.
Initialize odometry module by specifying the initial position/angle of the robot. Angles range from (-pi to +pi) signified by -8192 to +8191.
| initx | initial x position of the robot (units of 0.0001cm) |
| inity | initial y position of the robot (units of 0.0001cm) |
| initTheta | initial angle of the robot (units of 2*pi/16384 radians) |
| void Odometry_SetPower | ( | uint32_t | fast, |
| uint32_t | slow | ||
| ) |
Set power levels.
Specifies two power levels to use during odometry task, and PWM levels must be between 0 to 14,998. Obviously, the power level must be high enough to move robot
| fast | PWM level for moving forward |
| slow | PWM level for turning |
| void Odometry_Update | ( | int32_t | LCount, |
| int32_t | RCount | ||
| ) |
Compute new position/angle of the robot.
Inputs are wheel tachometer counts (n counts/rotation), and outputs are private global variable of new position/angle
| LCount | number of left wheel tachometer counts from the last update |
| RCount | number of right wheel tachometer counts from the last update |
Fuzzy or.
Fuzzy logic or
| u1 | is a fuzzy variable |
| u2 | is a fuzzy variable |
Fuzzy or.
Fuzzy logic or
| u1 | is a fuzzy variable |
| u2 | is a fuzzy variable |
| u3 | is a fuzzy variable |
| int32_t sin540 | ( | int32_t | theta | ) |
binary fixed-point sin
binary fixed-point sin****************
e.g., 359.3degrees (2pi radians) theta=539 sin540(539) = -763 (-0.0116)
e.g., 270 degrees (3pi/4 radians) theta=405 sin540(405) = -65536 (-1)
e.g., 180 degrees (pi radians) theta=270 sin540(270) = 0 (0)
e.g., 90 degrees (pi/2 radians) theta=135 sin540(135) = 65536 (+1)
e.g., 45 degrees (pi/4 radians) theta= 67 sin540(67) = 46071 (sqrt(2)/2)
e.g., 0 degrees (0 radians) theta= 0 sin540(0) = 0 (0)
| theta | 0 to 539, angle is in units 2*pi/540 = 0.011635528 radians (0 to 2pi) |
| uint32_t SoftLeftUntilTh | ( | int32_t | desiredTh | ) |
Soft Left Until Theta.
Odometry command to soft left until theta goal has been reached. This routine will run until goal is met.
| desiredTh | desired theta position in units of 2*pi/16384 radians |
| void SoftLeftUntilThStart | ( | int32_t | thedesiredTh | ) |
start going Forward Until Y
Odometry command to soft left until theta goal has been reached. This routine will start the command, but return immediately
| thedesiredTh | desired theta position in units of 2*pi/16384 radians |
| void StopUntilBumperTouched | ( | void | ) |
Stop and wait until bumper touched.
Stop and wait until bumper touched
| none |
| uint32_t umin32 | ( | uint32_t | u1, |
| uint32_t | u2 | ||
| ) |
minimum
32-bit unsigned minimum
| u1 | is a 32-bit unsigned number |
| u2 | is a 32-bit unsigned number |
| void UpdatePosition | ( | void | ) |
update position
Take wheel counts and update position
| none |
| void WaitUntilBumperTouched | ( | void | ) |
Wait until bumper touched.
Wait until bumper touched
| none |