MRPT  1.9.9
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-2019, 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>
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 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
GLdouble GLdouble t
Definition: glext.h:3695
GLdouble GLdouble z
Definition: glext.h:3879
A high-performance stopwatch, with typical resolution of nanoseconds.
Contains classes for various device interfaces.
STL namespace.
GLubyte GLubyte b
Definition: glext.h:6372
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
void TestJoystick()
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
GLenum GLint GLint y
Definition: glext.h:3542
GLenum GLint x
Definition: glext.h:3542
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
Access to joysticks and gamepads (read buttons and position), and request number of joysticks in the ...
Definition: CJoystick.h:22



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019