EE149
/
FinalProject
Final Project files for mBed development.
main.h@46:a3b9c0fe8f36, 2014-12-19 (annotated)
- Committer:
- lsaristo
- Date:
- Fri Dec 19 03:47:18 2014 +0000
- Revision:
- 46:a3b9c0fe8f36
- Parent:
- 40:0199bad6c979
Changed postscript file name to control.ps instead.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lsaristo | 8:12d780f7443e | 1 | /** |
lsaristo | 10:94b068b2ce1d | 2 | * @file main.h |
lsaristo | 35:a1c14c6d9282 | 3 | * @brief Main header file for includes and defs |
lsaristo | 8:12d780f7443e | 4 | * @author John Wilkey |
lsaristo | 29:459ff10d2a07 | 5 | * @author Alec Guertin |
lsaristo | 29:459ff10d2a07 | 6 | * @author Chester Chu |
lsaristo | 8:12d780f7443e | 7 | */ |
lsaristo | 8:12d780f7443e | 8 | |
lsaristo | 10:94b068b2ce1d | 9 | #ifndef _MAIN_H |
lsaristo | 10:94b068b2ce1d | 10 | #define _MAIN_H |
lsaristo | 8:12d780f7443e | 11 | |
lsaristo | 8:12d780f7443e | 12 | #include "mbed.h" |
lsaristo | 8:12d780f7443e | 13 | #include "m3pi.h" |
lsaristo | 9:3a0433c391cb | 14 | #include <string.h> |
lsaristo | 9:3a0433c391cb | 15 | #include <stdarg.h> |
lsaristo | 9:3a0433c391cb | 16 | #include <stdio.h> |
lsaristo | 21:0c80a5d89ea3 | 17 | #include <math.h> |
lsaristo | 35:a1c14c6d9282 | 18 | |
alecguertin | 40:0199bad6c979 | 19 | /* Constants used in main.c */ |
alecguertin | 40:0199bad6c979 | 20 | #define TURN_SPEED 0.15 /**< Motor power for turning */ |
alecguertin | 40:0199bad6c979 | 21 | #define DRIVE_SPEED 0.25 /**< Motor power for drawing/moving */ |
alecguertin | 40:0199bad6c979 | 22 | #define TIME_FACT 1780 /**< Multiplier for forward() and backward() */ |
alecguertin | 40:0199bad6c979 | 23 | #define CAL_SPEED 0.25 /**< Drive speed during calibration */ |
alecguertin | 40:0199bad6c979 | 24 | #define CLOSE_ENOUGH 0.0008 /**< Threshold for calibration line centering */ |
alecguertin | 40:0199bad6c979 | 25 | #define WIGGLE_MAX 30 /**< Max 'wiggles' during calibration */ |
alecguertin | 40:0199bad6c979 | 26 | #define CORNER_THRESHOLD 0.3 /**< Threshold for checking if line position denotes a corner */ |
alecguertin | 40:0199bad6c979 | 27 | #define INST_BUF_SIZE 250 /**< Size of input buffer for instructions */ |
alecguertin | 40:0199bad6c979 | 28 | #define CORRECTION_SPEED 0.2*DRIVE_SPEED /**< Amount to change speed of one wheel when following line */ |
alecguertin | 40:0199bad6c979 | 29 | #define CORRECTION_THRESHOLD 0.05 /**< Maximum tolerable deviation from line when following line */ |
alecguertin | 40:0199bad6c979 | 30 | #define RAD_TO_DEG 57.29 /**< Factor to convert radians to degrees */ |
alecguertin | 40:0199bad6c979 | 31 | #define FULL_TURN 360 /**< Degrees for a full turn */ |
alecguertin | 40:0199bad6c979 | 32 | #define HALF_TURN 180 /**< Degrees for a half turn */ |
alecguertin | 40:0199bad6c979 | 33 | #define QUARTER_TURN 90 /**< Degrees for a quarter (right angle) turn */ |
alecguertin | 40:0199bad6c979 | 34 | #define CAL_FACTOR 1.1 /**< Scaling factor for time to drive */ |
alecguertin | 40:0199bad6c979 | 35 | #define DEGREE_CORRECTION 0 /**< Amount (in degrees) added to the angle for each turn */ |
alecguertin | 40:0199bad6c979 | 36 | #define SEC_TO_MSEC 1000 /**< Scaling factor to convert seconds to milliseconds */ |
alecguertin | 40:0199bad6c979 | 37 | #define SEC_TO_USEC 1000000 /**< Scaling factor to convert seconds to microseconds */ |
alecguertin | 40:0199bad6c979 | 38 | #define MSEC_TO_USEC 1000 /**< Scaling factor to convert milliseconds to microseconds */ |
alecguertin | 18:eab7b0e89398 | 39 | |
lsaristo | 8:12d780f7443e | 40 | /** |
alecguertin | 17:c72c092fcdf7 | 41 | * @brief get values of next PostScript instruction. |
alecguertin | 17:c72c092fcdf7 | 42 | * |
alecguertin | 17:c72c092fcdf7 | 43 | * @param buf Buffer with PS instructions. |
alecguertin | 17:c72c092fcdf7 | 44 | * @param x Pointer to storage for x coordinate. |
alecguertin | 17:c72c092fcdf7 | 45 | * @param y Pointer to storage for y coordinate. |
alecguertin | 17:c72c092fcdf7 | 46 | * @param draw Pointer to storage for draw/move boolean. |
alecguertin | 17:c72c092fcdf7 | 47 | * |
alecguertin | 17:c72c092fcdf7 | 48 | * @return Success or failure code. |
alecguertin | 17:c72c092fcdf7 | 49 | */ |
alecguertin | 17:c72c092fcdf7 | 50 | int retrieve_inst(char *buf, int *x, int *y, int *draw); |
alecguertin | 17:c72c092fcdf7 | 51 | |
alecguertin | 17:c72c092fcdf7 | 52 | /** |
lsaristo | 29:459ff10d2a07 | 53 | * @brief Driver forward for a time. |
lsaristo | 8:12d780f7443e | 54 | * |
lsaristo | 29:459ff10d2a07 | 55 | * @param[in] amt Amount to drive forward. In milliseconds. |
lsaristo | 8:12d780f7443e | 56 | */ |
lsaristo | 29:459ff10d2a07 | 57 | void forward(int amt); |
lsaristo | 8:12d780f7443e | 58 | |
lsaristo | 8:12d780f7443e | 59 | /** |
lsaristo | 29:459ff10d2a07 | 60 | * @brief Drive backward for a time. |
lsaristo | 8:12d780f7443e | 61 | * |
lsaristo | 29:459ff10d2a07 | 62 | * @param[in] amt Amount to drive backward. In milliseconds. |
lsaristo | 8:12d780f7443e | 63 | */ |
lsaristo | 29:459ff10d2a07 | 64 | void backward(int amt); |
lsaristo | 8:12d780f7443e | 65 | |
lsaristo | 8:12d780f7443e | 66 | /** |
lsaristo | 29:459ff10d2a07 | 67 | * @brief Turn right by some angle. |
lsaristo | 8:12d780f7443e | 68 | * |
lsaristo | 29:459ff10d2a07 | 69 | * @param[in] deg Desired final turn angle in degrees from start. |
lsaristo | 29:459ff10d2a07 | 70 | * Note that a negative angle will turn in the opposite |
lsaristo | 29:459ff10d2a07 | 71 | * direction. |
lsaristo | 8:12d780f7443e | 72 | */ |
lsaristo | 29:459ff10d2a07 | 73 | void right(float deg); |
lsaristo | 8:12d780f7443e | 74 | |
lsaristo | 8:12d780f7443e | 75 | /** |
lsaristo | 29:459ff10d2a07 | 76 | * @brief Turn left by some angle. |
lsaristo | 8:12d780f7443e | 77 | * |
lsaristo | 29:459ff10d2a07 | 78 | * @param[in] deg Desired final turn angle in degrees from start. |
lsaristo | 29:459ff10d2a07 | 79 | * Note that a negative angle will turn in the opposite |
lsaristo | 29:459ff10d2a07 | 80 | * direction. |
lsaristo | 8:12d780f7443e | 81 | */ |
lsaristo | 29:459ff10d2a07 | 82 | void left (float deg); |
lsaristo | 8:12d780f7443e | 83 | |
lsaristo | 21:0c80a5d89ea3 | 84 | /** |
lsaristo | 29:459ff10d2a07 | 85 | * @brief Wait for a number of seconds, possibly fractional. |
lsaristo | 29:459ff10d2a07 | 86 | * |
lsaristo | 29:459ff10d2a07 | 87 | * Timer resolution is on the order of microseconds. A negative wait |
lsaristo | 29:459ff10d2a07 | 88 | * time does nothing. |
lsaristo | 21:0c80a5d89ea3 | 89 | * |
lsaristo | 29:459ff10d2a07 | 90 | * @param[in] amt Time to wait, in seconds. |
lsaristo | 21:0c80a5d89ea3 | 91 | */ |
lsaristo | 29:459ff10d2a07 | 92 | void timerWait(float amt); |
alecguertin | 39:cc8691700d2a | 93 | |
alecguertin | 40:0199bad6c979 | 94 | /** |
alecguertin | 40:0199bad6c979 | 95 | * @brief Follows the non-reflective line to a corner |
alecguertin | 40:0199bad6c979 | 96 | * |
alecguertin | 40:0199bad6c979 | 97 | * @param[in] left boolean specifying if the corner to look for |
alecguertin | 40:0199bad6c979 | 98 | * is a left corner |
alecguertin | 40:0199bad6c979 | 99 | * |
alecguertin | 40:0199bad6c979 | 100 | * @return returns an error code |
alecguertin | 40:0199bad6c979 | 101 | */ |
alecguertin | 39:cc8691700d2a | 102 | int find_corner(int left); |
alecguertin | 39:cc8691700d2a | 103 | |
alecguertin | 40:0199bad6c979 | 104 | /** |
alecguertin | 40:0199bad6c979 | 105 | * @brief Turns towards the base line and moves to reach it |
alecguertin | 40:0199bad6c979 | 106 | */ |
alecguertin | 39:cc8691700d2a | 107 | void find_line(); |
alecguertin | 39:cc8691700d2a | 108 | |
alecguertin | 40:0199bad6c979 | 109 | /** |
alecguertin | 40:0199bad6c979 | 110 | * @brief Computes the Euclidean distance between two points |
alecguertin | 40:0199bad6c979 | 111 | * |
alecguertin | 40:0199bad6c979 | 112 | * @param[in] x1 the x-coordinate of the first point |
alecguertin | 40:0199bad6c979 | 113 | * @param[in] y1 the y-coordinate of the first point |
alecguertin | 40:0199bad6c979 | 114 | * @param[in] x2 the x-coordinate of the second point |
alecguertin | 40:0199bad6c979 | 115 | * @param[in] y2 the y-coordinate of the second point |
alecguertin | 40:0199bad6c979 | 116 | * |
alecguertin | 40:0199bad6c979 | 117 | * @return the distance between points (x1,y1) and (x2,y2) |
alecguertin | 40:0199bad6c979 | 118 | */ |
alecguertin | 39:cc8691700d2a | 119 | float distance(int x1, int y1, int x2, int y2); |
alecguertin | 39:cc8691700d2a | 120 | |
alecguertin | 40:0199bad6c979 | 121 | /** |
alecguertin | 40:0199bad6c979 | 122 | * @brief prints to the m3pi's screen |
alecguertin | 40:0199bad6c979 | 123 | * |
alecguertin | 40:0199bad6c979 | 124 | * @param[in] line the line on the screen to print to, in {0,1} |
alecguertin | 40:0199bad6c979 | 125 | * @param[in] format the format string to print |
alecguertin | 40:0199bad6c979 | 126 | * @param[in] ... the arguments to pass into the format string |
alecguertin | 40:0199bad6c979 | 127 | */ |
alecguertin | 39:cc8691700d2a | 128 | void robot_printf(int line, const char *format, ...); |
alecguertin | 39:cc8691700d2a | 129 | |
alecguertin | 40:0199bad6c979 | 130 | /** |
alecguertin | 40:0199bad6c979 | 131 | * @brief computes the angle to turn to face towards (x,y) |
alecguertin | 40:0199bad6c979 | 132 | * |
alecguertin | 40:0199bad6c979 | 133 | * @param[in] last_x the current x-coordinate |
alecguertin | 40:0199bad6c979 | 134 | * @param[in] last_y the current y-coordinate |
alecguertin | 40:0199bad6c979 | 135 | * @param[in] x the desired x-coordinate |
alecguertin | 40:0199bad6c979 | 136 | * @param[in] y the desired y-coordinate |
alecguertin | 40:0199bad6c979 | 137 | * @param[in] angle the current angle the m3pi is facing |
alecguertin | 40:0199bad6c979 | 138 | * |
alecguertin | 40:0199bad6c979 | 139 | * @return the angle to turn left |
alecguertin | 40:0199bad6c979 | 140 | */ |
alecguertin | 39:cc8691700d2a | 141 | float compute_turn_angle(int last_x, int last_y, int x, int y, float angle); |
alecguertin | 40:0199bad6c979 | 142 | |
alecguertin | 40:0199bad6c979 | 143 | /** |
alecguertin | 40:0199bad6c979 | 144 | * @brief sends the signal on the output pin for the led |
alecguertin | 40:0199bad6c979 | 145 | * to turn it on |
alecguertin | 40:0199bad6c979 | 146 | */ |
alecguertin | 40:0199bad6c979 | 147 | void pen_up(); |
alecguertin | 40:0199bad6c979 | 148 | |
alecguertin | 40:0199bad6c979 | 149 | /** |
alecguertin | 40:0199bad6c979 | 150 | * @brief turns off the signal to the led |
alecguertin | 40:0199bad6c979 | 151 | */ |
alecguertin | 40:0199bad6c979 | 152 | void pen_down(); |
alecguertin | 40:0199bad6c979 | 153 | |
lsaristo | 29:459ff10d2a07 | 154 | #endif |