amiro-os / devices / DiWheelDrive / userthread.cpp @ bfffb0bd
History | View | Annotate | Download (5.663 KB)
1 |
#include "userthread.hpp" |
---|---|
2 |
|
3 |
#include "global.hpp" |
4 |
|
5 |
#include "linefollow2.hpp" |
6 |
|
7 |
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 |
// enum colorMember : uint8_t {
|
46 |
// BLACK=0,
|
47 |
// GREY=1,
|
48 |
// WHITE=2
|
49 |
// };
|
50 |
|
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 |
|
60 |
|
61 |
int vcnl4020AmbientLight[4] = {0}; |
62 |
int vcnl4020Proximity[4] = {0}; |
63 |
|
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 |
|
83 |
// Set the speed by the array
|
84 |
void setRpmSpeed(const int (&rpmSpeed)[2]) { |
85 |
global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000); |
86 |
} |
87 |
|
88 |
// Get the next policy rule
|
89 |
states getNextPolicy() { |
90 |
// If the policy is over, start again
|
91 |
if (policyCounter >= sizeOfPolicy)
|
92 |
policyCounter = 3;
|
93 |
|
94 |
return policy[policyCounter++];
|
95 |
} |
96 |
|
97 |
|
98 |
|
99 |
UserThread::UserThread() : |
100 |
chibios_rt::BaseStaticThread<USER_THREAD_STACK_SIZE>() |
101 |
{ |
102 |
} |
103 |
|
104 |
UserThread::~UserThread() |
105 |
{ |
106 |
} |
107 |
|
108 |
msg_t |
109 |
UserThread::main() |
110 |
{ |
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 |
{ |
125 |
/*
|
126 |
* read accelerometer z-value
|
127 |
*/
|
128 |
accel_z = global.lis331dlh.getAccelerationForce(LIS331DLH::AXIS_Z); |
129 |
|
130 |
/*
|
131 |
* evaluate the accelerometer
|
132 |
*/
|
133 |
|
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 |
} |
192 |
|
193 |
// this->sleep(US2ST(5));
|
194 |
this->sleep(CAN::UPDATE_PERIOD);
|
195 |
} |
196 |
|
197 |
return RDY_OK;
|
198 |
} |