amiro-os / devices / DiWheelDrive / userthread.cpp @ 1b3adcdd
History | View | Annotate | Download (7.23 KB)
1 | 58fe0e0b | Thomas Schöpping | #include "userthread.hpp" |
---|---|---|---|
2 | |||
3 | #include "global.hpp" |
||
4 | |||
5 | c76baf23 | Georg Alberding | #include "linefollow2.hpp" |
6 | |||
7 | 58fe0e0b | Thomas Schöpping | using namespace amiro; |
8 | |||
9 | extern Global global;
|
||
10 | |||
11 | // State machine states
|
||
12 | enum states : uint8_t {
|
||
13 | IDLE, |
||
14 | GO_RIGHT, |
||
15 | GO_STRAIGHT, |
||
16 | PARKING, |
||
17 | PARKING_RIGHT, |
||
18 | PARKING_LEFT, |
||
19 | GO_LEFT, |
||
20 | SPINNING_PARKING, |
||
21 | SPINNING |
||
22 | }; |
||
23 | |||
24 | // Policy
|
||
25 | states policy[] = { |
||
26 | GO_STRAIGHT, |
||
27 | GO_RIGHT, |
||
28 | GO_RIGHT, |
||
29 | GO_STRAIGHT, |
||
30 | GO_RIGHT, |
||
31 | GO_STRAIGHT, |
||
32 | GO_RIGHT, |
||
33 | GO_STRAIGHT, |
||
34 | GO_STRAIGHT, |
||
35 | GO_RIGHT, |
||
36 | GO_STRAIGHT, |
||
37 | GO_RIGHT, |
||
38 | GO_STRAIGHT |
||
39 | }; |
||
40 | |||
41 | // The different classes (or members) of color discrimination
|
||
42 | // BLACK is the line itselfe
|
||
43 | // GREY is the boarder between the line and the surface
|
||
44 | // WHITE is the common surface
|
||
45 | 1b3adcdd | galberding | // enum colorMember : uint8_t {
|
46 | // BLACK=0,
|
||
47 | // GREY=1,
|
||
48 | // WHITE=2
|
||
49 | // };
|
||
50 | 58fe0e0b | Thomas Schöpping | |
51 | // a buffer for the z-value of the accelerometer
|
||
52 | int16_t accel_z; |
||
53 | bool running;
|
||
54 | |||
55 | // Get some information about the policy
|
||
56 | const int sizeOfPolicy = sizeof(policy) / sizeof(states); |
||
57 | int policyCounter = 0; // Do not change this, it points to the beginning of the policy |
||
58 | |||
59 | // Different speed settings (all values in "rounds per minute")
|
||
60 | 1b3adcdd | galberding | // const int rpmTurnLeft[2] = {-10, 10};
|
61 | // const int rpmTurnRight[2] = {rpmTurnLeft[1],rpmTurnLeft[0]};
|
||
62 | // const int rpmHalt[2] = {0, 0};
|
||
63 | 58fe0e0b | Thomas Schöpping | |
64 | // Definition of the fuzzyfication function
|
||
65 | // | Membership
|
||
66 | // 1|_B__ G __W__
|
||
67 | // | \ /\ /
|
||
68 | // | \/ \/
|
||
69 | // |_____/\__/\______ Sensor values
|
||
70 | // SEE MATLAB SCRIPT "fuzzyRule.m" for adjusting the values
|
||
71 | // All values are "raw sensor values"
|
||
72 | /* Use these values for white ground surface (e.g. paper) */
|
||
73 | |||
74 | 1b3adcdd | galberding | // const int blackStartFalling = 0x1000; // Where the black curve starts falling
|
75 | // const int blackOff = 0x1800; // Where no more black is detected
|
||
76 | // const int whiteStartRising = 0x2800; // Where the white curve starts rising
|
||
77 | // const int whiteOn = 0x6000; // Where the white curve has reached the maximum value
|
||
78 | // const int greyMax = (whiteOn + blackStartFalling) / 2; // Where grey has its maximum
|
||
79 | // const int greyStartRising = blackStartFalling; // Where grey starts rising
|
||
80 | // const int greyOff = whiteOn; // Where grey is completely off again
|
||
81 | 58fe0e0b | Thomas Schöpping | |
82 | /* Use these values for gray ground surfaces */
|
||
83 | /*
|
||
84 | const int blackStartFalling = 0x1000; // Where the black curve starts falling
|
||
85 | const int blackOff = 0x2800; // Where no more black is detected
|
||
86 | const int whiteStartRising = 0x4000; // Where the white curve starts rising
|
||
87 | const int whiteOn = 0x5000; // Where the white curve starts rising
|
||
88 | const int greyMax = (whiteOn + blackStartFalling) / 2; // Where grey has its maximum
|
||
89 | const int greyStartRising = blackStartFalling; // Where grey starts rising
|
||
90 | const int greyOff = whiteOn; // Where grey is completely off again
|
||
91 | */
|
||
92 | |||
93 | int vcnl4020AmbientLight[4] = {0}; |
||
94 | int vcnl4020Proximity[4] = {0}; |
||
95 | |||
96 | // Border for the discrimination between black and white
|
||
97 | const int discrBlackWhite = 16000; // border in "raw sensor values" |
||
98 | // Discrimination between black and white (returns BLACK or WHITE)
|
||
99 | // The border was calculated by a MAP-decider
|
||
100 | colorMember discrimination(int value) {
|
||
101 | if (value < discrBlackWhite)
|
||
102 | return BLACK;
|
||
103 | else
|
||
104 | return WHITE;
|
||
105 | } |
||
106 | |||
107 | // Copy the speed from the source to the target array
|
||
108 | void copyRpmSpeed(const int (&source)[2], int (&target)[2]) { |
||
109 | target[constants::DiWheelDrive::LEFT_WHEEL] = source[constants::DiWheelDrive::LEFT_WHEEL]; |
||
110 | target[constants::DiWheelDrive::RIGHT_WHEEL] = source[constants::DiWheelDrive::RIGHT_WHEEL]; |
||
111 | 2330e415 | Georg Alberding | // chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]);
|
112 | 58fe0e0b | Thomas Schöpping | } |
113 | |||
114 | c76baf23 | Georg Alberding | |
115 | 58fe0e0b | Thomas Schöpping | // Set the speed by the array
|
116 | void setRpmSpeed(const int (&rpmSpeed)[2]) { |
||
117 | global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000); |
||
118 | } |
||
119 | |||
120 | // Get the next policy rule
|
||
121 | states getNextPolicy() { |
||
122 | // If the policy is over, start again
|
||
123 | if (policyCounter >= sizeOfPolicy)
|
||
124 | policyCounter = 3;
|
||
125 | |||
126 | return policy[policyCounter++];
|
||
127 | } |
||
128 | |||
129 | |||
130 | |||
131 | UserThread::UserThread() : |
||
132 | chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>() |
||
133 | { |
||
134 | } |
||
135 | |||
136 | UserThread::~UserThread() |
||
137 | { |
||
138 | } |
||
139 | |||
140 | msg_t |
||
141 | UserThread::main() |
||
142 | { |
||
143 | /*
|
||
144 | * SETUP
|
||
145 | */
|
||
146 | int rpmFuzzyCtrl[2] = {0}; |
||
147 | for (uint8_t led = 0; led < 8; ++led) { |
||
148 | global.robot.setLightColor(led, Color(Color::BLACK)); |
||
149 | } |
||
150 | running = false;
|
||
151 | 12463563 | galberding | LineFollow lf(&global); |
152 | 58fe0e0b | Thomas Schöpping | /*
|
153 | * LOOP
|
||
154 | */
|
||
155 | while (!this->shouldTerminate()) |
||
156 | { |
||
157 | /*
|
||
158 | * read accelerometer z-value
|
||
159 | */
|
||
160 | accel_z = global.lis331dlh.getAccelerationForce(LIS331DLH::AXIS_Z); |
||
161 | |||
162 | /*
|
||
163 | * evaluate the accelerometer
|
||
164 | */
|
||
165 | c76baf23 | Georg Alberding | |
166 | //Features over can: (Line Following)
|
||
167 | //SetKP
|
||
168 | //SetKI
|
||
169 | //SetKd ?
|
||
170 | //SetSleepTime
|
||
171 | //SeForwardSpeed? or SetMaxSpeed
|
||
172 | //DriveOnLeftLine
|
||
173 | //DriveOnRightLine
|
||
174 | |||
175 | //if accel_z<-900
|
||
176 | //send can event (one time)
|
||
177 | // Line following is started if amiro roteted
|
||
178 | if (accel_z < -900 /*-0.9g*/) { //new: (can.allowLineFollowOnTurn && accel_z < 900) || can.startLineFollow optional feature |
||
179 | 58fe0e0b | Thomas Schöpping | if (running) {
|
180 | // stop the robot
|
||
181 | running = false;
|
||
182 | global.motorcontrol.setTargetRPM(0, 0); |
||
183 | } else {
|
||
184 | // start the robot
|
||
185 | running = true;
|
||
186 | } |
||
187 | // set the front LEDs to blue for one second
|
||
188 | global.robot.setLightColor(constants::LightRing::LED_SSW, Color(Color::BLACK)); |
||
189 | global.robot.setLightColor(constants::LightRing::LED_WSW, Color(Color::BLACK)); |
||
190 | global.robot.setLightColor(constants::LightRing::LED_WNW, Color(Color::WHITE)); |
||
191 | global.robot.setLightColor(constants::LightRing::LED_NNW, Color(Color::WHITE)); |
||
192 | global.robot.setLightColor(constants::LightRing::LED_NNE, Color(Color::WHITE)); |
||
193 | global.robot.setLightColor(constants::LightRing::LED_ENE, Color(Color::WHITE)); |
||
194 | global.robot.setLightColor(constants::LightRing::LED_ESE, Color(Color::BLACK)); |
||
195 | global.robot.setLightColor(constants::LightRing::LED_SSE, Color(Color::BLACK)); |
||
196 | this->sleep(MS2ST(1000)); |
||
197 | global.robot.setLightColor(constants::LightRing::LED_WNW, Color(Color::BLACK)); |
||
198 | global.robot.setLightColor(constants::LightRing::LED_NNW, Color(Color::BLACK)); |
||
199 | global.robot.setLightColor(constants::LightRing::LED_NNE, Color(Color::BLACK)); |
||
200 | global.robot.setLightColor(constants::LightRing::LED_ENE, Color(Color::BLACK)); |
||
201 | } |
||
202 | |||
203 | if (running) {
|
||
204 | // Read the proximity values
|
||
205 | for (int i = 0; i < 4; i++) { |
||
206 | vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight(); |
||
207 | vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset(); |
||
208 | } |
||
209 | 1b3adcdd | galberding | // lf.stableFollow(vcnl4020Proximity, rpmFuzzyCtrl, &global);
|
210 | c76baf23 | Georg Alberding | // chprintf((BaseSequentialStream*) &SD1, "0x%04X 0x%04X 0x%04X 0x%04X\n",
|
211 | // vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT],
|
||
212 | // vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],
|
||
213 | // vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT],
|
||
214 | // vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT]);
|
||
215 | //if (configLineFollowing==2)
|
||
216 | // lineFollownew
|
||
217 | //else
|
||
218 | 2330e415 | Georg Alberding | // lineFollowing(vcnl4020Proximity, rpmFuzzyCtrl, &global);
|
219 | af93a91c | galberding | setRpmSpeed(rpmFuzzyCtrl); |
220 | 58fe0e0b | Thomas Schöpping | } |
221 | |||
222 | 2330e415 | Georg Alberding | // this->sleep(US2ST(5));
|
223 | 8dbafe16 | Thomas Schöpping | this->sleep(CAN::UPDATE_PERIOD);
|
224 | 58fe0e0b | Thomas Schöpping | } |
225 | |||
226 | return RDY_OK;
|
||
227 | } |