-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradar.cpp
167 lines (126 loc) · 3.32 KB
/
radar.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
//
// FILE: radar.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.8
// PURPOSE: Arduino library for a pan tilt radar.
// URL: https://github.com/RobTillaart/RADAR
#include "radar.h"
////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
RADAR::RADAR(const uint8_t pinPan, const uint8_t pinTilt)
{
_pinPan = pinPan;
_pinTilt = pinTilt;
_homePan = 0;
_homeTilt = 0;
_pan = _prevPan = 0;
_tilt = _prevTilt = 0;
_panPerSecond = 15;
_tiltPerSecond = 15;
_lastPanTime = _lastTiltTime = millis();
for (uint8_t i = 0; i < RADAR_POSITIONS; i++)
{
_panArray[i] = 0;
_tiltArray[i] = 0;
}
}
////////////////////////////////////////////////////////////
//
// PUBLIC
//
void RADAR::gotoPan(const int16_t pan)
{
if (pan == _pan) return;
_prevPan = getPan();
_pan = pan;
analogWrite(_pinPan, _pan);
_lastPanTime = millis();
}
int16_t RADAR::getPan()
{
// ESTIMATE current position on time it takes to go from previous to new
if (_pan == _prevPan) return _pan;
// if (enough time passed to move to new position) return new position
uint32_t duration = millis() - _lastPanTime;
uint32_t movement = round(duration * _panPerSecond * 0.001);
if ( abs(_pan - _prevPan) <= movement) return _pan;
// else estimate PAN by linear interpolation
if (_pan > _prevPan) return _prevPan + movement;
return _prevPan - movement;
}
void RADAR::gotoTilt(const int16_t tilt)
{
if (tilt == _tilt) return;
_prevTilt = getTilt();
_tilt = tilt;
analogWrite(_pinTilt, _tilt); // 0..180
_lastTiltTime = millis();
}
int16_t RADAR::getTilt()
{
// ESTIMATE current position on time it takes to go from previous to new
if (_tilt == _prevTilt) return _tilt;
// if (enough time passed to move to new position) return new position
uint32_t duration = millis() - _lastTiltTime;
uint32_t movement = round(duration * _tiltPerSecond * 0.001);
if (abs(_tilt - _prevTilt) <= movement) return _tilt;
// estimate TILT by linear interpolation
if (_tilt > _prevTilt) return _prevTilt + movement;
return _prevTilt - movement;
}
void RADAR::gotoPanTilt(const int16_t pan, const int16_t tilt)
{
gotoPan(pan);
gotoTilt(tilt);
}
bool RADAR::setPosition(const uint8_t index, const int16_t pan, const int16_t tilt)
{
if (index >= RADAR_POSITIONS) return false;
_panArray[index] = pan;
_tiltArray[index] = tilt;
return true;
}
bool RADAR::getPosition(const uint8_t index, int16_t & pan, int16_t & tilt)
{
if (index >= RADAR_POSITIONS) return false;
pan = _panArray[index];
tilt = _tiltArray[index];
return true;
}
bool RADAR::gotoPosition(const uint8_t index)
{
if (index >= RADAR_POSITIONS) return false;
gotoPan(_panArray[index]);
gotoTilt(_tiltArray[index]);
return true;
}
void RADAR::setHomePosition(const int16_t pan, const int16_t tilt)
{
_homePan = pan;
_homeTilt = tilt;
}
void RADAR::gotoHomePosition()
{
gotoPan(_homePan);
gotoTilt(_homeTilt);
}
uint32_t RADAR::ping()
{
// TODO ping code here - playground or teckel's improved ping))) ?
return 0;
}
uint32_t RADAR::ping(const int16_t pan, const int16_t tilt)
{
gotoPan(pan);
gotoTilt(tilt);
while (isMoving());
return ping();
}
////////////////////////////////////////////////////////////
//
// PRIVATE
//
// TODO distil private parts (getPan and getTilt share a lot
// -- END OF FILE --