-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_config.cpp
More file actions
83 lines (68 loc) · 2.95 KB
/
input_config.cpp
File metadata and controls
83 lines (68 loc) · 2.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
/*
* Control the Maplin A37JN Robotic Arm via a joystick.
*
* Copyright (C) 2013 Steve Cotton (steve@s.cotton.clara.co.uk)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; version 2 of the License, or
* (at your option) any later version.
*
*/
#include "input_config.h"
#include <stdexcept>
#include <iostream>
using InputMapping::AxisMapping;
using InputMapping::ButtonMapping;
namespace {
std::vector<AxisMapping> axisMapping {
// no joystick axis is mapped to output zero (open/close the claw)
{3, 1, false},
{2, 2, true},
{1, 3, false},
{0, 4, true}
};
std::vector<ButtonMapping> buttonMapping {
{5, 0, ArmDevice::Motion::DOWN_LEFT_OPEN},
{7, 0, ArmDevice::Motion::UP_RIGHT_CLOSE}
};
// buttons which turn on the light, can also be mapped to movement
std::vector<int> lightButtons {{6}, {7}};
};
const std::vector<AxisMapping> InputMapping::getAxisMappings() {
return axisMapping;
}
const std::vector<ButtonMapping> InputMapping::getButtonMappings() {
return buttonMapping;
}
const std::vector<int> InputMapping::getLightButtons() {
return lightButtons;
}
void InputMapping::sanityCheckConfig(int numAxis, int numButtons) {
for (auto mapping = axisMapping.begin(); mapping != axisMapping.end(); mapping++) {
const int inAxis = mapping->inAxis;
const int outAxis = mapping->outAxis;
if (0 > inAxis) {
std::cout << "The joystick configuration is wrong (negative numbers for axis)." << std::endl;
} else if (inAxis >= numAxis) {
std::cout << "Your joystick has " << numAxis << " axes, but controlling the robot needs at least " << (inAxis + 1) << std::endl;
}
if (0 > outAxis || outAxis >= ArmDevice::NUMBER_OF_AXIS) {
std::cerr << "This configuration expects a different robotic device; it's configured to drive motor #" << outAxis << std::endl;
throw std::runtime_error("Input configured to drive different device");
}
}
for (auto mapping = buttonMapping.begin(); mapping != buttonMapping.end(); mapping++) {
const int inButton = mapping->inButton;
const int outAxis = mapping->outAxis;
if (0 > inButton) {
std::cout << "The joystick configuration is wrong (negative numbers for buttons)." << std::endl;
} else if (inButton >= numButtons) {
std::cout << "Your joystick has " << numButtons << " buttons, but controlling the robot needs at least " << (inButton + 1) << std::endl;
}
if (0 > outAxis || outAxis >= ArmDevice::NUMBER_OF_AXIS) {
std::cerr << "This configuration expects a different robotic device; it's configured to drive motor #" << outAxis << std::endl;
throw std::runtime_error("Input configured to drive different device");
}
}
}