root / branches / simulator / projects / libdragonfly / move.c @ 891
History | View | Annotate | Download (3.79 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
|
27 |
/**
|
28 |
* @file move.c
|
29 |
* @brief Functions for moving
|
30 |
*
|
31 |
* Implementation of functions for moving the robot.
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
**/
|
35 |
|
36 |
#include "dragonfly_lib.h" |
37 |
|
38 |
#include "move.h" |
39 |
#include "rangefinder.h" |
40 |
|
41 |
|
42 |
// Internal prototypes
|
43 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr); |
44 |
|
45 |
// global varaibles for move_avoid
|
46 |
int d1, d2, d3, d4, d5;
|
47 |
|
48 |
void move (int velocity, int omega) { |
49 |
int vl = 0; |
50 |
int vr = 0; |
51 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
52 |
//
|
53 |
|
54 |
if (vl < 0) { |
55 |
motor1_set(BACKWARD, -vl); |
56 |
} else {
|
57 |
motor1_set(FORWARD, vl); |
58 |
} |
59 |
if (vr < 0) { |
60 |
motor2_set(BACKWARD, -vr); |
61 |
} else {
|
62 |
motor2_set(FORWARD, vr); |
63 |
} |
64 |
} |
65 |
|
66 |
|
67 |
void move_avoid(int velocity, int omega, int strength) { |
68 |
int pControl;
|
69 |
int vl = 0; |
70 |
int vr = 0; |
71 |
int temp;
|
72 |
|
73 |
temp=range_read_distance(IR1); |
74 |
d1 = (temp == -1) ? d1 : temp;
|
75 |
|
76 |
temp=range_read_distance(IR2); |
77 |
d2=(temp == -1) ? d2 : temp;
|
78 |
|
79 |
temp=range_read_distance(IR3); |
80 |
d3=(temp == -1) ? d3 : temp;
|
81 |
|
82 |
temp=range_read_distance(IR4); |
83 |
d4=(temp == -1) ? d4 : temp;
|
84 |
|
85 |
temp=range_read_distance(IR5); |
86 |
d5=(temp == -1) ? d5 : temp;
|
87 |
|
88 |
/* Avoid obstacles ahead
|
89 |
if(d2>170)
|
90 |
v*=-1;
|
91 |
|
92 |
Naturally slow down if there is something in the way.
|
93 |
if(d2>150 || d1>180 || d3>180){
|
94 |
v>>=1;
|
95 |
*/
|
96 |
|
97 |
//pControl= (((d3-d1) + (d4-d5))*strength)/100;
|
98 |
pControl= (((d5-d4))*strength)/100;
|
99 |
omega = (omega*(100-strength))/100 + pControl; |
100 |
translateAngulartoLinear(velocity, omega, &vl , &vr ); |
101 |
|
102 |
if (vl < 0) { |
103 |
motor1_set(BACKWARD, -vl); |
104 |
} else {
|
105 |
motor1_set(FORWARD, vl); |
106 |
} |
107 |
if (vr < 0) { |
108 |
motor2_set(BACKWARD, -vr); |
109 |
} else {
|
110 |
motor2_set(FORWARD, vr); |
111 |
} |
112 |
} |
113 |
|
114 |
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr) { |
115 |
//omega: angle measure, positive couter-clockwise from front.
|
116 |
// -180 <= omega <= 180
|
117 |
//velocity: -255 <= velocity <= 255
|
118 |
|
119 |
long int vltemp, vrtemp; |
120 |
|
121 |
//make sure values are in bounds
|
122 |
if (velocity < -255 || velocity >255 || omega < -255 || omega > 255) return; |
123 |
|
124 |
//compute
|
125 |
vrtemp = velocity + omega * 3;
|
126 |
vltemp = velocity - omega * 3;
|
127 |
|
128 |
//check to see if max linear velocities have been exceeded.
|
129 |
if (vrtemp > 255) { |
130 |
vltemp = 255 * vltemp / vrtemp;
|
131 |
vrtemp = 255;
|
132 |
} |
133 |
if (vltemp > 255) { |
134 |
vrtemp = 255 * vrtemp / vltemp;
|
135 |
vltemp = 255;
|
136 |
} |
137 |
if (vrtemp < -255) { |
138 |
vltemp = -255 * vltemp / vrtemp;
|
139 |
vrtemp = -255;
|
140 |
} |
141 |
if (vltemp < -255) { |
142 |
vrtemp = -255 * vrtemp / vltemp;
|
143 |
vltemp = -255;
|
144 |
} |
145 |
|
146 |
*vr = (int)vrtemp;
|
147 |
*vl = (int)vltemp;
|
148 |
} |