Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libdragonfly / move.c @ 980

History | View | Annotate | Download (5.3 KB)

1 241 bcoltin
/**
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 8 bcoltin
#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 380 jknichel
int d1, d2, d3, d4, d5;
47
/**
48 8 bcoltin
 * @defgroup move Movement
49
 * @brief Functions fo controlling robot motion
50
 * Higher level functions to control the movement of robots.
51
 *
52
 * @{
53
 **/
54
55
56
/**
57
 * Causes the robot to move with the given translation and rotational velocities.
58
 * motors_init must be called before this function can be used.
59
 *
60
 * @param velocity the translational velocity of the robot, in the range -255 to 255.
61
 * A positive value indicates forward motion, while a negative value indicates
62
 * backwards motion.
63
 *
64
 * @param omega the rotational velocity of the robot, in the range -255 to 255.
65
 * A positive value indicates a counterclockwise velocity, while a negative
66
 * value indicates a clockwise velocity.
67
 *
68
 * @see motors_init, motor1_set, motor2_set
69
 **/
70
void move (int velocity, int omega) {
71
  int vl = 0;
72
  int vr = 0;
73
  translateAngulartoLinear(velocity, omega, &vl , &vr );
74
  //
75
76
  if (vl < 0) {
77
    motor1_set(BACKWARD, -vl);
78
  } else {
79
    motor1_set(FORWARD, vl);
80
  }
81
  if (vr < 0) {
82
    motor2_set(BACKWARD, -vr);
83
  } else {
84
    motor2_set(FORWARD, vr);
85
  }
86
}
87
88
89
/**
90
 * Moves the robot with the given translational and angular velocities
91
 * while avoiding obstacles. To be effective, this function must be
92
 * called repeatedly throughout the motion. It relies on the IR
93
 * rangefinders to detect obstacles. Before calling this function,
94
 * motors_init and range_init must be called.
95
 *
96
 * @param velocity the translational velocity of the robot, in the
97
 * range -255 to 255. A positive value indicates forward motion.
98
 *
99
 * @param omega the rotational velocity of the robot, in the range
100
 * -255 to 255. A positive value indicates a counterclockwise velocity.
101
 *
102
 * @param strength the strength of the avoid behavior, in the range
103
 * 0 to 100.
104
 *
105
 * @see motors_init, range_init, move
106
 **/
107 380 jknichel
void move_avoid(int velocity, int omega, int strength) {
108 8 bcoltin
  int pControl;
109
  int vl = 0;
110
  int vr = 0;
111
  int temp;
112
113
  temp=range_read_distance(IR1);
114
  d1 = (temp == -1) ? d1 : temp;
115
116
  temp=range_read_distance(IR2);
117
  d2=(temp == -1) ? d2 : temp;
118
119
  temp=range_read_distance(IR3);
120
  d3=(temp == -1) ? d3 : temp;
121
122
  temp=range_read_distance(IR4);
123
  d4=(temp == -1) ? d4 : temp;
124
125
  temp=range_read_distance(IR5);
126
  d5=(temp == -1) ? d5 : temp;
127
128
  /* Avoid obstacles ahead
129 380 jknichel
     if(d2>170)
130
     v*=-1;
131 8 bcoltin

132 380 jknichel
     Naturally slow down if there is something in the way.
133
     if(d2>150 || d1>180 || d3>180){
134
     v>>=1;
135 8 bcoltin
  */
136
137
  //pControl= (((d3-d1) + (d4-d5))*strength)/100;
138
  pControl= (((d5-d4))*strength)/100;
139
  omega = (omega*(100-strength))/100 + pControl;
140
  translateAngulartoLinear(velocity, omega, &vl , &vr );
141
142
  if (vl < 0) {
143
    motor1_set(BACKWARD, -vl);
144
  } else {
145
    motor1_set(FORWARD, vl);
146
  }
147
  if (vr < 0) {
148
    motor2_set(BACKWARD, -vr);
149
  } else {
150
    motor2_set(FORWARD, vr);
151
  }
152
}
153
154
/**@}**///end the motion group
155
156
void translateAngulartoLinear (int velocity, int omega, int* vl, int* vr) {
157 380 jknichel
  //omega: angle measure, positive couter-clockwise from front.
158
  // -180 <= omega <= 180
159
  //velocity: -255 <= velocity <= 255
160 8 bcoltin
161
  long int vltemp, vrtemp;
162
163
  //make sure values are in bounds
164
  if (velocity < -255 || velocity >255 || omega < -255 || omega > 255) return;
165
166
  //compute
167 380 jknichel
  vrtemp = velocity + omega * 3;
168
  vltemp = velocity - omega * 3;
169 8 bcoltin
170 380 jknichel
  //check to see if max linear velocities have been exceeded.
171 8 bcoltin
  if (vrtemp > 255) {
172
    vltemp = 255 * vltemp / vrtemp;
173
    vrtemp = 255;
174
  }
175
  if (vltemp > 255) {
176 380 jknichel
    vrtemp = 255 * vrtemp / vltemp;
177
    vltemp = 255;
178 8 bcoltin
  }
179
  if (vrtemp < -255) {
180
    vltemp = -255 * vltemp / vrtemp;
181
    vrtemp = -255;
182
  }
183
  if (vltemp < -255) {
184
    vrtemp = -255 * vrtemp / vltemp;
185
    vltemp = -255;
186
  }
187
188
  *vr = (int)vrtemp;
189
  *vl = (int)vltemp;
190
}