|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
15 #define DEFAULT_THREAD_PERIOD 0.010 //s
31 for (
size_t i = 0; i < b.
size(); i++)
51 countGetters.insert(make_pair(
VOCAB_BUTTON, &IJoypadController::getButtonCount));
52 countGetters.insert(make_pair(
VOCAB_HAT, &IJoypadController::getHatCount));
53 countGetters.insert(make_pair(
VOCAB_TRACKBALL, &IJoypadController::getTrackballCount));
54 countGetters.insert(make_pair(
VOCAB_AXIS, &IJoypadController::getAxisCount));
55 countGetters.insert(make_pair(
VOCAB_STICK, &IJoypadController::getStickCount));
56 countGetters.insert(make_pair(
VOCAB_TOUCH, &IJoypadController::getTouchSurfaceCount));
80 if(countGetters.find(toGet) != countGetters.end())
83 getcountmethod getter;
84 getter = countGetters[toGet];
85 if((device->*getter)(count))
148 for(
size_t i = 0; i < frame.
size(); ++i)
161 unsigned int dofCount;
181 for(
size_t i = 0; i < pos.
size(); ++i)
198 for(
size_t i = 0; i < axes.
size(); ++i)
230 m_subDeviceOwned(nullptr),
231 m_isSubdeviceOwned(false),
232 m_separatePorts(false),
243 delete m_subDeviceOwned;
245 m_subDeviceOwned =
nullptr;
251 if(params.
check(
"help"))
253 yCInfo(JOYPADCONTROLSERVER)
255 <<
"period - refresh period of the broadcasted values in ms.. default" <<
DEFAULT_THREAD_PERIOD * 1000 <<
"\n"
256 <<
"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"
257 <<
"name - Prefix name of the ports opened by the JoypadControlServer, e.g. /robotName/joypad\n"
258 <<
"subdevice - name of the subdevice to open\n"
259 <<
"profile - print the joypad data for debugging purpose";
262 std::string rootName;
263 if (!params.
check(
"period",
"refresh period of the broadcasted values in ms"))
269 m_period = params.
find(
"period").
asInt32() / 1000.0;
272 m_profile = params.
check(
"profile");
274 if(params.
check(
"use_separate_ports"))
276 m_separatePorts = params.
find(
"use_separate_ports").
asBool();
279 yCError(JOYPADCONTROLSERVER) <<
"Single port mode not supported at the moment";
285 yCError(JOYPADCONTROLSERVER) <<
"Missing use_separate_ports in configuration";
289 rootName = params.
check(
"name",
Value(
"/"),
"starting '/' if needed.").asString();
291 if (!params.
check(
"name",
"Prefix name of the ports opened by the JoypadControlServer."))
293 yCError(JOYPADCONTROLSERVER) <<
"Missing 'name' parameter. Check you configuration file; it must be like:";
294 yCError(JOYPADCONTROLSERVER) <<
" name: Prefix name of the ports opened by the JoypadControlServer, e.g. /robotName/joypad";
299 m_rpcPortName = rootName +
"/rpc:i";
300 m_portButtons.name = rootName +
"/buttons:o";
301 m_portAxis.name = rootName +
"/axis:o";
302 m_portStick.name = rootName +
"/stick:o";
303 m_portTouch.name = rootName +
"/touch:o";
304 m_portTrackball.name = rootName +
"/trackball:o";
305 m_portHats.name = rootName +
"/hat:o";
310 if(params.
check(
"subdevice"))
312 m_isSubdeviceOwned=
true;
313 if(!openAndAttachSubDevice(params))
315 yCError(JOYPADCONTROLSERVER) <<
"Error while opening subdevice";
321 m_isSubdeviceOwned=
false;
326 bool JoypadControlServer::openAndAttachSubDevice(
Searchable& prop)
333 p.setMonitor(prop.getMonitor(),
"subdevice");
338 m_subDeviceOwned->
open(p);
340 if (!m_subDeviceOwned->
isValid())
342 yCError(JOYPADCONTROLSERVER) <<
"Opening subdevice... FAILED";
345 m_isSubdeviceOwned =
true;
346 if(!
attach(m_subDeviceOwned))
351 yCError(JOYPADCONTROLSERVER) <<
"Error configuring interfaces for parsers";
356 PeriodicThread::setPeriod(m_period);
357 return PeriodicThread::start();
363 poly->
view(m_device);
365 if(m_device ==
nullptr)
367 yCError(JOYPADCONTROLSERVER) <<
"Attached device has no valid IJoypadController interface.";
377 yCError(JOYPADCONTROLSERVER) <<
"Attached device has no valid IJoystickController interface.";
401 bool JoypadControlServer::openPorts()
408 if(!m_rpcPort.
open(m_rpcPortName))
410 yCError(JOYPADCONTROLSERVER) <<
"Unable to open rpc Port" << m_rpcPortName.c_str();
530 vector<solver> getters;
532 getters.emplace_back(&IJoypadController::getAxisCount, &m_portAxis );
533 getters.emplace_back(&IJoypadController::getButtonCount, &m_portButtons );
534 getters.emplace_back(&IJoypadController::getStickCount, &m_portStick );
535 getters.emplace_back(&IJoypadController::getTouchSurfaceCount, &m_portTouch );
536 getters.emplace_back(&IJoypadController::getTrackballCount, &m_portTrackball);
537 getters.emplace_back(&IJoypadController::getHatCount, &m_portHats );
539 for(
auto& getter : getters)
541 if((m_device->*(getter.getter))(getter.port->count))
543 if(getter.port->count == 0)
545 getter.port->valid =
false;
549 getter.port->contactable->open(getter.port->name);
550 getter.port->valid =
true;
568 void JoypadControlServer::profile()
575 for(
unsigned int i = 0; i < count; ++i)
579 message += to_string(data) +
" ";
581 yCInfo(JOYPADCONTROLSERVER) << message;
585 for(
unsigned int i = 0; i < count; ++i)
588 m_device->
getHat(i, data);
589 message += to_string(data) +
" ";
591 yCInfo(JOYPADCONTROLSERVER) << message;
593 message =
"Buttons: ";
595 for(
unsigned int i = 0; i < count; ++i)
599 message += to_string(data) +
" ";
601 yCInfo(JOYPADCONTROLSERVER) << message;
605 for(
unsigned int i = 0; i < count; ++i)
609 message +=
"n_" + to_string(i) +
": ";
610 for (
size_t j = 0; j < data.
size(); ++j)
612 message += to_string(data[j]) +
" ";
617 yCInfo(JOYPADCONTROLSERVER) << message;
619 message =
"trackball: ";
621 for(
unsigned int i = 0; i < count; ++i)
625 message +=
"n_" + to_string(i) +
": ";
626 for (
size_t j = 0; j < data.
size(); ++j)
628 message += to_string(data[j]) +
" ";
633 message =
"touch Surface: ";
635 for(
unsigned int i = 0; i < count; ++i)
639 message +=
"n_" + to_string(i) +
": ";
640 for (
size_t j = 0; j < data.
size(); ++j)
642 message += to_string(data[j]) +
" ";
646 yCInfo(JOYPADCONTROLSERVER) << message;
653 if (m_portButtons.valid)
657 Vector& b = m_portButtons.prepare();
659 for(
size_t i = 0; i < m_portButtons.count; ++i)
669 if(
write)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))
689 if(
write)m_portHats.write();
692 if (m_portAxis.valid)
697 Vector& b = m_portAxis.prepare();
699 for(
size_t i = 0; i < m_portAxis.count; ++i)
704 yCError(JOYPADCONTROLSERVER) <<
"Cannot get axis with id" << i;
710 if(
write)m_portAxis.write();
713 if (m_portTrackball.valid)
718 Vector& b = m_portTrackball.prepare();
720 for(
size_t i = 0; i < m_portTrackball.count; ++i)
725 yCError(JOYPADCONTROLSERVER) <<
"Cannot get axis with id" << i;
731 if(
write)m_portTrackball.write();
734 if (m_portStick.valid)
738 Vector& b = m_portStick.prepare();
740 for(
size_t i = 0; i < m_portStick.count; ++i)
743 unsigned int dofCount;
751 if(
write)m_portStick.write();
754 if (m_portTouch.valid)
758 Vector& b = m_portTouch.prepare();
760 for(
unsigned int i = 0; i < m_portTouch.count; ++i)
771 if(
write)m_portTouch.write();
792 yCError(JOYPADCONTROLSERVER) <<
"Cannot attach more than one device";
797 if(p[0]->key ==
"IJoypadController")
799 yCInfo(JOYPADCONTROLSERVER) <<
"Good name!";
803 yCInfo(JOYPADCONTROLSERVER) <<
"Bad name!";
806 if (!Idevice2attach->
isValid())
808 yCError(JOYPADCONTROLSERVER) <<
"Device " << p[0]->key <<
" to attach to is not valid ... cannot proceed";
812 Idevice2attach->
view(m_device);
816 PeriodicThread::setPeriod(m_period);
817 if (!PeriodicThread::start())
830 if (m_isSubdeviceOwned)
842 if(m_isSubdeviceOwned)
844 if(m_subDeviceOwned)m_subDeviceOwned->
close();
846 m_subDeviceOwned =
nullptr;
848 m_isSubdeviceOwned =
false;
852 vector<JoypadControl::LoopablePort*> portv;
853 portv.push_back(&m_portButtons);
854 portv.push_back(&m_portAxis);
855 portv.push_back(&m_portStick);
856 portv.push_back(&m_portTouch);
857 portv.push_back(&m_portTrackball);
858 portv.push_back(&m_portHats);
863 p->contactable->close();
void close() override
Stop port activity.
A simple collection of objects that can be described and transmitted in a portable way.
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
virtual bool getTrackballCount(unsigned int &Trackball_count)=0
Get number of trackballs.
A base class for nested structures that can be searched.
constexpr yarp::conf::vocab32_t VOCAB_STICKDOF
bool detachAll() override
Detach the object (you must have first called attach).
virtual bool getAxis(unsigned int axis_id, double &value)=0
Get the value of an axis if present, return false otherwise.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
virtual bool getAxisCount(unsigned int &axis_count)=0
Get number of Axes.
bool isValid() const
Check if device is valid.
#define YARP_LOG_COMPONENT(name,...)
virtual bool getButtonCount(unsigned int &button_count)=0
Get number of Buttons.
constexpr yarp::conf::vocab32_t VOCAB_GET
constexpr yarp::conf::vocab32_t VOCAB_TRACKBALL
bool detach() override
Detach the object (you must have first called attach).
bool view(T *&x)
Get an interface to the device driver.
virtual bool getTouchSurfaceCount(unsigned int &touch_count)=0
get the number of touch surface.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
virtual bool getStickDoF(unsigned int stick_id, unsigned int &DoF)=0
Get the Degree Of Freedom count for desired stick.
~JoypadControlServer() override
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
bool open(const std::string &txt)
Construct and configure a device by its common name.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
bool isRunning() const
Returns true when the thread is started, false otherwise.
An interface for the device drivers.
virtual bool getStickCount(unsigned int &stick_count)=0
get the number of the sticks
constexpr yarp::conf::vocab32_t VOCAB_VALUE
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
constexpr yarp::conf::vocab32_t VOCAB_BUTTON
virtual bool asBool() const
Get boolean value.
virtual bool getButton(unsigned int button_id, float &value)=0
Get the value of a button.
virtual std::string asString() const
Get string value.
A container for a device driver.
constexpr yarp::conf::vocab32_t VOCAB_FAILED
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
virtual bool getTouch(unsigned int touch_id, yarp::sig::Vector &value)=0
Get the value of a touch if present, return false otherwise.
bool close() override
Close the DeviceDriver.
constexpr yarp::conf::vocab32_t VOCAB_AXIS
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
constexpr yarp::conf::vocab32_t VOCAB_TOUCH
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
void setReader(PortReader &reader) override
Set an external reader for port data.
An abstraction for a periodic thread.
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool close() override
Close the DeviceDriver.
constexpr yarp::conf::vocab32_t VOCAB_CARTESIAN
constexpr yarp::conf::vocab32_t VOCAB_HAT
void threadRelease() override
Release method.
#define yCError(component,...)
virtual std::int32_t asInt32() const
Get 32-bit integer value.
#define yCInfo(component,...)
void run() override
Loop function.
An interface to the operating system, including Port based communication.
constexpr yarp::conf::vocab32_t VOCAB_COUNT
virtual bool getHat(unsigned int hat_id, unsigned char &value)=0
Get the value of an Hat.
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.
bool configure(yarp::dev::IJoypadController *interface)
The main, catch-all namespace for YARP.
void cat(Vector &a, const Vector &b)
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
bool threadInit() override
Initialization method.
constexpr yarp::conf::vocab32_t VOCAB_IJOYPADCTRL
virtual bool getHatCount(unsigned int &Hat_count)=0
Get number of Hats.
virtual bool getTrackball(unsigned int trackball_id, yarp::sig::Vector &value)=0
Get the axes change of a Trackball.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
A single value (typically within a Bottle).
constexpr yarp::conf::vocab32_t VOCAB_STICK
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
#define DEFAULT_THREAD_PERIOD
void push_back(const T &elem)
Push a new element in the vector: size is changed.
constexpr yarp::conf::vocab32_t VOCAB_OK
virtual bool isVocab() const
Checks if value is a vocabulary identifier.
A class for storing options and configuration information.
virtual bool isInt32() const
Checks if value is a 32-bit integer.