Repository navigation
Expand file tree
/
Copy pathRoverControls.cpp
More file actions
222 lines (207 loc) · 7.95 KB
/
Copy pathRoverControls.cpp
File metadata and controls
222 lines (207 loc) · 7.95 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "stdafx.h"
#include "FA_API.h"
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
// define symbolic constant names & hexadecimal values to identify keypresses
#define VK_KEY_W 0x57 //('W') W forward
#define VK_KEY_A 0x41 //('A') A left
#define VK_KEY_D 0x44 //('D') D right
#define VK_KEY_S 0x53 //('S') S backward
#define VK_KEY_Q 0x51 //('Q') Q cancel reverse keys
class DeviceOne {
public:
// Member to use later to link to GUI
void forward() {};
void backward() {};
void left() {};
void right() {};
void reverse() {};
void cancel_reverse() {};
char comPort_dvc1 = 5; // COM port
};
class MovementKeys {
// member variables:
private:
// keeping reference to deviceOne instead of copy;
// main() has to make sure deviceOne object out-lives MovementKeys object
DeviceOne& deviceOne;
int key_forward, key_backward, key_left, key_right, key_reverse, cancel_reverse;
public:
char iPort = deviceOne.comPort_dvc1;
int reset = 0;
int reverse = 0;
// constructor, which only sets deviceOne and keeps the keys name at their Default
// deviceOne must be initialized before constructor function body
// as references have no default value
MovementKeys(DeviceOne& gr) : deviceOne(gr)
{ // Default list:
key_forward = VK_UP;
key_backward = VK_DOWN;
key_left = VK_LEFT;
key_right = VK_RIGHT;
key_reverse = VK_RSHIFT;
cancel_reverse = VK_BACK;
int reverse = 0;
/*string name;
int comPort;*/
}
// loop to define rules of the keys pressed commands
void definedCommand()
{
//FA_ComOpen(iPort);
while (true && reset < 2)
{
// Forwards driving
if (GetAsyncKeyState(key_forward) < 0)
{
cout << ("forward\n");
FA_SetMotors(iPort, 30, 30);
if (GetAsyncKeyState(key_forward) == 0)
{
cout << ("Forwards stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
// Backwards driving
if (GetAsyncKeyState(key_backward) < 0)
{
cout << ("backwards\n");
FA_SetMotors(iPort, -30, -30);
if (GetAsyncKeyState(key_backward) == 0)
{
cout << ("Backwards stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
//====================================================
// programming LEFT & RIGHT keys to perfom 2 actions
// Forward-Left or Forward-Right and Backwards-Left or Backwards-Right
if (GetAsyncKeyState(key_reverse) < 0) { reverse = 1; }
if (reverse == 0) {
// Forwards-Left driving
if (GetAsyncKeyState(key_left) < 0)
{
cout << ("Forwards left.\n");
FA_SetMotors(iPort, 5, 30);
if (GetAsyncKeyState(key_left) == 0)
{
cout << ("Forwards left stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
// Forwards-Right driving
if (GetAsyncKeyState(key_right) < 0)
{
cout << ("Forwards right.\n");
FA_SetMotors(iPort, 30, 5);
if (GetAsyncKeyState(key_right) == 0)
{
cout << ("Forwards right stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
}
if (reverse == 1) {
// Backwards-Left driving
if (GetAsyncKeyState(key_left) < 0)
{
cout << ("back left\n");
FA_SetMotors(iPort, -5, -30);
if (GetAsyncKeyState(key_left) == 0)
{
cout << ("Back left stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
if (GetAsyncKeyState(key_right) < 0)
{
cout << ("backwards right\n");
// Backwards-Right driving
if (GetAsyncKeyState(key_right) < 0)
{
cout << ("Back right\n");
FA_SetMotors(iPort, -30, -5);
if (GetAsyncKeyState(key_right) == 0)
{
cout << ("Back right stopped. \n");
FA_SetMotors(iPort, 0, 0);
}
}
}
}
if (GetAsyncKeyState(cancel_reverse) < 0) { reverse = 0; }
// ====================================================
// OPTIONAL change keys from within loop with ths code:
// key_forward = VK_KEY_W; key_backward = VK_KEY_S; key_left = VK_KEY_A; key_right = VK_KEY_D;
if (GetAsyncKeyState(VK_LCONTROL) < 0 && reset == 0) {
cout << ("Main control changed to W - A - S - D letters.\n");
reset++;
break;
}
if (GetAsyncKeyState(VK_RCONTROL) < 0 && reset == 1) {
cout << ("Main control reset to default - Arrow Keys.\n");
reset--;
break;
}
// define Honk
if (GetAsyncKeyState(VK_TAB) < 0) {
if (GetAsyncKeyState(VK_TAB) < 0) {
FA_PlayNote(iPort, 50, 280);
FA_PlayNote(iPort, 50, 286);
Sleep(3000);
}
}
// option to exit program
if (GetAsyncKeyState(VK_SPACE) < 0)
{
FA_ComClose(iPort);
exit(0);
}
}
}
// function overloading
void sounds(int start) {
FA_PlayNote(iPort, 500, start); // make start 100
FA_PlayNote(iPort, 600, start);
}
void sounds(int start, int honk) {
FA_PlayNote(iPort, start, 100); // start = 500
FA_PlayNote(iPort, honk, 100); // honk = 600
}
// function to change the key control values stored in the member variables
// can be called by constructor or externally
void changeKeys(int change_key_forward, int change_key_backward, int change_key_left, int change_key_right, int change_key_reverse, int change_key_cancel_reverse)
{
key_forward = change_key_forward;
key_backward = change_key_backward;
key_left = change_key_left;
key_right = change_key_right;
key_reverse = change_key_reverse;
cancel_reverse = change_key_cancel_reverse;
}
};
int main()
{
DeviceOne deviceOne; // making a class object
FA_ComOpen(deviceOne.comPort_dvc1); // open device COM port
// 1. making a class object referenced to previous class object
// 2/3/4. calling its member methods
while (true) {
MovementKeys control(deviceOne);
control.sounds(100);
control.definedCommand();
control.changeKeys(VK_KEY_W, VK_KEY_S, VK_KEY_A, VK_KEY_D, VK_LSHIFT, VK_KEY_Q);
control.sounds(300, 400);
control.definedCommand();
}
FA_ComClose(deviceOne.comPort_dvc1); // closing device COM port
}
// TO implement:
//
// //Print a string to the LCD
// FA_LCDClear(PortNumber);
// FA_LCDPrintString(PortNumber, 19, 8, (unsigned char *) "Liverpool Hope University");
// FA_LCDPrintString(PortNumber, 31, 16, (unsigned char *) "Driving Test");
//