-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicalController.cpp
More file actions
257 lines (228 loc) · 7.24 KB
/
PhysicalController.cpp
File metadata and controls
257 lines (228 loc) · 7.24 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "PhysicalController.hpp"
#include "SFML/Window/Joystick.hpp"
#include <iostream>
#include <string>
const std::string PhysicalController::buttonName[] = {"A","B","X","Y","LB","RB","MENU","START","XBOX","LSB","RSB"};
PhysicalController::PhysicalController(unsigned int port, unsigned int mode){
this->setMode(mode);
this->setPort(port);
sf::Joystick::update();
if(sf::Joystick::isConnected(port)) std::cout << "Controller (Port #" << port << " Mode #" << mode << ") connected" <<std::endl;
else std::cout << "Connection failed" << std::endl;
}
PhysicalController::~PhysicalController(){
std::cout << "Controller destructed" << std::endl;
}
void PhysicalController::setMode(unsigned int mode){
if(mode>9) this->mode = 0; //assures all other channel arguments default to 0
else this->mode = mode;
}
void PhysicalController::setPort(unsigned int port){
if(port>8) this->port = 0; //assures invalid port arguments default to 0
else this->port = port;
}
bool PhysicalController::getButton(unsigned int buttonNumber){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(this->port,buttonNumber)) return true;
else return false;
}
bool PhysicalController::getButton(std::string buttonString){
for(int x=0;x<11;x++){
if(buttonName[x].compare(std::string(buttonString)) == 0){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(this->port,x)) return true;
}
}
return false;
}
float PhysicalController::getThrust(){
if(cruiseControl){
return this->cruiseControlThrust;
}
else{
float thrust = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::V);
thrust-=(2*thrust); //thrust values need to be reversed because of how SFML is designed
if(thrust<20) return 0; //cannot have negative thrust
else return thrust/100 * .23;
case 2:
case 3:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Y);
thrust-=(2*thrust); //thrust values need to be reversed because of how SFML is designed
if(thrust<20) return 0; //cannot have negative thrust
else return thrust/100 * .23 ;
case 4:
case 5:
case 9:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::R);
return (thrust+=100)/200 * .23; //thrust values from the trigger range [-100,100] because of how SFML is designed... need to be from [0,200] then [0,100]
case 6:
case 7:
case 8:
thrust = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Z);
return (thrust+=100)/200 * .23; //thrust values from the trigger range [-100,100] because of how SFML is designed... need to be from [0,200] then [0,100]
default:
std::cout << "Error: thrust" << std::endl;
return 0;
}
}
}
float PhysicalController::getPitch(){
float pitch = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
pitch = sf::Joystick::getAxisPosition(this->port, sf::Joystick::Y);
if(pitch<20 && pitch>-20) return 0;
else return pitch/100;
case 2:
case 3:
case 6:
case 7:
case 9:
pitch = sf::Joystick::getAxisPosition(this->port, sf::Joystick::V);
if(pitch<20 && pitch>-20) return 0;
else return pitch/100;
default:
std::cout << "Error: pitch" << std::endl;
return 0;
}
}
float PhysicalController::getRoll(){
float roll = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
roll = sf::Joystick::getAxisPosition(this->port, sf::Joystick::X);
if(roll<20 && roll>-20) return 0;
else return roll/100;
case 2:
case 3:
case 6:
case 7:
case 9:
roll = sf::Joystick::getAxisPosition(this->port, sf::Joystick::U);
if(roll<20 && roll>-20) return 0;
else return roll/100;
default:
std::cout << "Error: roll" << std::endl;
return 0;
}
}
float PhysicalController::getYaw(){
float yaw = 0;
sf::Joystick::update();
switch(mode){
case 0:
case 3:
case 4:
case 7:
case 8:
yaw = sf::Joystick::getAxisPosition(this->port, sf::Joystick::X);
if(yaw<20 && yaw>-20) return 0;
else return yaw/100;
case 1:
case 2:
case 5:
case 6:
case 9:
yaw = sf::Joystick::getAxisPosition(this->port, sf::Joystick::U);
if(yaw<20 && yaw>-20) return 0;
else return yaw/100;
default:
std::cout << "Error: yaw" << std::endl;
return 0;
}
}
float PhysicalController::getMotor(){
return this->getThrust();
}
float PhysicalController::getElevator(){
return this->getPitch();
}
float PhysicalController::getAileron(){
return this->getRoll();
}
float PhysicalController::getRudder(){
return this->getYaw();
}
float PhysicalController::toggleLeftHandMode(){
sf::Joystick::update();
if(sf::Joystick::getAxisPosition(this->port, sf::Joystick::PovX)==-100){
this->setMode(8);
}
}
float PhysicalController::toggleRightHandMode(){
sf::Joystick::update();
if(sf::Joystick::getAxisPosition(this->port, sf::Joystick::PovX)==100){
this->setMode(9);
}
}
float PhysicalController::toggleCruiseControll(){
float prevCruiseControlCounter = this->cruiseControlCounter;
sf::Joystick::update();
switch(mode){
case 0:
case 1:
case 4:
case 5:
case 8:
if(sf::Joystick::isButtonPressed(this->port,9)){
this->cruiseControlCounter++;
}
else{
this->cruiseControlCounter = 0;
}
break;
case 2:
case 3:
case 6:
case 7:
case 9:
if(sf::Joystick::isButtonPressed(this->port,10)){
this->cruiseControlCounter++;
}
else{
this->cruiseControlCounter = 0;
}
break;
}
if(cruiseControlCounter==0 && prevCruiseControlCounter>0){
std::cout << "cruise control toggled" << std::endl;
this->cruiseControlThrust = this->getThrust();
cruiseControl = !cruiseControl;
}
}
void PhysicalController::listenForToggles(){
this->toggleLeftHandMode();
this->toggleRightHandMode();
this->toggleCruiseControll();
}
float PhysicalController::listButtons(){
std::cout << "";
for(int x=0;x<32;x++){
sf::Joystick::update();
if(sf::Joystick::isButtonPressed(0,x)){
std::cout << buttonName[x] << " ";
}
}
std::cout << std::endl;
}
void PhysicalController::update() {
listenForToggles();
}
Controller::Type PhysicalController::getType() {
return Controller::PHYSICAL;
}