12 #define DEFAULT_THREAD_PERIOD 0.010
27 for (
size_t i = 0; i < b.
size(); i++)
47 countGetters.insert(std::make_pair(
VOCAB_BUTTON, &IJoypadController::getButtonCount));
48 countGetters.insert(std::make_pair(
VOCAB_HAT, &IJoypadController::getHatCount));
49 countGetters.insert(std::make_pair(
VOCAB_TRACKBALL, &IJoypadController::getTrackballCount));
50 countGetters.insert(std::make_pair(
VOCAB_AXIS, &IJoypadController::getAxisCount));
51 countGetters.insert(std::make_pair(
VOCAB_STICK, &IJoypadController::getStickCount));
52 countGetters.insert(std::make_pair(
VOCAB_TOUCH, &IJoypadController::getTouchSurfaceCount));
76 if(countGetters.find(toGet) != countGetters.end())
79 getcountmethod getter;
80 getter = countGetters[toGet];
81 if((device->*getter)(count))
144 for(
size_t i = 0; i < frame.
size(); ++i)
157 unsigned int dofCount;
177 for(
size_t i = 0; i < pos.
size(); ++i)
194 for(
size_t i = 0; i < axes.
size(); ++i)
226 m_subDeviceOwned(nullptr),
227 m_isSubdeviceOwned(false),
228 m_separatePorts(false),
239 delete m_subDeviceOwned;
241 m_subDeviceOwned =
nullptr;
247 if(params.
check(
"help"))
249 yCInfo(JOYPADCONTROLSERVER)
251 <<
"period - refresh period of the broadcasted values in ms.. default" <<
DEFAULT_THREAD_PERIOD * 1000 <<
"\n"
252 <<
"use_separate_ports - set it to 1 to use separate ports (buttons, axes, trackballs, hats) and 0 to stream all in one single port\n"
253 <<
"name - Prefix name of the ports opened by the JoypadControlServer, e.g. /robotName/joypad\n"
254 <<
"subdevice - name of the subdevice to open\n"
255 <<
"profile - print the joypad data for debugging purpose";
258 std::string rootName;
259 if (!params.
check(
"period",
"refresh period of the broadcasted values in ms"))
265 m_period = params.
find(
"period").
asInt32() / 1000.0;
268 m_profile = params.
check(
"profile");
270 if(params.
check(
"use_separate_ports"))
272 m_separatePorts = params.
find(
"use_separate_ports").
asBool();
275 yCError(JOYPADCONTROLSERVER) <<
"Single port mode not supported at the moment";
281 yCError(JOYPADCONTROLSERVER) <<
"Missing use_separate_ports in configuration";
285 rootName = params.
check(
"name",
Value(
"/"),
"starting '/' if needed.").asString();
287 if (!params.
check(
"name",
"Prefix name of the ports opened by the JoypadControlServer."))
289 yCError(JOYPADCONTROLSERVER) <<
"Missing 'name' parameter. Check you configuration file; it must be like:";
290 yCError(JOYPADCONTROLSERVER) <<
" name: Prefix name of the ports opened by the JoypadControlServer, e.g. /robotName/joypad";
295 m_rpcPortName = rootName +
"/rpc:i";
296 m_portButtons.name = rootName +
"/buttons:o";
297 m_portAxis.name = rootName +
"/axis:o";
298 m_portStick.name = rootName +
"/stick:o";
299 m_portTouch.name = rootName +
"/touch:o";
300 m_portTrackball.name = rootName +
"/trackball:o";
301 m_portHats.name = rootName +
"/hat:o";
306 if(params.
check(
"subdevice"))
308 m_isSubdeviceOwned=
true;
309 if(!openAndAttachSubDevice(params))
311 yCError(JOYPADCONTROLSERVER) <<
"Error while opening subdevice";
317 m_isSubdeviceOwned=
false;
322 bool JoypadControlServer::openAndAttachSubDevice(
Searchable& prop)
329 p.setMonitor(prop.getMonitor(),
"subdevice");
334 m_subDeviceOwned->
open(p);
336 if (!m_subDeviceOwned->
isValid())
338 yCError(JOYPADCONTROLSERVER) <<
"Opening subdevice... FAILED";
341 m_isSubdeviceOwned =
true;
342 if (!
attach(m_subDeviceOwned)) {
348 yCError(JOYPADCONTROLSERVER) <<
"Error configuring interfaces for parsers";
353 PeriodicThread::setPeriod(m_period);
354 return PeriodicThread::start();
360 poly->
view(m_device);
363 if(m_device ==
nullptr)
365 yCError(JOYPADCONTROLSERVER) <<
"Attached device has no valid IJoypadController interface.";
375 yCError(JOYPADCONTROLSERVER) <<
"Attached device has no valid IJoystickController interface.";
399 bool JoypadControlServer::openPorts()
406 if(!m_rpcPort.
open(m_rpcPortName))
408 yCError(JOYPADCONTROLSERVER) <<
"Unable to open rpc Port" << m_rpcPortName.c_str();
528 std::vector<solver> getters;
530 getters.emplace_back(&IJoypadController::getAxisCount, &m_portAxis );
531 getters.emplace_back(&IJoypadController::getButtonCount, &m_portButtons );
532 getters.emplace_back(&IJoypadController::getStickCount, &m_portStick );
533 getters.emplace_back(&IJoypadController::getTouchSurfaceCount, &m_portTouch );
534 getters.emplace_back(&IJoypadController::getTrackballCount, &m_portTrackball);
535 getters.emplace_back(&IJoypadController::getHatCount, &m_portHats );
537 for(
auto& getter : getters)
539 if((m_device->*(getter.getter))(getter.port->count))
541 if(getter.port->count == 0)
543 getter.port->
valid =
false;
547 getter.port->contactable->open(getter.port->name);
548 getter.port->valid =
true;
566 void JoypadControlServer::profile()
573 for(
unsigned int i = 0; i < count; ++i)
579 yCInfo(JOYPADCONTROLSERVER) << message;
583 for(
unsigned int i = 0; i < count; ++i)
586 m_device->
getHat(i, data);
589 yCInfo(JOYPADCONTROLSERVER) << message;
591 message =
"Buttons: ";
593 for(
unsigned int i = 0; i < count; ++i)
599 yCInfo(JOYPADCONTROLSERVER) << message;
603 for(
unsigned int i = 0; i < count; ++i)
608 for (
size_t j = 0; j < data.
size(); ++j)
615 yCInfo(JOYPADCONTROLSERVER) << message;
617 message =
"trackball: ";
619 for(
unsigned int i = 0; i < count; ++i)
624 for (
size_t j = 0; j < data.
size(); ++j)
631 message =
"touch Surface: ";
633 for(
unsigned int i = 0; i < count; ++i)
638 for (
size_t j = 0; j < data.
size(); ++j)
644 yCInfo(JOYPADCONTROLSERVER) << message;
651 if (m_portButtons.valid)
655 Vector& b = m_portButtons.prepare();
657 for(
size_t i = 0; i < m_portButtons.count; ++i)
668 m_portButtons.write();
672 if (m_portHats.valid)
679 for(
size_t i = 0; i < m_portHats.count; ++i)
682 if(!m_device->
getHat(i, v))
694 if (m_portAxis.valid)
699 Vector& b = m_portAxis.prepare();
701 for(
size_t i = 0; i < m_portAxis.count; ++i)
706 yCError(JOYPADCONTROLSERVER) <<
"Cannot get axis with id" << i;
717 if (m_portTrackball.valid)
722 Vector& b = m_portTrackball.prepare();
724 for(
size_t i = 0; i < m_portTrackball.count; ++i)
729 yCError(JOYPADCONTROLSERVER) <<
"Cannot get axis with id" << i;
736 m_portTrackball.write();
740 if (m_portStick.valid)
744 Vector& b = m_portStick.prepare();
746 for(
size_t i = 0; i < m_portStick.count; ++i)
749 unsigned int dofCount;
762 if (m_portTouch.valid)
766 Vector& b = m_portTouch.prepare();
768 for(
unsigned int i = 0; i < m_portTouch.count; ++i)
802 yCError(JOYPADCONTROLSERVER) <<
"Cannot attach more than one device";
807 if(p[0]->key ==
"IJoypadController")
809 yCInfo(JOYPADCONTROLSERVER) <<
"Good name!";
813 yCInfo(JOYPADCONTROLSERVER) <<
"Bad name!";
816 if (!Idevice2attach->
isValid())
818 yCError(JOYPADCONTROLSERVER) <<
"Device " << p[0]->key <<
" to attach to is not valid ... cannot proceed";
822 Idevice2attach->
view(m_device);
827 PeriodicThread::setPeriod(m_period);
828 if (!PeriodicThread::start()) {
843 if (m_isSubdeviceOwned) {
856 if(m_isSubdeviceOwned)
858 if (m_subDeviceOwned) {
859 m_subDeviceOwned->
close();
862 m_subDeviceOwned =
nullptr;
864 m_isSubdeviceOwned =
false;
868 std::vector<JoypadControl::LoopablePort*> portv;
869 portv.push_back(&m_portButtons);
870 portv.push_back(&m_portAxis);
871 portv.push_back(&m_portStick);
872 portv.push_back(&m_portTouch);
873 portv.push_back(&m_portTrackball);
874 portv.push_back(&m_portHats);
879 p->contactable->close();
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_GET
constexpr yarp::conf::vocab32_t VOCAB_FAILED
constexpr yarp::conf::vocab32_t VOCAB_VALUE
constexpr yarp::conf::vocab32_t VOCAB_COUNT
constexpr yarp::conf::vocab32_t VOCAB_BUTTON
constexpr yarp::conf::vocab32_t VOCAB_IJOYPADCTRL
constexpr yarp::conf::vocab32_t VOCAB_TOUCH
constexpr yarp::conf::vocab32_t VOCAB_HAT
constexpr yarp::conf::vocab32_t VOCAB_TRACKBALL
constexpr yarp::conf::vocab32_t VOCAB_CARTESIAN
constexpr yarp::conf::vocab32_t VOCAB_STICKDOF
constexpr yarp::conf::vocab32_t VOCAB_AXIS
constexpr yarp::conf::vocab32_t VOCAB_STICK
#define DEFAULT_THREAD_PERIOD
void cat(Vector &a, const Vector &b)
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
void threadRelease() override
Release method.
~JoypadControlServer() override
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
bool close() override
Close the DeviceDriver.
bool detach() override
Detach the object (you must have first called attach).
void run() override
Loop function.
bool threadInit() override
Initialization method.
bool detachAll() override
Detach the object (you must have first called attach).
bool configure(yarp::dev::IJoypadController *interface)
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool view(T *&x)
Get an interface to the device driver.
virtual bool getHatCount(unsigned int &Hat_count)=0
Get number of hats.
virtual bool getTrackballCount(unsigned int &Trackball_count)=0
Get number of trackballs.
virtual bool getTouchSurfaceCount(unsigned int &touch_count)=0
Get the number of touch surface.
virtual bool getButton(unsigned int button_id, float &value)=0
Get the value of a button.
virtual bool getAxisCount(unsigned int &axis_count)=0
Get number of axes.
virtual bool getAxis(unsigned int axis_id, double &value)=0
Get the value of an axis if present, return false otherwise.
virtual bool getStickCount(unsigned int &stick_count)=0
Get the number of the sticks.
virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value)=0
Get the axes change of a Trackball.
virtual bool getStick(unsigned int stick_id, yarp::sig::Vector &value, JoypadCtrl_coordinateMode coordinate_mode)=0
Get the value of a stick if present, return false otherwise.
virtual bool getStickDoF(unsigned int stick_id, unsigned int &DoF)=0
Get the Degree Of Freedom count for desired stick.
virtual bool getHat(unsigned int hat_id, unsigned char &value)=0
Get the value of an Hat.
virtual bool getButtonCount(unsigned int &button_count)=0
Get number of buttons.
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector &value)=0
Get the value of a touch if present, return false otherwise.
A container for a device driver.
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
bool open(const std::string &txt)
Construct and configure a device by its common name.
A simple collection of objects that can be described and transmitted in a portable way.
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
An abstraction for a periodic thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
void setReader(PortReader &reader) override
Set an external reader for port data.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
virtual bool asBool() const
Get boolean value.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual bool isInt32() const
Checks if value is a 32-bit integer.
virtual bool isVocab32() const
Checks if value is a vocabulary identifier.
virtual std::string asString() const
Get string value.
void push_back(const T &elem)
Push a new element in the vector: size is changed.
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string to_string(IntegerType x)
An interface for the device drivers.
An interface to the operating system, including Port based communication.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
The main, catch-all namespace for YARP.