Revision 9295436d
ID | 9295436d4707250ea93f322a6e4575b5d2a4610a |
The lineDrive function from the colony 3 bots that has been modded to be compatable with c++ and also the motors commands were switched to handle the scouts library. Note: If it fails to compile, I probably did the motors wrong. Love James.
scout/libscout/src/behaviors/lineDrive.cpp | ||
---|---|---|
1 |
/** |
|
2 |
* @file lineDrive.cpp |
|
3 |
* |
|
4 |
* Provides functions to implement line driving behavior. This program extends |
|
5 |
* the behavior of the line-following program by following lines automatically |
|
6 |
* and implementing behaviors to deal with commands passed to lineDrive. |
|
7 |
* |
|
8 |
* Specific implementation for the colony scout robots in a warehouse behaviour |
|
9 |
* setting. |
|
10 |
* |
|
11 |
* @author James Carroll(キヤロル) |
|
12 |
* Cannabalized from Dan Jacobs code. |
|
13 |
* @date 2-6-2012 |
|
14 |
*/ |
|
15 |
|
|
16 |
#include "lineDrive.h" |
|
17 |
|
|
18 |
int state[5]; //! Stores a queue of sub-commands to be executed |
|
19 |
int stateCounter; |
|
20 |
int stateLength; |
|
21 |
|
|
22 |
//! Whether lineDrive is currently paused. Set to 0 on initialization. |
|
23 |
int stopped=1; |
|
24 |
|
|
25 |
|
|
26 |
/** |
|
27 |
* Starts the line following procedure. Must be called before other |
|
28 |
* line-following functions will work. This function essentially resets the |
|
29 |
* state of line-following. |
|
30 |
*/ |
|
31 |
void lineDrive_init() |
|
32 |
{ |
|
33 |
lineFollow_init(); |
|
34 |
for(int i=0; i<5; i++)state[i]=0; |
|
35 |
stateCounter=0; |
|
36 |
stateLength=0; |
|
37 |
stopped=0; |
|
38 |
} |
|
39 |
|
|
40 |
|
|
41 |
/** |
|
42 |
* Follows a line and executes whatever command is next on the queue. |
|
43 |
* @param speed The speed with which to drive along the line. |
|
44 |
*/ |
|
45 |
int doDrive(int speed) |
|
46 |
{ |
|
47 |
if(stopped) |
|
48 |
{ |
|
49 |
motors->set_sides(0,0,ABSOLUTE); |
|
50 |
return NORMAL; |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
int code; |
|
55 |
switch(state[0]) |
|
56 |
{ |
|
57 |
case ISTRAIGHT: |
|
58 |
code = lineFollow(speed); |
|
59 |
if(code==INTERSECTION) |
|
60 |
{ |
|
61 |
for(int i=0; i<4; i++) state[i]=state[i+1]; |
|
62 |
state[4]=0; |
|
63 |
if(state[0]==0)stateCounter++; |
|
64 |
break; |
|
65 |
} |
|
66 |
else if(code==NOBARCODE) return NORMAL; |
|
67 |
return code; |
|
68 |
|
|
69 |
|
|
70 |
case ILEFT: |
|
71 |
code = turnLeft(); |
|
72 |
if(code==0) |
|
73 |
{ |
|
74 |
state[0]=0; |
|
75 |
stateCounter++; |
|
76 |
} |
|
77 |
break; |
|
78 |
|
|
79 |
case IRIGHT: |
|
80 |
code = turnRight(); |
|
81 |
if(code==0) |
|
82 |
{ |
|
83 |
state[0]=0; |
|
84 |
stateCounter++; |
|
85 |
} |
|
86 |
break; |
|
87 |
|
|
88 |
|
|
89 |
default: |
|
90 |
return LOST; |
|
91 |
|
|
92 |
} |
|
93 |
|
|
94 |
if(stateCounter>=stateLength) |
|
95 |
{ |
|
96 |
stateCounter=stateLength=0; |
|
97 |
return FINISHED; |
|
98 |
} |
|
99 |
return NORMAL; |
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
/** Starts the line-drive process if paused. */ |
|
104 |
void start(void){ |
|
105 |
stopped=0; |
|
106 |
} |
|
107 |
|
|
108 |
/** Pauses the line-drive process. Default is started. */ |
|
109 |
void stop(void){ |
|
110 |
stopped=1; |
|
111 |
} |
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
/** |
|
116 |
* Executes an intersection turn where the intersection type is specified by the |
|
117 |
* parameters. |
|
118 |
* @param type A valid defined intersection type |
|
119 |
* @param dir The direction to turn at the intersection |
|
120 |
*/ |
|
121 |
int turn(int type, int dir) |
|
122 |
{ |
|
123 |
if(stateLength!=0) |
|
124 |
return ERROR; |
|
125 |
|
|
126 |
//turns that don't depend on what kind of road your on |
|
127 |
if(dir==ISTRAIGHT) |
|
128 |
{ |
|
129 |
stateLength++; |
|
130 |
state[1]=ISTRAIGHT; |
|
131 |
return NORMAL; |
|
132 |
} |
|
133 |
if(dir==IUTURN) |
|
134 |
{ |
|
135 |
stateLength+=2; |
|
136 |
state[1]=state[2]=ILEFT; |
|
137 |
return NORMAL; |
|
138 |
} |
|
139 |
|
|
140 |
if(type==ON_ROAD) |
|
141 |
{ |
|
142 |
if(dir==IRIGHT){ |
|
143 |
stateLength++; |
|
144 |
state[1] = IRIGHT; |
|
145 |
return NORMAL; |
|
146 |
} |
|
147 |
if(dir==ILEFT){ |
|
148 |
stateLength += 2; |
|
149 |
state[1] = ILEFT; |
|
150 |
state[2] = ISTRAIGHT; |
|
151 |
return NORMAL |
|
152 |
} |
|
153 |
} |
|
154 |
if(type==OFF_ROAD) |
|
155 |
{ |
|
156 |
if(dir==IRIGHT){ |
|
157 |
stateLength++; |
|
158 |
state[1]=IRIGHT; |
|
159 |
return NORMAL; |
|
160 |
} |
|
161 |
if(dir==ILEFT){ |
|
162 |
stateLength+=2; |
|
163 |
state[1]=ISTRAIGHT; |
|
164 |
state[2]=ILEFT; |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
//Should never get here |
|
169 |
return ERROR; |
|
170 |
} |
|
171 |
|
scout/libscout/src/behaviors/lineDrive.h | ||
---|---|---|
1 |
#ifndef _LINE_DRIVE_ |
|
2 |
#define _LINE_DRIVE_ |
|
3 |
|
|
4 |
#include "lineFollow.h" |
|
5 |
|
|
6 |
/* |
|
7 |
* Defines whether your making a turn from the main two-way road |
|
8 |
* or from a one-way sub road. |
|
9 |
*/ |
|
10 |
#define ON_ROAD 0 |
|
11 |
#define OFF_ROAD 0 |
|
12 |
|
|
13 |
/* |
|
14 |
* The type of turn you are making from the road you are on. |
|
15 |
*/ |
|
16 |
#define ISTRAIGHT 0 |
|
17 |
#define ILEFT 1 |
|
18 |
#define IRIGHT 2 |
|
19 |
#define IUTURN 3 |
|
20 |
|
|
21 |
#define NORMAL -1 |
|
22 |
#define FINISHED -2 |
|
23 |
#define LOST -3 |
|
24 |
#define ERROR -4 |
|
25 |
|
|
26 |
class lineDrive : Behavior{ |
|
27 |
lineDrive (String scoutname): Behavior(scoutname){}; |
|
28 |
run(); |
|
29 |
|
|
30 |
void lineDrive_init(void); |
|
31 |
|
|
32 |
int doDrive(int speed); |
|
33 |
|
|
34 |
void start(void); |
|
35 |
void stop(void); |
|
36 |
|
|
37 |
|
|
38 |
int turn(int type, int dir); |
|
39 |
|
|
40 |
} |
|
41 |
#endif |
Also available in: Unified diff