Revision 5d138bca
devices/DiWheelDrive/userthread.cpp | ||
---|---|---|
8 | 8 |
|
9 | 9 |
extern Global global; |
10 | 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 |
|
11 |
enum class states{ |
|
12 |
FOLLOW_LINE, |
|
13 |
OCCUPY, |
|
14 |
RELEASE, |
|
15 |
CHARGING |
|
22 | 16 |
}; |
23 | 17 |
|
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 |
// enum colorMember : uint8_t { |
|
46 |
// BLACK=0, |
|
47 |
// GREY=1, |
|
48 |
// WHITE=2 |
|
49 |
// }; |
|
18 |
// User thread state: |
|
19 |
states utState = states::FOLLOW_LINE; |
|
50 | 20 |
|
51 | 21 |
// a buffer for the z-value of the accelerometer |
52 | 22 |
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 |
|
|
23 |
bool running = false; |
|
60 | 24 |
|
61 | 25 |
int vcnl4020AmbientLight[4] = {0}; |
62 | 26 |
int vcnl4020Proximity[4] = {0}; |
63 | 27 |
|
64 |
// Border for the discrimination between black and white |
|
65 |
const int discrBlackWhite = 16000; // border in "raw sensor values" |
|
66 |
// Discrimination between black and white (returns BLACK or WHITE) |
|
67 |
// The border was calculated by a MAP-decider |
|
68 |
colorMember discrimination(int value) { |
|
69 |
if (value < discrBlackWhite) |
|
70 |
return BLACK; |
|
71 |
else |
|
72 |
return WHITE; |
|
73 |
} |
|
74 |
|
|
75 |
// Copy the speed from the source to the target array |
|
76 |
void copyRpmSpeed(const int (&source)[2], int (&target)[2]) { |
|
77 |
target[constants::DiWheelDrive::LEFT_WHEEL] = source[constants::DiWheelDrive::LEFT_WHEEL]; |
|
78 |
target[constants::DiWheelDrive::RIGHT_WHEEL] = source[constants::DiWheelDrive::RIGHT_WHEEL]; |
|
79 |
// chprintf((BaseSequentialStream*) &SD1, "Speed left: %d, Speed right: %d\r\n", target[0], target[1]); |
|
80 |
} |
|
81 |
|
|
82 | 28 |
|
83 | 29 |
// Set the speed by the array |
84 | 30 |
void setRpmSpeed(const int (&rpmSpeed)[2]) { |
85 |
global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
|
|
31 |
global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000);
|
|
86 | 32 |
} |
87 | 33 |
|
88 |
// Get the next policy rule |
|
89 |
states getNextPolicy() { |
|
90 |
// If the policy is over, start again |
|
91 |
if (policyCounter >= sizeOfPolicy) |
|
92 |
policyCounter = 3; |
|
34 |
void lightOneLed(Color color, int idx){ |
|
35 |
global.robot.setLightColor(idx, Color(color)); |
|
36 |
} |
|
93 | 37 |
|
94 |
return policy[policyCounter++]; |
|
38 |
void lightAllLeds(Color color){ |
|
39 |
int led = 0; |
|
40 |
for(led=0; led<8; led++){ |
|
41 |
lightOneLed(color, led); |
|
42 |
} |
|
95 | 43 |
} |
96 | 44 |
|
97 | 45 |
|
... | ... | |
108 | 56 |
msg_t |
109 | 57 |
UserThread::main() |
110 | 58 |
{ |
111 |
/*
|
|
112 |
* SETUP
|
|
113 |
*/
|
|
114 |
int rpmFuzzyCtrl[2] = {0};
|
|
115 |
for (uint8_t led = 0; led < 8; ++led) {
|
|
116 |
global.robot.setLightColor(led, Color(Color::BLACK));
|
|
117 |
}
|
|
118 |
running = false;
|
|
119 |
LineFollow lf(&global);
|
|
120 |
/*
|
|
121 |
* LOOP
|
|
122 |
*/
|
|
123 |
while (!this->shouldTerminate())
|
|
124 |
{
|
|
59 |
/*
|
|
60 |
* SETUP
|
|
61 |
*/
|
|
62 |
int rpmFuzzyCtrl[2] = {0};
|
|
63 |
for (uint8_t led = 0; led < 8; ++led) { |
|
64 |
global.robot.setLightColor(led, Color(Color::BLACK));
|
|
65 |
} |
|
66 |
running = false; |
|
67 |
LineFollow lf(&global);
|
|
68 |
/*
|
|
69 |
* LOOP
|
|
70 |
*/
|
|
71 |
while (!this->shouldTerminate())
|
|
72 |
{
|
|
125 | 73 |
/* |
126 | 74 |
* read accelerometer z-value |
127 | 75 |
*/ |
... | ... | |
131 | 79 |
* evaluate the accelerometer |
132 | 80 |
*/ |
133 | 81 |
|
134 |
//Features over can: (Line Following) |
|
135 |
//SetKP |
|
136 |
//SetKI |
|
137 |
//SetKd ? |
|
138 |
//SetSleepTime |
|
139 |
//SeForwardSpeed? or SetMaxSpeed |
|
140 |
//DriveOnLeftLine |
|
141 |
//DriveOnRightLine |
|
142 |
|
|
143 |
//if accel_z<-900 |
|
144 |
//send can event (one time) |
|
145 |
// Line following is started if amiro roteted |
|
146 |
if (accel_z < -900 /*-0.9g*/) { //new: (can.allowLineFollowOnTurn && accel_z < 900) || can.startLineFollow optional feature |
|
147 |
if (running) { |
|
148 |
// stop the robot |
|
149 |
running = false; |
|
150 |
global.motorcontrol.setTargetRPM(0, 0); |
|
151 |
} else { |
|
152 |
// start the robot |
|
153 |
running = true; |
|
154 |
} |
|
155 |
// set the front LEDs to blue for one second |
|
156 |
global.robot.setLightColor(constants::LightRing::LED_SSW, Color(Color::BLACK)); |
|
157 |
global.robot.setLightColor(constants::LightRing::LED_WSW, Color(Color::BLACK)); |
|
158 |
global.robot.setLightColor(constants::LightRing::LED_WNW, Color(Color::WHITE)); |
|
159 |
global.robot.setLightColor(constants::LightRing::LED_NNW, Color(Color::WHITE)); |
|
160 |
global.robot.setLightColor(constants::LightRing::LED_NNE, Color(Color::WHITE)); |
|
161 |
global.robot.setLightColor(constants::LightRing::LED_ENE, Color(Color::WHITE)); |
|
162 |
global.robot.setLightColor(constants::LightRing::LED_ESE, Color(Color::BLACK)); |
|
163 |
global.robot.setLightColor(constants::LightRing::LED_SSE, Color(Color::BLACK)); |
|
164 |
this->sleep(MS2ST(1000)); |
|
165 |
global.robot.setLightColor(constants::LightRing::LED_WNW, Color(Color::BLACK)); |
|
166 |
global.robot.setLightColor(constants::LightRing::LED_NNW, Color(Color::BLACK)); |
|
167 |
global.robot.setLightColor(constants::LightRing::LED_NNE, Color(Color::BLACK)); |
|
168 |
global.robot.setLightColor(constants::LightRing::LED_ENE, Color(Color::BLACK)); |
|
169 |
} |
|
170 |
|
|
171 |
if (running) { |
|
172 |
// Read the proximity values |
|
173 |
for (int i = 0; i < 4; i++) { |
|
174 |
vcnl4020AmbientLight[i] = global.vcnl4020[i].getAmbientLight(); |
|
175 |
vcnl4020Proximity[i] = global.vcnl4020[i].getProximityScaledWoOffset(); |
|
176 |
} |
|
177 |
|
|
178 |
|
|
179 |
// lf.stableFollow(vcnl4020Proximity, rpmFuzzyCtrl, &global); |
|
180 |
// chprintf((BaseSequentialStream*) &SD1, "0x%04X 0x%04X 0x%04X 0x%04X\n", |
|
181 |
// vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT], |
|
182 |
// vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT], |
|
183 |
// vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT], |
|
184 |
// vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT]); |
|
185 |
//if (configLineFollowing==2) |
|
186 |
// lineFollownew |
|
187 |
//else |
|
188 |
// lineFollowing(vcnl4020Proximity, rpmFuzzyCtrl, &global); |
|
189 |
lf.followLine(rpmFuzzyCtrl); |
|
190 |
setRpmSpeed(rpmFuzzyCtrl); |
|
191 |
} |
|
82 |
//Features over can: (Line Following) |
|
83 |
//SetKP |
|
84 |
//SetKI |
|
85 |
//SetKd ? |
|
86 |
//SetSleepTime |
|
87 |
//SeForwardSpeed? or SetMaxSpeed |
|
88 |
//DriveOnLeftLine |
|
89 |
//DriveOnRightLine |
|
90 |
|
|
91 |
//if accel_z<-900 |
|
92 |
//send can event (one time) |
|
93 |
// Line following is started if amiro roteted |
|
94 |
if (accel_z < -900 /*-0.9g*/) { //new: (can.allowLineFollowOnTurn && accel_z < 900) || can.startLineFollow optional feature |
|
95 |
// set the front LEDs to blue for one second |
|
96 |
lightAllLeds(Color::GREEN); |
|
97 |
this->sleep(1000); |
|
98 |
lightAllLeds(Color::BLACK); |
|
99 |
running = !running; |
|
100 |
} |
|
101 |
if(running){ |
|
102 |
|
|
103 |
} |
|
192 | 104 |
|
193 |
// this->sleep(US2ST(5)); |
|
194 |
this->sleep(CAN::UPDATE_PERIOD); |
|
195 |
} |
|
105 |
lf.followLine(rpmFuzzyCtrl); |
|
106 |
setRpmSpeed(rpmFuzzyCtrl); |
|
107 |
// this->sleep(US2ST(5)); |
|
108 |
this->sleep(CAN::UPDATE_PERIOD); |
|
109 |
} |
|
196 | 110 |
|
197 | 111 |
return RDY_OK; |
198 | 112 |
} |
Also available in: Unified diff