//
// This page was written by Tafifet
//
The aim of this tutorial is to introduce how to control a SIGVerse robot using Joystick. The example below allows to control a robot in 4 directions: moving forward, backward, turning left and right.

Up:[[Tutorial]]     Previous:[[Controlling agent with Kinect]]     Next:[[Speech recognition using Julius]]

----
#contents

The aim of this tutorial is to introduce how to control a SIGVerse robot using Joystick. The example below allows to control a robot in 4 directions: moving forward, backward, turning left and right.

*Installation of the joystick [#ed1b8136]
Download and install [[Microsoft directx sdk:http://www.microsoft.com/en-us/download/details.aspx?id=6812]]


*Controller [#ed1b8137]

The controller receives data from the joystick service as messages using the joystick, sets the corresponding velocity then sends the command to the robot.

joystick.cpp:

#highlight(cpp){{
#include "Controller.h"
#include "Logger.h"
#include <unistd.h>
#include "ControllerEvent.h"
#include <sstream>

class voiceRecognition : public Controller
{
  public:
  void onInit(InitEvent &evt);
  double onAction(ActionEvent &evt);
  void onRecvMsg(RecvMsgEvent &evt);
  public:
  RobotObj *my;
  double velocity;
  int i;
  double direction1;
  double direction2;
  int j;
  double Weel_one;
  double Weel_two;
};

void voiceRecognition::onInit(InitEvent &evt)
{
  Weel_one = 0.0;
  Weel_two = 0.0;
  my = this->getRobotObj(this->myname());
  my->setWheel(10.0, 10.0);
}

double voiceRecognition::onAction(ActionEvent &evt)
{
  my->setWheelVelocity(Weel_one,Weel_two);
  return 0.01;
}

void voiceRecognition::onRecvMsg(RecvMsgEvent &evt)
{
  std::string sender = evt.getSender();
  std::string msg = evt.getMsg();
  LOG_MSG(("message : %s", msg.c_str()));
  if(strcmp("moveforward",msg.c_str())==0)
    {
      Weel_one = 3.0;
      Weel_two = 3.0;
    }
  else if(strcmp("movebackward",msg.c_str())==0)
    {
      Weel_one = -3.0;
      Weel_two = -3.0;
    }
  else if(strcmp("turnleft",msg.c_str())==0)
    {
      Weel_one = -0.78;
      Weel_two = 0.78;
    }
  else if(strcmp("turnright",msg.c_str())==0)
    {
      Weel_one = 0.78;
      Weel_two = -0.78;
    }    
}

extern "C"  Controller * createController ()
{
 return new voiceRecognition;
}
}}


*Service[#ed1b8139]

The service receives data from the joystick, for each button of the joystick corresponds an action, the allowed actions are: move forward, move backward, turn left and turn right, when a button is pressed, the appropriate message is sent to the controller to move the robot.


Joystick_Service.cpp


#highlight(cpp){{
#include <tchar.h>
#include <string>
#include <iostream>
#include < fstream>
#include "SIGService.h"
#include <windows.h>
#include "Joystick_Driver.h"

bool Proceed = false;
bool lancer = false;

class Joystick_Service : public sigverse::SIGService
{

public:
	Joystick_Service(std::string name);
	~Joystick_Service();
    double onAction();
	void onRecvMsg(sigverse::RecvMsgEvent &evt);
private: Joystick*  JS; //pointer to joystick object
private: HWND hWnd;	
};

Joystick_Service::Joystick_Service(std::string name) : SIGService(name){

            JS = new Joystick();
            hWnd = NULL;
	};

Joystick_Service::~Joystick_Service()
{
	this->disconnect();
}

double Joystick_Service::onAction()
{
    JS->runJoystick(hWnd);
	printf("moveforward message        : %d\n",JS->getButton(0));
	printf("movebackward message      : %d\n",JS->getButton(1));
	printf("turnleft message      : %d\n",JS->getButton(2));
	printf("turnright message     : %d\n",JS->getButton(3));

	if(JS->getButton(0))
	{
this->sendMsg("robot_000","moveforward");
	}
	if(JS->getButton(1))
	{
this->sendMsg("robot_000","movebackward");

	}
	if(JS->getButton(2))
	{
this->sendMsg("robot_000","turnright");

	}

	if(JS->getButton(3))
	{
this->sendMsg("robot_000","turnleft");

	}

	return 0.1;
}

void Joystick_Service::onRecvMsg(sigverse::RecvMsgEvent &evt)
{
   
}
int main(int argc, char** argv)
{
	Joystick_Service srv("Joystick__Service");
	unsigned short port = (unsigned short)(atoi(argv[2]));
	srv.connect(argv[1], port);
	srv.startLoop();
	return 0;
}


}}


To download the plugin:  
#ref(Sig_Joystick.sig)


*World file[#ed1b8140]

joystickWorld.xml

#highlight(cpp){{
<?xml version="1.0" encoding="utf-8"?>
<world name="myworld1">
  <gravity x="0.0" y="-980.7" z="0.0"/>

  <instanciate class="WheelRobot-nii-v1.xml" type="Robot">
    <set-attr-value name="name" value="robot_000"/>
    <set-attr-value name="language" value="c++"/>
    <set-attr-value name="implementation"
                    value="./joystick.so"/>
    <set-attr-value name="dynamics" value="false"/>
    <set-attr-value name="x" value="-100.0"/>
    <set-attr-value name="y" value="30.0"/>
    <set-attr-value name="z" value="-130.0"/>
    <set-attr-value name="collision" value="true"/>
    <!--stereo camera right-->
    <camera id="1"
            link="REYE_LINK"
            direction="0.0 -1.0 1.0"
            position="0.0 0.0 3.0"/>
    <!--stereo camera left-->
    <camera id="2"
            link="LEYE_LINK"
            direction="0.0 -1.0 1.0"
            position="0.0 0.0 3.0"/>
    <!--distance sensor-->
    <camera id="3"
            link="WAIST_LINK0"
            direction="0.0 0.0 1.0"
            position="0.0 -5.0 20.0"/>
    <!--monitoring camera-->
    <camera id="4"
            link="WAIST_LINK2"
            direction="0 0 1"
            quaternion="0.0 0.0 -0.966 0.259"
            position="0.0 40.0 120.0"/>
  </instanciate>
</world>
}}



* Download the project [#ed1b3123]
To download the project from GIT repository, use the following link:

git@socio4.iir.nii.ac.jp:~/SigverseGitServer/unstable/usersContribution/tafifet/joystick_service.git
#highlight(end)

[[Tutorial]]
Up:[[Tutorial]]     Previous:[[Controlling agent with Kinect]]     Next:[[Speech recognition using Julius]]

#counter


Front page   New List of pages Search Recent changes   Help   RSS of recent changes