anyKode Marilou
ContentsIndexHome
PreviousUpNext
ModaCPP::QueryDevice

Query a specific Device and creates the template object.

C++
template <class DEVICEOBJECT> DEVICEOBJECT* QueryDevice(RobotPHX * pRobot, const xkode::lib::String& Name, Marilou::Commons::ActiveEntityClasses ActiveClass);

ModaCpp.h

Parameters 
Description 
RobotPHX * pRobot 
[in] Pointer to the robot that contains the device 
const xkode::lib::String& Name 
[in] Device relative name 
Marilou::Commons::ActiveEntityClasses ActiveClass 
[in] Required deice type 

A pointer to the template object if device is found, NULL otherwise. Returned objects must be deleted with the C++ delete keyword.

QueryDevice query a device according to its type and name from a specific robot. If the device exists, QueryDevice creates the template object and connect it to the simulation. if the template object does not have a constructor as specified hereafter, you must use QueryDevice2. The query AcitveClass can be specific (Ex: Marilou::Commons::CoreDeviceMotor) or generic (Marilou::Commons::CoreDevice). The first found device is considered as acceptable. 

 

The template object must have a constructor like:

MyDeviceObject::MyDeviceObject(xkode::lib::String Name):ExistingDeviceType(Name);

 

For exemple, these 2 methods are equivalents: 

 

//get the motor device using the predefined QueryDeviceMotor function:
ModaCPP::DeviceMotor *pMotor1    =robot->QueryDeviceMotor("hinge0/a1/m");

//or get the motor device using the QueryDevice function:
ModaCPP::DeviceMotor *pMotor2    =ModaCPP::QueryDevice<ModaCPP::DeviceMotor>(robot,"hinge0/a1/m",Marilou::Commons::DeviceMotor);

 

This function is useful for user devices plugins : you can create your own MODA device object then connect it on an existing remote device. Also, QueryUserDevice can be used to enum devices list, override existing device or create new device object that implements device plugin messages: read the following samples: 

 

Override existing device:
#include "stdafx.h"
#include "Modacpp.h"
#include "conio.h"

#define MODASERVER "localhost"
#define MYROBOTNAME    "/"

using namespace ModaCPP;

//-----------------------------------------------------------
class MyMotor:public ModaCPP::DeviceMotor
    {
    public:
        MyMotor(xkode::lib::String Name):DeviceMotor(Name)
            {

            }

        //set the motor speed from a voltage value (+-12v)
        void SetVoltage(float voltage)
            {
            SetVelocityDPS((voltage/12.0f)*720);
            }
    };

//-----------------------------------------------------------
int main(int argc, char* argv[])
{
ModaCPP::Connection *connection=new ModaCPP::Connection(true);
//Try connect to MODA server
if(connection->Connect(MODASERVER))
    {
    _cprintf("Connection ok to moda server\r\n");
    //Find the robot
    ModaCPP::RobotPHX *robot=connection->QueryRobotPHX(MYROBOTNAME);
    if(robot)
        {
        //robot found, find motor
        _cprintf("robot found\r\n");

        MyMotor *pMyMotor=ModaCPP::QueryDevice<MyMotor>(robot,"hinge0/a1/m",Marilou::Commons::DeviceMotor);
        if(pMyMotor)
            {
            _cprintf("Motor OK\r\n");
            pMyMotor->SetVoltage(6.0f);

            //(...)

            delete pMyMotor;
            }
        else
            {
            _cprintf("Motor not found\r\n");
            }
        }
    else
        {
        _cprintf("robot not found\r\n");
        }
    }
else
    {
    _cprintf("Unable to connect to moda server\r\n");
    }
//Disconnect & delete
connection->Disconnect();
delete connection;
return 0;
}

 

Enum devices sample:
//C++ source code
#include "stdafx.h"
#include "Modacpp.h"
#include "conio.h"

#define MODASERVER "localhost"
#define MYROBOTNAME    "/"

using namespace ModaCPP;

//-----------------------------------------------------------
void EnumRobotDevices(RobotPHX *pRobot)
{
//2 kind of devices: CodeDevice and FromPluginDevice

xkode::lib::ObjectsArray<xkode::lib::String> list=pRobot->GetEntitiesList(Marilou::Commons::CoreDevice,true);
for(int i=0;i<list.Count();i++)
    {
    Device *pDevice=ModaCPP::QueryDevice2<Device>(pRobot,list[i],Marilou::Commons::CoreDevice);
    if(pDevice)
        {
        _cprintf("found device %s, type %X\r\n",pDevice->GetName().GetData(),pDevice->GetClass());
        delete pDevice;
        }
    else
        {
        _cprintf("found NULL device\r\n");
        }
    }
}

//-----------------------------------------------------------
int main(int argc, char* argv[])
{
ModaCPP::Connection *connection=new ModaCPP::Connection(true);
//Try connect to MODA server
if(connection->Connect(MODASERVER))
    {
    _cprintf("Connection ok to moda server\r\n");
    //Find the robot
    ModaCPP::RobotPHX *robot=connection->QueryRobotPHX(MYROBOTNAME);
    if(robot)
        {
        //robot found, enum devices
        _cprintf("robot found\r\n");
        EnumRobotDevices(robot);
        }
    else
        {
        _cprintf("robot not found\r\n");
        }
    }
else
    {
    _cprintf("Unable to connect to moda server\r\n");
    }
//Disconnect & delete
connection->Disconnect();
delete connection;
return 0;
}
Documentation v4.7 (18/01/2015), Copyright (c) 2015 anyKode. All rights reserved.
What do you think about this topic? Send feedback!