Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (79 loc) · 2.24 KB
/
Copy pathmain.cpp
File metadata and controls
100 lines (79 loc) · 2.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
#include "lib/SIO/inputhub.h"
#include "lib/SIO/outputhub.h"
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <fstream>
#include "KRBC-15MOS.h"
#include <yaml-cpp/yaml.h>
int initPins() {
if (wiringPiSetupPinType(WPI_PIN_BCM) < 0) {
// Failed, return an error
return -1;
}
return 0;
}
std::string getEvent() {
std::ifstream f("/proc/bus/input/devices");
std::cout<<"getting event";
std::string line;
bool fi = false;
std::string event;
while (std::getline(f, line)) {
if (!fi && line.find("Xbox") != std::string::npos) {
fi = true;
} else if (fi && line.find("Handlers") != std::string::npos) {
auto r = line.find("event");
event = line.substr(r, 6); // TODO regexify this to account for >1 digit nums
break;
}
}
f.close();
return event;
}
std::map<std::string, Motor_Config> getConfigs() {
YAML::Node n = YAML::LoadFile("config.yaml");
return n["MotorControllers"].as<std::map<std::string, Motor_Config> >();
}
int main() {
// Init pin system
if (initPins() < 0) {
// Failed, return an error
std::cerr << "wiringPi setup failed!" << std::endl;
return -1;
}
// Init Motor controllers
std::map<std::string, Motor_Config> configs = getConfigs();
KRBC_15MOS left(configs["Left"]);
// KRBC_15MOS right(configs["Right"]);
// Init I/0
InputHub ihub;
OutputHub ohub;
ihub.makeReadThread("event5", XBOX);
float sig = 0;
ohub.addAction(
action(
ihub.getConsumable(controller::AXIS, xbox::R_TRIGGER),
[&left] (float f) {
float sig = (f / 1023); // [0, 1023-]->[0,100]
sig = 0.20 + sig * (0.73 - 0.20); //
left.throttle(sig); // actual controller takes [~24%-74%]
}
)
);
ohub.addAction(
action(
ihub.getConsumable(controller::BUTTON, xbox::HAMBURGER),
[&left] (bool b) {
if (!b) {
left.reverse();
}
}
)
);
std::cout << "Running..." << std::endl;
// Main loop
while (true) {
ohub.exec();
}
return 0;
}