MRPT
2.0.3
test.cpp
Go to the documentation of this file.
1
/* +------------------------------------------------------------------------+
2
| Mobile Robot Programming Toolkit (MRPT) |
3
| https://www.mrpt.org/ |
4
| |
5
| Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6
| See: https://www.mrpt.org/Authors - All rights reserved. |
7
| Released under BSD License. See: https://www.mrpt.org/License |
8
+------------------------------------------------------------------------+ */
9
10
#include <
mrpt/core/exceptions.h
>
11
#include <
mrpt/hwdrivers/CJoystick.h
>
12
#include <
mrpt/system/CTicTac.h
>
13
#include <
mrpt/system/os.h
>
14
15
#include <cstdio>
16
#include <iostream>
17
#include <thread>
18
19
using namespace
std;
20
using namespace
mrpt
;
21
using namespace
mrpt::system
;
22
using namespace
mrpt::hwdrivers
;
23
24
// ------------------------------------------------------
25
// TestJoystick
26
// ------------------------------------------------------
27
void
TestJoystick
()
28
{
29
// Open first joystick:
30
// ---------------------------
31
float
x, y, z;
32
std::vector<bool> buttons;
33
CTicTac
tictac;
34
CJoystick
joy;
35
36
const
int
nJoystick = 0;
// The first one
37
38
printf(
"Press any key to stop program...\n"
);
39
40
while
(!
mrpt::system::os::kbhit
())
41
{
42
tictac.
Tic
();
43
if
(joy.
getJoystickPosition
(nJoystick, x, y, z, buttons))
44
{
45
double
t = tictac.
Tac
();
46
47
printf(
"Joystick readings: %.03f, %.03f, %.03f ("
, x, y, z);
48
for
(
unsigned
b = 0; b < buttons.size(); b++)
49
printf(
"B%u:%c "
, b, buttons[b] ?
'X'
:
'-'
);
50
printf(
") [Query %uus] \r"
, (
unsigned
)(t * 1e6));
51
52
fflush(stdout);
53
}
54
else
55
{
56
printf(
57
"Error reading from joystick, please connect one to the "
58
"system...\r"
);
59
}
60
61
std::this_thread::sleep_for(20ms);
62
}
63
}
64
65
int
main
()
66
{
67
try
68
{
69
TestJoystick
();
70
71
return
0;
72
}
73
catch
(
const
std::exception& e)
74
{
75
std::cerr <<
"MRPT error: "
<<
mrpt::exception_to_str
(e) << std::endl;
76
return
-1;
77
}
78
}
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition:
os.cpp:392
os.h
exceptions.h
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition:
system/CTicTac.h:17
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition:
C2DRangeFinderAbstract.h:19
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition:
BaseAppDataSource.h:15
mrpt::hwdrivers::CJoystick
Access to joysticks and gamepads (read buttons and position), and request number of joysticks in the ...
Definition:
CJoystick.h:22
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition:
CTicTac.cpp:86
main
int main()
Definition:
vision_stereo_rectify/test.cpp:78
CJoystick.h
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition:
CTicTac.cpp:75
CTicTac.h
TestJoystick
void TestJoystick()
Definition:
vision_stereo_rectify/test.cpp:27
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition:
exceptions.cpp:59
mrpt::hwdrivers::CJoystick::getJoystickPosition
bool getJoystickPosition(int nJoy, float &x, float &y, float &z, std::vector< bool > &buttons, int *raw_x_pos=nullptr, int *raw_y_pos=nullptr, int *raw_z_pos=nullptr)
Gets joystick information.
Definition:
CJoystick.cpp:106
mrpt::system
Definition:
backtrace.h:14
Page generated by
Doxygen 1.8.17
for MRPT 2.0.3 at Fri May 15 23:51:15 UTC 2020