2013-09-30 12:46:09 -04:00
|
|
|
//
|
|
|
|
// FILE: radar.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-23 07:53:23 -05:00
|
|
|
// VERSION: 0.1.7
|
2020-11-27 05:33:55 -05:00
|
|
|
// PURPOSE: Arduino library for a pan tilt radar.
|
|
|
|
// URL: https://github.com/RobTillaart/RADAR
|
2022-11-23 07:53:23 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2013-09-30 12:46:09 -04:00
|
|
|
|
|
|
|
#include "radar.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2013-09-30 12:46:09 -04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
2022-11-23 07:53:23 -05:00
|
|
|
// CONSTRUCTOR
|
2013-09-30 12:46:09 -04:00
|
|
|
//
|
2020-11-27 05:33:55 -05:00
|
|
|
RADAR::RADAR(const uint8_t pinPan, const uint8_t pinTilt)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
_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;
|
|
|
|
}
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PUBLIC
|
|
|
|
//
|
2020-11-27 05:33:55 -05:00
|
|
|
void RADAR::gotoPan(const int16_t pan)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
if (pan == _pan) return;
|
|
|
|
_prevPan = getPan();
|
|
|
|
_pan = pan;
|
|
|
|
analogWrite(_pinPan, _pan);
|
|
|
|
_lastPanTime = millis();
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
int16_t RADAR::getPan()
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2022-11-23 07:53:23 -05:00
|
|
|
// ESTIMATE current position on time it takes to go from previous to new
|
2020-11-27 05:33:55 -05:00
|
|
|
if (_pan == _prevPan) return _pan;
|
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
// if (enough time passed to move to new position) return new position
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t duration = millis() - _lastPanTime;
|
|
|
|
uint32_t movement = round(duration * _panPerSecond * 0.001);
|
|
|
|
if ( abs(_pan - _prevPan) <= movement) return _pan;
|
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
// else estimate PAN by linear interpolation
|
2020-11-27 05:33:55 -05:00
|
|
|
if (_pan > _prevPan) return _prevPan + movement;
|
|
|
|
return _prevPan - movement;
|
2015-03-06 17:34:37 -05:00
|
|
|
}
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
void RADAR::gotoTilt(const int16_t tilt)
|
2015-03-06 17:34:37 -05:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
if (tilt == _tilt) return;
|
|
|
|
|
|
|
|
_prevTilt = getTilt();
|
|
|
|
_tilt = tilt;
|
2022-11-23 07:53:23 -05:00
|
|
|
analogWrite(_pinTilt, _tilt); // 0..180
|
2020-11-27 05:33:55 -05:00
|
|
|
_lastTiltTime = millis();
|
2015-03-06 17:34:37 -05:00
|
|
|
}
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
int16_t RADAR::getTilt()
|
2015-03-06 17:34:37 -05:00
|
|
|
{
|
2022-11-23 07:53:23 -05:00
|
|
|
// ESTIMATE current position on time it takes to go from previous to new
|
2020-11-27 05:33:55 -05:00
|
|
|
if (_tilt == _prevTilt) return _tilt;
|
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
// if (enough time passed to move to new position) return new position
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t duration = millis() - _lastTiltTime;
|
|
|
|
uint32_t movement = round(duration * _tiltPerSecond * 0.001);
|
|
|
|
if (abs(_tilt - _prevTilt) <= movement) return _tilt;
|
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
// estimate TILT by linear interpolation
|
2021-12-27 14:23:58 -05:00
|
|
|
if (_tilt > _prevTilt) return _prevTilt + movement;
|
2020-11-27 05:33:55 -05:00
|
|
|
return _prevTilt - movement;
|
2015-03-06 17:34:37 -05:00
|
|
|
}
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
void RADAR::gotoPanTilt(const int16_t pan, const int16_t tilt)
|
2015-03-06 17:34:37 -05:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
gotoPan(pan);
|
|
|
|
gotoTilt(tilt);
|
2015-03-06 17:34:37 -05:00
|
|
|
}
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-27 14:23:58 -05:00
|
|
|
bool RADAR::setPosition(const uint8_t index, const int16_t pan, const int16_t tilt)
|
2015-03-06 17:34:37 -05:00
|
|
|
{
|
2021-12-27 14:23:58 -05:00
|
|
|
if (index >= RADAR_POSITIONS) return false;
|
|
|
|
_panArray[index] = pan;
|
|
|
|
_tiltArray[index] = tilt;
|
2020-11-27 05:33:55 -05:00
|
|
|
return true;
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-27 14:23:58 -05:00
|
|
|
bool RADAR::getPosition(const uint8_t index, int16_t & pan, int16_t & tilt)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2021-12-27 14:23:58 -05:00
|
|
|
if (index >= RADAR_POSITIONS) return false;
|
|
|
|
pan = _panArray[index];
|
|
|
|
tilt = _tiltArray[index];
|
2020-11-27 05:33:55 -05:00
|
|
|
return true;
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-12-27 14:23:58 -05:00
|
|
|
bool RADAR::gotoPosition(const uint8_t index)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2021-12-27 14:23:58 -05:00
|
|
|
if (index >= RADAR_POSITIONS) return false;
|
|
|
|
gotoPan(_panArray[index]);
|
|
|
|
gotoTilt(_tiltArray[index]);
|
2020-11-27 05:33:55 -05:00
|
|
|
return true;
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
void RADAR::setHomePosition(const int16_t pan, const int16_t tilt)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
_homePan = pan;
|
|
|
|
_homeTilt = tilt;
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2015-03-06 17:34:37 -05:00
|
|
|
void RADAR::gotoHomePosition()
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
gotoPan(_homePan);
|
|
|
|
gotoTilt(_homeTilt);
|
2013-09-30 12:46:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t RADAR::ping()
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2022-11-23 07:53:23 -05:00
|
|
|
// TODO ping code here - playground or teckel's improved ping))) ?
|
2013-09-30 12:46:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t RADAR::ping(const int16_t pan, const int16_t tilt)
|
2013-09-30 12:46:09 -04:00
|
|
|
{
|
2015-03-06 17:34:37 -05:00
|
|
|
gotoPan(pan);
|
|
|
|
gotoTilt(tilt);
|
|
|
|
while (isMoving());
|
2013-09-30 12:46:09 -04:00
|
|
|
return ping();
|
|
|
|
}
|
2022-11-23 07:53:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-12-27 14:23:58 -05:00
|
|
|
// TODO distil private parts (getPan and getTilt share a lot
|
2013-09-30 12:46:09 -04:00
|
|
|
|
2021-05-28 07:42:27 -04:00
|
|
|
|
2022-11-23 07:53:23 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-12-27 14:23:58 -05:00
|
|
|
|