yarp-devices
Loading...
Searching...
No Matches
PhidgetSpatial.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __PHIDGET_SPATIAL_HPP__
4#define __PHIDGET_SPATIAL_HPP__
5
6#include <mutex>
7
8#include <yarp/conf/version.h>
9
10#include <yarp/dev/DeviceDriver.h>
11#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
12
13#include <phidget21.h>
14
15constexpr auto NUM_SENSORS = 1;
16
17#if YARP_VERSION_COMPARE(>=, 4, 1, 0) || defined(YARP_NEXT)
18#define CHECK_SENSOR(n) do { if ((n) < 0 || (n) > NUM_SENSORS - 1) return yarp::dev::ReturnValue_error_input_out_of_bounds; } while (0)
19#else
20#define CHECK_SENSOR(n) do { if ((n) < 0 || (n) > NUM_SENSORS - 1) return false; } while (0)
21#endif
22
33class PhidgetSpatial : public yarp::dev::DeviceDriver,
34 public yarp::dev::IThreeAxisLinearAccelerometers,
35 public yarp::dev::IThreeAxisGyroscopes,
36 public yarp::dev::IThreeAxisMagnetometers
37{
38public:
39 // -------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp --------
40 bool open(yarp::os::Searchable & config) override;
41 bool close() override;
42
43 // --------- IThreeAxisLinearAccelerometers declarations. Implementation in IThreeAxisLinearAccelerometersImpl.cpp ---------
44#if YARP_VERSION_COMPARE(>=, 4, 1, 0) || defined(YARP_NEXT)
45 yarp::dev::ReturnValue getNrOfThreeAxisLinearAccelerometers(std::size_t & num) const override;
46 yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(std::size_t sens_index) const override;
47 yarp::dev::ReturnValue getThreeAxisLinearAccelerometerName(std::size_t sens_index, std::string & name) const override;
48 yarp::dev::ReturnValue getThreeAxisLinearAccelerometerFrameName(std::size_t sens_index, std::string & frameName) const override;
49 yarp::dev::ReturnValue getThreeAxisLinearAccelerometerMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
50#else
51 std::size_t getNrOfThreeAxisLinearAccelerometers() const override;
52 yarp::dev::MAS_status getThreeAxisLinearAccelerometerStatus(std::size_t sens_index) const override;
53 bool getThreeAxisLinearAccelerometerName(std::size_t sens_index, std::string & name) const override;
54 bool getThreeAxisLinearAccelerometerFrameName(std::size_t sens_index, std::string & frameName) const override;
55 bool getThreeAxisLinearAccelerometerMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
56#endif
57
58 // --------- IThreeAxisGyroscopes declarations. Implementation in IThreeAxisGyroscopesImpl.cpp ---------
59#if YARP_VERSION_COMPARE(>=, 4, 1, 0) || defined(YARP_NEXT)
60 yarp::dev::ReturnValue getNrOfThreeAxisGyroscopes(std::size_t & num) const override;
61 yarp::dev::MAS_status getThreeAxisGyroscopeStatus(std::size_t sens_index) const override;
62 yarp::dev::ReturnValue getThreeAxisGyroscopeName(std::size_t sens_index, std::string & name) const override;
63 yarp::dev::ReturnValue getThreeAxisGyroscopeFrameName(std::size_t sens_index, std::string & frameName) const override;
64 yarp::dev::ReturnValue getThreeAxisGyroscopeMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
65#else
66 std::size_t getNrOfThreeAxisGyroscopes() const override;
67 yarp::dev::MAS_status getThreeAxisGyroscopeStatus(std::size_t sens_index) const override;
68 bool getThreeAxisGyroscopeName(std::size_t sens_index, std::string & name) const override;
69 bool getThreeAxisGyroscopeFrameName(std::size_t sens_index, std::string & frameName) const override;
70 bool getThreeAxisGyroscopeMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
71#endif
72
73 // --------- IThreeAxisMagnetometers declarations. Implementation in IThreeAxisMagnetometersImpl.cpp ---------
74#if YARP_VERSION_COMPARE(>=, 4, 1, 0) || defined(YARP_NEXT)
75 yarp::dev::ReturnValue getNrOfThreeAxisMagnetometers(std::size_t & num) const override;
76 yarp::dev::MAS_status getThreeAxisMagnetometerStatus(std::size_t sens_index) const override;
77 yarp::dev::ReturnValue getThreeAxisMagnetometerName(std::size_t sens_index, std::string & name) const override;
78 yarp::dev::ReturnValue getThreeAxisMagnetometerFrameName(std::size_t sens_index, std::string & frameName) const override;
79 yarp::dev::ReturnValue getThreeAxisMagnetometerMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
80#else
81 std::size_t getNrOfThreeAxisMagnetometers() const override;
82 yarp::dev::MAS_status getThreeAxisMagnetometerStatus(std::size_t sens_index) const override;
83 bool getThreeAxisMagnetometerName(std::size_t sens_index, std::string & name) const override;
84 bool getThreeAxisMagnetometerFrameName(std::size_t sens_index, std::string & frameName) const override;
85 bool getThreeAxisMagnetometerMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
86#endif
87
88private:
89 // -- Helper Funcion declarations. Implementation in PhidgetSpatial.cpp --
90
92 // The following six functions have been extracted and modified from the - Spatial simple -
93 // example ((creates an Spatial handle, hooks the event handlers, and then waits for an
94 // encoder is attached. Once it is attached, the program will wait for user input so that
95 // we can see the event data on the screen when using the encoder. Legal info:
96 // Copyright 2008 Phidgets Inc. All rights reserved.
97 // This work is licensed under the Creative Commons Attribution 2.5 Canada License.
98 // view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
99 static int AttachHandler(CPhidgetHandle ENC, void * userptr);
100 static int DetachHandler(CPhidgetHandle ENC, void * userptr);
101 static int ErrorHandler(CPhidgetHandle ENC, void * userptr, int ErrorCode, const char * Description);
102 static int SpatialDataHandler(CPhidgetSpatialHandle spatial, void * userptr, CPhidgetSpatial_SpatialEventDataHandle * data, int count);
103 static int display_properties(CPhidgetSpatialHandle phid);
105
106 CPhidgetSpatialHandle hSpatial0;
107 mutable std::mutex mtx;
108
109 double acceleration[3];
110 double angularRate[3];
111 double magneticField[3];
112
113 double timestamp {0.0};
114};
115
116#endif // __PHIDGET_SPATIAL_HPP__
Implementation of a Phidgets device.
Definition PhidgetSpatial.hpp:37