-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnoopRobot.cpp
121 lines (98 loc) · 2.04 KB
/
SnoopRobot.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
#include "SnoopRobot.h"
#include <cstdio>
#include <unistd.h>
#include "MotorController.h"
#ifdef __arm__
#include "MotorControllerWiringpi.h"
#else
#include "MotorControllerNopi.h"
#endif
enum DirectionState
{
STRAIGHT = 0,
LEFT = 10,
RIGHT = 20
};
enum SpeedState
{
STOP = 0,
FORWARD = 10,
BACKWARD = 20
};
struct SnoopRobotPimplData
{
MotorController* motorController;
DirectionState directionState;
SpeedState speedState;
};
SnoopRobot::SnoopRobot(void)
{
pimplData = new SnoopRobotPimplData;
#ifdef __arm__
pimplData->motorController = MotorControllerWiringpi::getInstance();
#else
pimplData->motorController = MotorControllerNopi::getInstance();
#endif
pimplData->directionState = STRAIGHT;
pimplData->speedState = STOP;
}
SnoopRobot::~SnoopRobot(void)
{
delete pimplData;
}
void SnoopRobot::applyCommand(SnoopRobot::Command command)
{
printf("Command %d\n", command);
switch(command)
{
case STOP_MOVE:
pimplData->speedState = SpeedState::STOP;
break;
case FORWARD:
pimplData->speedState = SpeedState::FORWARD;
break;
case BACKWARD:
pimplData->speedState = SpeedState::BACKWARD;
break;
case STOP_CURVE:
pimplData->directionState = DirectionState::STRAIGHT;
break;
case LEFT:
pimplData->directionState = DirectionState::LEFT;
break;
case RIGHT:
pimplData->directionState = DirectionState::RIGHT;
break;
}
}
void SnoopRobot::run(void)
{
while(1)
{
switch(pimplData->speedState)
{
case SpeedState::STOP:
pimplData->motorController->setMotorA(0);
break;
case SpeedState::FORWARD:
pimplData->motorController->setMotorA(60);
break;
case SpeedState::BACKWARD:
pimplData->motorController->setMotorA(-60);
break;
}
switch(pimplData->directionState)
{
case DirectionState::STRAIGHT:
pimplData->motorController->setMotorB(0);
break;
case DirectionState::LEFT:
pimplData->motorController->setMotorB(60);
break;
case DirectionState::RIGHT:
pimplData->motorController->setMotorB(-60);
break;
}
usleep(10000);
}
}