roneos
Operating System for the r-one robot
 All Data Structures Files Functions Variables Typedefs Groups
intMath.h
Go to the documentation of this file.
1 
8 #ifndef intMath_h
9 #define intMath_h
10 
11 
12 /******** Defines ********/
13 #define __PI 3.14159
14 
15 /* Angles */
16 #define MILLIRAD_HALF_PI 1571
17 #define MILLIRAD_PI (MILLIRAD_HALF_PI * 2)
18 #define MILLIRAD_2PI (MILLIRAD_HALF_PI * 4)
19 
20 #define MILLIRAD_DEG_0 0
21 #define MILLIRAD_DEG_15 (MILLIRAD_HALF_PI / 6)
22 #define MILLIRAD_DEG_20 (MILLIRAD_PI / 9)
23 #define MILLIRAD_DEG_30 (MILLIRAD_HALF_PI / 3)
24 #define MILLIRAD_DEG_45 (MILLIRAD_HALF_PI / 2)
25 #define MILLIRAD_DEG_90 MILLIRAD_HALF_PI
26 #define MILLIRAD_DEG_180 MILLIRAD_PI
27 #define MILLIRAD_DEG_270 (MILLIRAD_PI + MILLIRAD_HALF_PI)
28 #define MILLIRAD_DEG_360 MILLIRAD_2PI
29 
30 #define MILLIRAD_TRIG_SCALER 32767
31 
32 #define GAUSSIAN_LOOKUP_SIZE 64
33 #define GAUSSIAN_LOOKUP_MAX_RETURN 255
34 
35 
36 /******** Structs ********/
37 
41 typedef struct Pose {
42  int32 x;
43  int32 y;
44  int32 theta;
45 } Pose;
46 
47 extern Pose poseOrigin;
48 
49 
50 
51 /******** Macros ********/
52 
53 
54 /******** Functions ********/
55 
56 //General math
57 
66 uint32 sqrtInt(uint32 val);
67 
68 
76 int32 vectorMag(int32 x, int32 y);
77 
84 uint32 decToZero(uint32 val);
85 
86 
94 int32 average(int32 val1, int32 val2);
95 
96 
104 uint8 bitsCount(uint32 val);
105 
106 
114 int32 min(int32 x, int32 y);
115 
116 
125 int32 max(int32 x, int32 y);
126 
127 
137 int32 boundAbs(int32 val, int32 bound);
138 
139 
150 int32 bound(int32 val, int32 lowerBound, int32 upperBound);
151 
152 // trig and angles
153 
160 int16 sinMilliRad(int16 angle);
161 
162 
169 int16 cosMilliRad(int16 angle);
170 
171 
180 int16 atan2MilliRad(int32 y, int32 x);
181 
182 
190 int16 normalizeAngleMilliRad(int16 angle);
191 
192 
200 int16 normalizeAngleMilliRad2(int16 angle);
201 
202 
210 int32 normalizeAngleMicroRad(int32 angle);
211 
212 
213 //TODO: Commented out in C file, Should this be deleted
214 int16 normalizeAngleMilliRad3(int16 angle);
215 
216 
225 int16 smallestAngleDifference(int16 thetaGoal, int16 theta);
226 
227 
236 int32 min3(int32 valueA, int32 valueB, int32 valueC);
237 
238 // Averaging and arrays
239 
240 //Is this even right? Updated so that params are the same but there's a TODO:check for overflow in the actual code
251 int32 filterIIR(int32 sample, int32 average, int32 alpha);
252 
253 
261 int16 averageAngles(int16 angle1, int16 angle2);
262 
263 
271 int32 averageAnglesMicroRad(int32 theta1, int32 theta2);
272 
273 
282 int16 averageArrayAngle(int16 angleArray[], int32 size);
283 
284 
292 int16 averageAnglesLeftToRight(int16 angleLeft, int16 angleRight);
293 
294 
302 int16 byteToMillirad(int8 angle);
303 
304 
312 int8 milliradToByte(int16 angle);
313 
314 
315 // byte packing
316 
324 void pack16(uint8 * arrayPtr, uint32 dataWord);
325 
326 
334 void pack24(uint8 * arrayPtr, uint32 dataWord);
335 
336 
344 void pack32(uint8 * arrayPtr, uint32 dataWord);
345 
346 
353 uint16 unpack16(uint8 * arrayPtr);
354 
355 
362 uint32 unpack24(uint8 * arrayPtr);
363 
364 
372 uint32 unpack32(uint8 * arrayPtr);
373 
374 // Angle - Byte converting
375 
383 int16 byteToMillirad(int8 angle);
384 
385 
391 int8 milliradToByte(int16 angle);
392 
393 
401 int16 byteToMilliradUnsigned(uint8 angle);
402 
403 
411 uint8 milliradToByteUnsigned(int16 angle);
412 
413 
414 //for midi parser
415 
416 /*
417  * @brief Converts string of bytes into a long int.
418  *
419  * Converts a string of 'len' bytes, MSB first, into a long int. This is
420  * not to be confused with 'atoi()': 'atoi()' converts "abcdef12" into
421  * '0xabcdef12L', whereas 'stol()' converts '0xab,0xcd,0xef,0x12' into
422  * '0xabcdef12L'.
423  *
424  * @param str a string
425  * @param len the number of bytes in str
426  * @returns str converted into a long it
427  */
428 uint32 stol(uint8 * str, int len);
429 
430 // angles from bits
431 
438 int16 angleFromBitVector(uint8 bitVector);
439 
440 
448 int16 angleFromBitVectorOffset(uint8 bitVector);
449 
450 
451 //TODO: Update comment to talk about beacons (how is this different from angle FromBItVector)
458 int16 angleFromBitVectorBeacon(uint8 bitVector);
459 
460 
468 uint8 bitsMaxContiguous(uint8 val);
469 
470 
471 // Pose math
472 
480 int32 poseAngleDiff(Pose* poseGoalPtr, Pose* posePtr);
481 
482 
491 void poseAdd(Pose* poseResPtr, Pose* pose1Ptr, Pose* pose2Ptr);
492 
493 
501 int32 poseDistance(Pose* pose1Ptr, Pose* pose2Ptr);
502 
503 // Gaussian
504 
505 /*
506  * @brief Calculates gaussian noise.
507  *
508  * Optimization errors fixed by setting roneos optimization to 0!!!!!!!
509  *
510  * @returns gaussian noise +/-GAUSSIAN_LOOKUP_MAX_RETURN centered at 0.
511  */
512 int16 gaussianNoise();
513 
514 #if 0
515 // overflow checking math
516 #define add32(v1, v2) add32func(v1, v2, __FILE__, __LINE__)
517 #define sub32(v1, v2) sub32func(v1, v2, __FILE__, __LINE__)
518 #define mul32(v1, v2) mul32func(v1, v2, __FILE__, __LINE__)
519 #define div32(v1, v2) div32func(v1, v2, __FILE__, __LINE__)
520 
521 uint8 msb32(uint32 val);
522 int32 add32func(int32 v1, int32 v2, const char * filePathName, unsigned int lineNum);
523 int32 sub32func(int32 v1, int32 v2, const char * filePathName, unsigned int lineNum);
524 int32 mul32func(int32 v1, int32 v2, const char * filePathName, unsigned int lineNum);
525 int32 div32func(int32 v1, int32 v2, const char * filePathName, unsigned int lineNum);
526 
527 #define add64(v1, v2) add64func(v1, v2, __FILE__, __LINE__)
528 #define sub64(v1, v2) sub64func(v1, v2, __FILE__, __LINE__)
529 #define mul64(v1, v2) mul64func(v1, v2, __FILE__, __LINE__)
530 #define div64(v1, v2) div64func(v1, v2, __FILE__, __LINE__)
531 
532 int64 abs64(int64 val);
533 uint8 msb64(uint64 val);
534 int64 add64func(int64 v1, int64 v2, const char * filePathName, unsigned int lineNum);
535 int64 sub64func(int64 v1, int64 v2, const char * filePathName, unsigned int lineNum);
536 int64 mul64func(int64 v1, int64 v2, const char * filePathName, unsigned int lineNum);
537 int64 div64func(int64 v1, int64 v2, const char * filePathName, unsigned int lineNum);
538 #endif
539 
540 #endif // #ifndef intMath_h
541