gyroscope.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Open Source Robotics Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <functional>
31 #include <memory>
32 #include <stdexcept>
33 #include <string>
34 
35 #include <libphidget22/phidget22.h>
36 
37 #include "phidgets_api/gyroscope.h"
38 #include "phidgets_api/phidget22.h"
39 
40 namespace phidgets {
41 
42 Gyroscope::Gyroscope(int32_t serial_number, int hub_port,
43  bool is_hub_port_device,
44  std::function<void(const double[3], double)> data_handler)
45  : data_handler_(data_handler)
46 {
47  PhidgetReturnCode ret = PhidgetGyroscope_create(&gyro_handle_);
48  if (ret != EPHIDGET_OK)
49  {
50  throw Phidget22Error("Failed to create Gyroscope handle", ret);
51  }
52 
54  reinterpret_cast<PhidgetHandle>(gyro_handle_), serial_number, hub_port,
55  is_hub_port_device, 0);
56 
57  ret = PhidgetGyroscope_setOnAngularRateUpdateHandler(gyro_handle_,
58  DataHandler, this);
59  if (ret != EPHIDGET_OK)
60  {
61  throw Phidget22Error("Failed to set Gyroscope update handler", ret);
62  }
63 }
64 
66 {
67  PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(gyro_handle_);
68  helpers::closeAndDelete(&handle);
69 }
70 
71 void Gyroscope::zero() const
72 {
73  PhidgetReturnCode ret = PhidgetGyroscope_zero(gyro_handle_);
74  if (ret != EPHIDGET_OK)
75  {
76  throw Phidget22Error("Failed to calibrate Gyroscope", ret);
77  }
78 }
79 
80 void Gyroscope::getAngularRate(double &x, double &y, double &z,
81  double &timestamp) const
82 {
83  double angular_rate[3];
84  PhidgetReturnCode ret =
85  PhidgetGyroscope_getAngularRate(gyro_handle_, &angular_rate);
86  if (ret != EPHIDGET_OK)
87  {
88  throw Phidget22Error("Failed to get angular rate from gyroscope", ret);
89  }
90 
91  x = angular_rate[0];
92  y = angular_rate[1];
93  z = angular_rate[2];
94 
95  double ts;
96  ret = PhidgetGyroscope_getTimestamp(gyro_handle_, &ts);
97  if (ret != EPHIDGET_OK)
98  {
99  throw Phidget22Error("Failed to get timestamp from gyroscope", ret);
100  }
101 
102  timestamp = ts;
103 }
104 
105 void Gyroscope::setDataInterval(uint32_t interval_ms) const
106 {
107  PhidgetReturnCode ret =
108  PhidgetGyroscope_setDataInterval(gyro_handle_, interval_ms);
109  if (ret != EPHIDGET_OK)
110  {
111  throw Phidget22Error("Failed to set data interval", ret);
112  }
113 }
114 
115 void Gyroscope::dataHandler(const double angular_rate[3],
116  double timestamp) const
117 {
118  data_handler_(angular_rate, timestamp);
119 }
120 
121 void Gyroscope::DataHandler(PhidgetGyroscopeHandle /* input_handle */,
122  void *ctx, const double angular_rate[3],
123  double timestamp)
124 {
125  ((Gyroscope *)ctx)->dataHandler(angular_rate, timestamp);
126 }
127 
128 } // namespace phidgets
phidgets
Definition: accelerometer.h:39
phidgets::Gyroscope::getAngularRate
void getAngularRate(double &x, double &y, double &z, double &timestamp) const
Definition: gyroscope.cpp:80
phidgets::Gyroscope::~Gyroscope
~Gyroscope()
Definition: gyroscope.cpp:65
phidgets::Gyroscope
Definition: gyroscope.h:41
phidgets::Gyroscope::setDataInterval
void setDataInterval(uint32_t interval_ms) const
Definition: gyroscope.cpp:105
phidgets::Gyroscope::DataHandler
static void DataHandler(PhidgetGyroscopeHandle input_handle, void *ctx, const double angular_rate[3], double timestamp)
Definition: gyroscope.cpp:121
phidgets::helpers::openWaitForAttachment
void openWaitForAttachment(PhidgetHandle handle, int32_t serial_number, int hub_port, bool is_hub_port_device, int channel)
Definition: phidget22.cpp:57
phidgets::Gyroscope::zero
void zero() const
Definition: gyroscope.cpp:71
phidgets::helpers::closeAndDelete
void closeAndDelete(PhidgetHandle *handle) noexcept
Definition: phidget22.cpp:93
phidgets::Gyroscope::Gyroscope
Gyroscope(int32_t serial_number, int hub_port, bool is_hub_port_device, std::function< void(const double[3], double)> data_handler)
Definition: gyroscope.cpp:42
phidget22.h
phidgets::Phidget22Error
Definition: phidget22.h:46
gyroscope.h
phidgets::Gyroscope::dataHandler
void dataHandler(const double angular_rate[3], double timestamp) const
Definition: gyroscope.cpp:115
phidgets::Gyroscope::data_handler_
std::function< void(const double[3], double)> data_handler_
Definition: gyroscope.h:62
phidgets::Gyroscope::gyro_handle_
PhidgetGyroscopeHandle gyro_handle_
Definition: gyroscope.h:63


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Sun May 11 2025 02:20:27