-
Notifications
You must be signed in to change notification settings - Fork 18
/
ElegooAutomaticDriver1.h
50 lines (39 loc) · 1.24 KB
/
ElegooAutomaticDriver1.h
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
#ifndef __ELEGOO_AUTOMATIC_DRIVER_1_H__
#define __ELEGOO_AUTOMATIC_DRIVER_1_H__
#include "ElegooDriverBase.h"
// is interruptible
class ElegooAutomaticDriver1: public ElegooDriverBase
{
public:
ElegooAutomaticDriver1(int pSafetyDistanceInCM, ElegooDistanceUnit & pDistUnit, ElegooMotorUnit & pMotorUnit) :
ElegooDriverBase(pSafetyDistanceInCM, pDistUnit, pMotorUnit)
{
}
virtual ~ElegooAutomaticDriver1()
{
}
virtual int processCommand(ElegooCommand cmd)
{
const int frontDistance = distUnit.frontDistance();
if (frontDistance > safetyDistanceInCM)
{
return motorUnit.moveForwards().statusOK();
}
// frontDistance <= safetyDistanceInCM !!
motorUnit.stopMoving();
const int rightDistance = distUnit.rightDistance();
const int leftDistance = distUnit.leftDistance();
distUnit.frontDistance(); // reposition sensor, to avoid delays
if ((rightDistance > safetyDistanceInCM) && (rightDistance >= leftDistance))
{
return motorUnit.turnRight().stopMoving().statusOK();
}
if ((leftDistance > safetyDistanceInCM) && leftDistance >= rightDistance)
{
return motorUnit.turnLeft().stopMoving().statusOK();
}
// we're stuck
return backOut().statusOK();
}
};
#endif