-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Phidgets Current sensors #148
base: noetic
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2019, Open Source Robotics Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef PHIDGETS_API_CURRENT_INPUT_H | ||
#define PHIDGETS_API_CURRENT_INPUT_H | ||
|
||
#include <functional> | ||
|
||
#include <libphidget22/phidget22.h> | ||
|
||
#include "phidgets_api/phidget22.h" | ||
|
||
namespace phidgets { | ||
|
||
class CurrentInput final | ||
{ | ||
public: | ||
PHIDGET22_NO_COPY_NO_MOVE_NO_ASSIGN(CurrentInput) | ||
|
||
explicit CurrentInput(int32_t serial_number, int hub_port, | ||
bool is_hub_port_device, int channel, | ||
std::function<void(int, double)> input_handler); | ||
|
||
~CurrentInput(); | ||
|
||
double getSensorValue() const; | ||
|
||
void setDataInterval(uint32_t data_interval_ms) const; | ||
|
||
void currentChangeHandler(double sensorValue) const; | ||
|
||
private: | ||
int channel_; | ||
std::function<void(int, double)> input_handler_; | ||
PhidgetCurrentInputHandle ci_handle_; | ||
|
||
static void CurrentChangeHandler(PhidgetCurrentInputHandle input_handle, | ||
void *ctx, double sensorValue); | ||
}; | ||
|
||
} // namespace phidgets | ||
|
||
#endif // PHIDGETS_API_CURRENT_INPUT_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) 2019, Open Source Robotics Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#ifndef PHIDGETS_API_CURRENT_INPUTS_H | ||
#define PHIDGETS_API_CURRENT_INPUTS_H | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "phidgets_api/current_input.h" | ||
#include "phidgets_api/phidget22.h" | ||
|
||
namespace phidgets { | ||
|
||
class CurrentInputs final | ||
{ | ||
public: | ||
PHIDGET22_NO_COPY_NO_MOVE_NO_ASSIGN(CurrentInputs) | ||
|
||
explicit CurrentInputs(int32_t serial_number, int hub_port, | ||
bool is_hub_port_device, | ||
std::function<void(int, double)> input_handler); | ||
|
||
~CurrentInputs(); | ||
|
||
uint32_t getInputCount() const noexcept; | ||
|
||
double getSensorValue(int index) const; | ||
|
||
void setDataInterval(int index, uint32_t data_interval_ms) const; | ||
|
||
private: | ||
uint32_t input_count_; | ||
std::vector<std::unique_ptr<CurrentInput>> cis_; | ||
}; | ||
|
||
} // namespace phidgets | ||
|
||
#endif // PHIDGETS_API_CURRENT_INPUTS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2019, Open Source Robotics Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include <libphidget22/phidget22.h> | ||
|
||
#include "phidgets_api/current_input.h" | ||
#include "phidgets_api/phidget22.h" | ||
|
||
namespace phidgets { | ||
|
||
CurrentInput::CurrentInput(int32_t serial_number, int hub_port, | ||
bool is_hub_port_device, int channel, | ||
std::function<void(int, double)> input_handler) | ||
: channel_(channel), input_handler_(input_handler) | ||
{ | ||
PhidgetReturnCode ret = PhidgetCurrentInput_create(&ci_handle_); | ||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error( | ||
"Failed to create CurrentInput handle for channel " + | ||
std::to_string(channel), | ||
ret); | ||
} | ||
|
||
helpers::openWaitForAttachment(reinterpret_cast<PhidgetHandle>(ci_handle_), | ||
serial_number, hub_port, is_hub_port_device, | ||
channel); | ||
|
||
ret = PhidgetCurrentInput_setOnCurrentChangeHandler( | ||
ci_handle_, CurrentChangeHandler, this); | ||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error( | ||
"Failed to set change handler for CurrentInput channel " + | ||
std::to_string(channel), | ||
ret); | ||
} | ||
} | ||
|
||
CurrentInput::~CurrentInput() | ||
{ | ||
PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(ci_handle_); | ||
helpers::closeAndDelete(&handle); | ||
} | ||
|
||
double CurrentInput::getSensorValue() const | ||
{ | ||
double sensor_value; | ||
PhidgetReturnCode ret = | ||
PhidgetCurrentInput_getCurrent(ci_handle_, &sensor_value); | ||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error("Failed to get analog sensor value", ret); | ||
} | ||
|
||
return sensor_value; | ||
} | ||
|
||
void CurrentInput::setDataInterval(uint32_t data_interval_ms) const | ||
{ | ||
PhidgetReturnCode ret = | ||
PhidgetCurrentInput_setDataInterval(ci_handle_, data_interval_ms); | ||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error("Failed to set analog data interval", ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. analog -> current |
||
} | ||
} | ||
|
||
void CurrentInput::currentChangeHandler(double sensorValue) const | ||
{ | ||
input_handler_(channel_, sensorValue); | ||
} | ||
|
||
void CurrentInput::CurrentChangeHandler( | ||
PhidgetCurrentInputHandle /* input_handle */, void *ctx, double sensorValue) | ||
{ | ||
((CurrentInput *)ctx)->currentChangeHandler(sensorValue); | ||
} | ||
|
||
} // namespace phidgets |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2019, Open Source Robotics Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include <libphidget22/phidget22.h> | ||
|
||
#include "phidgets_api/current_input.h" | ||
#include "phidgets_api/current_inputs.h" | ||
#include "phidgets_api/phidget22.h" | ||
|
||
namespace phidgets { | ||
|
||
CurrentInputs::CurrentInputs(int32_t serial_number, int hub_port, | ||
bool is_hub_port_device, | ||
std::function<void(int, double)> input_handler) | ||
{ | ||
PhidgetReturnCode ret; | ||
|
||
PhidgetCurrentInputHandle ai_handle; | ||
|
||
ret = PhidgetCurrentInput_create(&ai_handle); | ||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error( | ||
"Failed to create CurrentInput handle for determining channel " | ||
"count", | ||
ret); | ||
} | ||
|
||
PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(ai_handle); | ||
|
||
helpers::openWaitForAttachment(handle, serial_number, hub_port, | ||
is_hub_port_device, 0); | ||
|
||
ret = Phidget_getDeviceChannelCount(handle, PHIDCHCLASS_CURRENTINPUT, | ||
&input_count_); | ||
|
||
helpers::closeAndDelete(&handle); | ||
|
||
if (ret != EPHIDGET_OK) | ||
{ | ||
throw Phidget22Error("Failed to get CurrentInput device channel count", | ||
ret); | ||
} | ||
|
||
cis_.resize(input_count_); | ||
for (uint32_t i = 0; i < input_count_; ++i) | ||
{ | ||
cis_[i] = std::make_unique<CurrentInput>( | ||
serial_number, hub_port, is_hub_port_device, i, input_handler); | ||
} | ||
} | ||
|
||
CurrentInputs::~CurrentInputs() | ||
{ | ||
} | ||
|
||
uint32_t CurrentInputs::getInputCount() const noexcept | ||
{ | ||
return input_count_; | ||
} | ||
|
||
double CurrentInputs::getSensorValue(int index) const | ||
{ | ||
return cis_.at(index)->getSensorValue(); | ||
} | ||
|
||
void CurrentInputs::setDataInterval(int index, uint32_t data_interval_ms) const | ||
{ | ||
cis_.at(index)->setDataInterval(data_interval_ms); | ||
} | ||
|
||
} // namespace phidgets |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||||||||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||
Changelog for package phidgets_current_inputs | ||||||||||||||||||||||||||||||||||||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.5 (2022-02-17) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.4 (2021-10-22) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.3 (2021-09-29) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.2 (2021-03-09) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.1 (2020-06-04) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
1.0.0 (2020-06-03) | ||||||||||||||||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+5
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, it is better if you don't add a CHANGELOG at all. We have automated tools to do that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
analog -> current