Program that combines a linefollower program with visible ligt communication.
Dependencies: m3pi_custom mbed
linefollower.h@1:243ec35fafcd, 2018-05-16 (annotated)
- Committer:
- bertgereels
- Date:
- Wed May 16 19:12:20 2018 +0000
- Revision:
- 1:243ec35fafcd
- Parent:
- 0:1f5782fc5ca3
Part two of the 'projectlab' course.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bertgereels | 0:1f5782fc5ca3 | 1 | #include "mbed.h" |
bertgereels | 0:1f5782fc5ca3 | 2 | #include "m3pi.h" |
bertgereels | 0:1f5782fc5ca3 | 3 | #include "manchester.h" |
bertgereels | 1:243ec35fafcd | 4 | #include "analoog.h" |
bertgereels | 0:1f5782fc5ca3 | 5 | |
bertgereels | 0:1f5782fc5ca3 | 6 | #pragma once |
bertgereels | 0:1f5782fc5ca3 | 7 | |
bertgereels | 0:1f5782fc5ca3 | 8 | // Minimum and maximum motor speeds |
bertgereels | 1:243ec35fafcd | 9 | #define MAX_SPEED 0.22 |
bertgereels | 0:1f5782fc5ca3 | 10 | #define MIN_SPEED 0 |
bertgereels | 0:1f5782fc5ca3 | 11 | |
bertgereels | 0:1f5782fc5ca3 | 12 | // PID terms |
bertgereels | 0:1f5782fc5ca3 | 13 | #define P_TERM 1 |
bertgereels | 0:1f5782fc5ca3 | 14 | #define I_TERM 0 |
bertgereels | 0:1f5782fc5ca3 | 15 | #define D_TERM 10 |
bertgereels | 0:1f5782fc5ca3 | 16 | |
bertgereels | 0:1f5782fc5ca3 | 17 | namespace ProjectTwo{ |
bertgereels | 0:1f5782fc5ca3 | 18 | |
bertgereels | 0:1f5782fc5ca3 | 19 | class LineFollower{ |
bertgereels | 0:1f5782fc5ca3 | 20 | public: |
bertgereels | 1:243ec35fafcd | 21 | /* |
bertgereels | 1:243ec35fafcd | 22 | * Constructor for LineFollower class. |
bertgereels | 1:243ec35fafcd | 23 | * |
bertgereels | 1:243ec35fafcd | 24 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 25 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 26 | */ |
bertgereels | 0:1f5782fc5ca3 | 27 | LineFollower(void); |
bertgereels | 1:243ec35fafcd | 28 | |
bertgereels | 1:243ec35fafcd | 29 | /* |
bertgereels | 1:243ec35fafcd | 30 | * Most important method of this class, contains the state machine. |
bertgereels | 1:243ec35fafcd | 31 | * |
bertgereels | 1:243ec35fafcd | 32 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 33 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 34 | */ |
bertgereels | 0:1f5782fc5ca3 | 35 | void followTheLine(void); |
bertgereels | 0:1f5782fc5ca3 | 36 | private: |
bertgereels | 1:243ec35fafcd | 37 | |
bertgereels | 0:1f5782fc5ca3 | 38 | m3pi m3pi; |
bertgereels | 1:243ec35fafcd | 39 | Manchester vlcDecoder; |
bertgereels | 1:243ec35fafcd | 40 | AnalogReader analogSensor; |
bertgereels | 0:1f5782fc5ca3 | 41 | |
bertgereels | 0:1f5782fc5ca3 | 42 | enum lineFollowerStates{ |
bertgereels | 0:1f5782fc5ca3 | 43 | STATE_INIT, |
bertgereels | 0:1f5782fc5ca3 | 44 | STATE_COMPUTE, |
bertgereels | 0:1f5782fc5ca3 | 45 | STATE_FORWARD, |
bertgereels | 1:243ec35fafcd | 46 | STATE_LONG_FORWARD, |
bertgereels | 0:1f5782fc5ca3 | 47 | STATE_LEFT, |
bertgereels | 0:1f5782fc5ca3 | 48 | STATE_RIGHT, |
bertgereels | 1:243ec35fafcd | 49 | STATE_TURN180, |
bertgereels | 0:1f5782fc5ca3 | 50 | STATE_STOP, |
bertgereels | 0:1f5782fc5ca3 | 51 | STATE_VLC |
bertgereels | 0:1f5782fc5ca3 | 52 | }; |
bertgereels | 0:1f5782fc5ca3 | 53 | lineFollowerStates CurrentLineFollowerState; |
bertgereels | 0:1f5782fc5ca3 | 54 | |
bertgereels | 1:243ec35fafcd | 55 | /* |
bertgereels | 1:243ec35fafcd | 56 | * Method that computes the derivative. |
bertgereels | 1:243ec35fafcd | 57 | * |
bertgereels | 1:243ec35fafcd | 58 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 59 | @return The derivate as a float. |
bertgereels | 1:243ec35fafcd | 60 | */ |
bertgereels | 0:1f5782fc5ca3 | 61 | float computeDerivative(void); |
bertgereels | 1:243ec35fafcd | 62 | |
bertgereels | 1:243ec35fafcd | 63 | /* |
bertgereels | 1:243ec35fafcd | 64 | * Method that computes the integral. |
bertgereels | 1:243ec35fafcd | 65 | * |
bertgereels | 1:243ec35fafcd | 66 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 67 | @return The integral as a float. |
bertgereels | 1:243ec35fafcd | 68 | */ |
bertgereels | 0:1f5782fc5ca3 | 69 | float computeIntegral(void); |
bertgereels | 1:243ec35fafcd | 70 | |
bertgereels | 1:243ec35fafcd | 71 | /* |
bertgereels | 1:243ec35fafcd | 72 | * Method that computes the power. |
bertgereels | 1:243ec35fafcd | 73 | * |
bertgereels | 1:243ec35fafcd | 74 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 75 | @return The power as a float. |
bertgereels | 1:243ec35fafcd | 76 | */ |
bertgereels | 0:1f5782fc5ca3 | 77 | float computePower(void); |
bertgereels | 1:243ec35fafcd | 78 | |
bertgereels | 1:243ec35fafcd | 79 | /* |
bertgereels | 1:243ec35fafcd | 80 | * Method that computes the new speeds for the robot, after the calculations. |
bertgereels | 1:243ec35fafcd | 81 | * |
bertgereels | 1:243ec35fafcd | 82 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 83 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 84 | */ |
bertgereels | 0:1f5782fc5ca3 | 85 | void computeNewSpeeds(void); |
bertgereels | 1:243ec35fafcd | 86 | |
bertgereels | 1:243ec35fafcd | 87 | /* |
bertgereels | 1:243ec35fafcd | 88 | * Method that checks the limits of the robot. |
bertgereels | 1:243ec35fafcd | 89 | * |
bertgereels | 1:243ec35fafcd | 90 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 91 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 92 | */ |
bertgereels | 0:1f5782fc5ca3 | 93 | void limitChecks(void); |
bertgereels | 0:1f5782fc5ca3 | 94 | |
bertgereels | 1:243ec35fafcd | 95 | /* |
bertgereels | 1:243ec35fafcd | 96 | * Method that makes the robot turn left 90 degrees. |
bertgereels | 1:243ec35fafcd | 97 | * |
bertgereels | 1:243ec35fafcd | 98 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 99 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 100 | */ |
bertgereels | 0:1f5782fc5ca3 | 101 | void turnLeft90(void); |
bertgereels | 1:243ec35fafcd | 102 | |
bertgereels | 1:243ec35fafcd | 103 | /* |
bertgereels | 1:243ec35fafcd | 104 | * Method that makes the robot turn right 90 degrees. |
bertgereels | 1:243ec35fafcd | 105 | * |
bertgereels | 1:243ec35fafcd | 106 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 107 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 108 | */ |
bertgereels | 0:1f5782fc5ca3 | 109 | void turnRight90(void); |
bertgereels | 1:243ec35fafcd | 110 | |
bertgereels | 1:243ec35fafcd | 111 | /* |
bertgereels | 1:243ec35fafcd | 112 | * Method that makes the robot go backward. |
bertgereels | 1:243ec35fafcd | 113 | * |
bertgereels | 1:243ec35fafcd | 114 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 115 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 116 | */ |
bertgereels | 0:1f5782fc5ca3 | 117 | void goBackwards(void); |
bertgereels | 1:243ec35fafcd | 118 | |
bertgereels | 1:243ec35fafcd | 119 | /* |
bertgereels | 1:243ec35fafcd | 120 | * Method that makes the robot go forward. |
bertgereels | 1:243ec35fafcd | 121 | * |
bertgereels | 1:243ec35fafcd | 122 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 123 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 124 | */ |
bertgereels | 0:1f5782fc5ca3 | 125 | void goForwards(void); |
bertgereels | 0:1f5782fc5ca3 | 126 | |
bertgereels | 1:243ec35fafcd | 127 | /* |
bertgereels | 1:243ec35fafcd | 128 | * Method that makes the robot 'jiggle'. |
bertgereels | 1:243ec35fafcd | 129 | * The purpose of this is to make the robot search the area for a LED. |
bertgereels | 1:243ec35fafcd | 130 | * |
bertgereels | 1:243ec35fafcd | 131 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 132 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 133 | */ |
bertgereels | 1:243ec35fafcd | 134 | void Scan(void); |
bertgereels | 1:243ec35fafcd | 135 | |
bertgereels | 1:243ec35fafcd | 136 | /* |
bertgereels | 1:243ec35fafcd | 137 | * Method that uses the 2 leftmost and 2 rightmost IR sensors |
bertgereels | 1:243ec35fafcd | 138 | * to determine if an intersection is ahead. |
bertgereels | 1:243ec35fafcd | 139 | * |
bertgereels | 1:243ec35fafcd | 140 | @param Nothing. |
bertgereels | 1:243ec35fafcd | 141 | @return Nothing. |
bertgereels | 1:243ec35fafcd | 142 | */ |
bertgereels | 1:243ec35fafcd | 143 | bool checkForIntersection(void); |
bertgereels | 0:1f5782fc5ca3 | 144 | |
bertgereels | 0:1f5782fc5ca3 | 145 | float right; |
bertgereels | 0:1f5782fc5ca3 | 146 | float left; |
bertgereels | 0:1f5782fc5ca3 | 147 | float current_pos_of_line; |
bertgereels | 0:1f5782fc5ca3 | 148 | float previous_pos_of_line; |
bertgereels | 0:1f5782fc5ca3 | 149 | float derivative,proportional,integral; |
bertgereels | 0:1f5782fc5ca3 | 150 | float power; |
bertgereels | 0:1f5782fc5ca3 | 151 | float speed; |
bertgereels | 0:1f5782fc5ca3 | 152 | }; |
bertgereels | 0:1f5782fc5ca3 | 153 | }; |