-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRRangers.cpp
executable file
·177 lines (128 loc) · 3.35 KB
/
IRRangers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* IRRangers.cpp
*
* Created on: Dec 12, 2009
* Author: asher
*/
#include "IRRangers.h"
#include "WProgram.h"
#include "Robot.h"
#include <avr/delay.h>
IRRangers::IRRangers() {
// TODO Auto-generated constructor stub
RFServo.attach(6,544, 2300);
LFservo.attach(5);
LBServo.attach(4, 500, 2400);//544, 2300);
RBServo.attach(2, 544, 2300);
setServoAngle(0);
this->setMinScanAngle(0);
this->setMaxScanAngle(180);
currentAngle = minAngle;
setScanRate(90);
Serial.print("Scan rate : ");
Serial.print(this->DeltaAnglePerUpdate);
}
IRRangers::~IRRangers() {
// TODO Auto-generated destructor stub
}
void IRRangers::setScanRate(int angleDegreesPerSecond)
{
this->scanRateDegSec = angleDegreesPerSecond;
this->DeltaAnglePerUpdate= angleDegreesPerSecond/25;
}
void IRRangers::setMinScanAngle(int degrees)
{
this->minAngle = degrees;
}
void IRRangers::setServoAngle(int degrees)
{
Serial.print("Setting Angle to: ");
Serial.println(degrees);
RFServo.write(180-degrees);
LFservo.write(degrees);
LBServo.write(180-degrees);
RBServo.write(degrees);
}
void IRRangers::setMaxScanAngle(int degrees)
{
this->maxAngle = degrees;
}
void IRRangers::ContinousScan()
{
int angle=0;
int direction =0;
while(1==1){
setServoAngle(angle);
Serial.print(angle);
Serial.println(" ");
delay(80);
if (direction ==0) angle = angle +5;
else angle = angle -5;
if (angle >180) {
angle = 180;
direction =1;
}
if (angle < 0){
direction =0;
angle = 0;
}
}
}
//
void IRRangers::scan2(){
static unsigned long pTime ;
unsigned long time = millis();
if ( (time-pTime) >= 40){ //Update Scan Angle
currentAngle += DeltaAnglePerUpdate;
if (currentAngle >= maxAngle) {
currentAngle = maxAngle;
DeltaAnglePerUpdate = DeltaAnglePerUpdate *-1;
}
if (currentAngle <=minAngle ){
currentAngle = minAngle;
DeltaAnglePerUpdate = DeltaAnglePerUpdate *-1;
}
setServoAngle(currentAngle);
scanIndex++;
if (scanIndex > (maxScans -1)){
scanIndex = 0;
}
this->data[scanIndex].angle = currentAngle;
this->data[scanIndex].leftEncoderCount = aiRobot.getLeftEncoderCount();
this->data[scanIndex].rightEncocerCount = aiRobot.getRightEncoderCount();
pTime =time;
}
for (int i=0;i<4; i++){
this->data[scanIndex].irData[i] = this->data[scanIndex].irData[i] *3/4 + analogRead(i)/4;
}
}
/*
*Scan moves the IR Sensors back and forth at the at the rate
*/
void IRRangers::scan()
{
static int time ;
time++;
if ( time >= 40){ //Update Scan Angle
currentAngle += DeltaAnglePerUpdate;
if (currentAngle >= maxAngle) {
currentAngle = maxAngle;
DeltaAnglePerUpdate = DeltaAnglePerUpdate *-1;
}
if (currentAngle <=minAngle ){
currentAngle = minAngle;
DeltaAnglePerUpdate = DeltaAnglePerUpdate *-1;
}
setServoAngle(currentAngle);
scanIndex++;
if (scanIndex > (maxScans -1)){
scanIndex = 0;
}
this->data[scanIndex].leftEncoderCount = aiRobot.getLeftEncoderCount();
this->data[scanIndex].rightEncocerCount = aiRobot.getRightEncoderCount();
time =0;
}
for (int i=0;i<4; i++){
this->data[scanIndex].irData[i] = this->data[scanIndex].irData[i] *3/4 + analogRead(i)/4;
}
}