基于BBB的4轮移动轮式机器人系统设计与实现(七)-- 遥控手柄 控制 类

遥控手柄类应用的北通神鹰2手柄   

北通(BETOP)BTP-2175 阿修罗SE 有线震动版 游戏手柄 镜面白

对这个手柄在 linux下的数据采集方案需要用到 PlayJoy  手柄框架,可以在

基于BBB的4轮移动轮式机器人系统设计与实现(七)-- 遥控手柄 控制 类_第1张图片

GraduationCommon类定义与实现

#ifndef QGRADUATIONCOMMON_H
#define QGRADUATIONCOMMON_H
#include 
#include 
#include 
typedef std::string	SFEString;
class GraduationCommon{
   public:
          static SFEString ShortToString(const short &i);
          static SFEString UnCharToString(const unsigned char &type);
          static SFEString UnIntToString(const unsigned int &iIput);
          static SFEString IntToString(const int &iIput);
          static int            StringToInt(const SFEString &stInput);
};
#endif // QGRADUATIONCOMMON_H


#include "QGraduationCommon.h"
SFEString GraduationCommon::ShortToString(const short &i)
{
  SFEString strValue ;
  std::stringstream stream;
  stream << i;
  stream >> strValue;
  return strValue;
}
SFEString GraduationCommon::UnCharToString(const unsigned char &type)
{
  SFEString strValue ;
  std::stringstream stream;
  stream << type;
  stream >> strValue;
  return strValue;
}
SFEString GraduationCommon::UnIntToString(const unsigned int &iIput)
{
    SFEString strValue ;
    std::stringstream stream;
    stream << iIput;
    stream >> strValue;
    return strValue;
}
SFEString GraduationCommon::IntToString(const int &iIput)
{
    SFEString strValue ;
    std::stringstream stream;
    stream << iIput;
    stream >> strValue;
    return strValue;
}
int GraduationCommon::StringToInt(const SFEString &stInput)
{
    int  iValue ;
    std::stringstream stream;
    stream << stInput;
    stream >> iValue;
    return iValue;
}



PlayJoy类定义与实现

#ifndef QJOYSTICK_H
#define QJOYSTICK_H
#include 
typedef std::string	SFEString;
class JoyStickClass{
  public:
    typedef struct tagJoyStickEvent
    {
            unsigned int time;    /* event timestamp in milliseconds */
            short value; /* value */
            unsigned char type; /* event type */
            unsigned char number; /* axis/button number */
            tagJoyStickEvent& operator=(const tagJoyStickEvent& JseTemp){
            this->time   = JseTemp.time;
            this->value  = JseTemp.value;
            this->type   = JseTemp.type;
            this->number =  JseTemp.number;
            return *this;
        }
    }stJoyStickEvent;
    typedef struct tagJoyStickUsing
    {
        SFEString strTime;    /* event timestamp in milliseconds */
        SFEString strValue; /* value */
        SFEString strType; /* event type */
        SFEString strNumber; /* axis/button number */
        tagJoyStickUsing& operator=(const tagJoyStickUsing& JseTemp){
             this->strTime   = JseTemp.strTime;
             this->strValue  = JseTemp.strValue;
             this->strType   =  JseTemp.strType;
             this->strNumber =  JseTemp.strNumber;
             return *this;
        }
    }stJoyStickUsing;
  public:
            bool  OpenJoyStickDevice(int &iJoyStickFd,const std::string &strJoystickDevice)const;
            bool  ReadJoyStickEvent(stJoyStickEvent &o_stJoyStickEvent,const int &iJoyStickFd)const;
            bool  CloseJoystickDevice(const int &iJoyStickFd)const;
            bool  GetJoyStickStatus(const stJoyStickEvent &o_stJoyStickEvent,stJoyStickUsing &o_stJoyStickUsing)const;
            bool  PackingJoyStickData(const stJoyStickUsing &o_stJoyStickUsing,SFEString &strPackingData)const;
};
#endif // QJOYSTICK_H

#include"QJoyStick.h"
#include 
#include 
#include 
#include"QGraduationCommon.h"

#define JS_EVENT_TYPE_FIRST             0x01 /*   TYPE1    */
#define JS_EVENT_TYPE_SECOND        0x02 /*   TYPE2    */
#define JS_EVENT_NEGATION              0x80 /*   Negation */

bool  JoyStickClass::OpenJoyStickDevice(int &iJoyStickFd,const std::string &strJoystickDevice)const
{
    iJoyStickFd = open(strJoystickDevice.c_str(), O_RDONLY | O_NONBLOCK);
    if (iJoyStickFd < 0){
        return false;
    }
    return true;
}
bool  JoyStickClass::ReadJoyStickEvent(stJoyStickEvent &o_stJoyStickEvent,const int &iJoyStickFd)const
{
    int ibytes;
    ibytes = read(iJoyStickFd, &o_stJoyStickEvent, sizeof(o_stJoyStickEvent));
    if (ibytes == -1){
        return false;
    }
    if (ibytes == sizeof(o_stJoyStickEvent)){
        return true;
    }
    std::cout<<"Unexpected bytes from joystick:"<


方法

/*
 * main.cpp
 *
 *  Created on: 2015-1-18
 *      Author: root
 */
#include
#include
#include
#include //serial com
#include"QGraduationCommon.h"
#include"QJoyStick.h"
#include"SerialCom.h"
#include"Tcp.h"
//#define DEBUG_OUT_PUT 1
using namespace std;
bool TwoCommandIsSame(const JoyStickClass::stJoyStickEvent&FormerJse,
		               const JoyStickClass::stJoyStickEvent&NowJse)
{
	if(FormerJse.number== NowJse.number)
	{
		if(FormerJse.type == NowJse.type)
		{
			if(FormerJse.value == NowJse.value)
			{
			  return true;
			}
		}
	}
	return false;
}
//命令过滤
bool CommandFilter(const JoyStickClass::stJoyStickEvent&NowJse)
{
	if(NowJse.type == 2)
	{
		if((NowJse.number== 3)||( NowJse.number== 4 )||( NowJse.number== 1 )||( NowJse.number== 0 ))
		{
			return true;
		}
		else
			return false;
	}
	return false;
}
int main()
{
    //打开joySick
    const string strDevice("/dev/input/js0");
    JoyStickClass  m_JoyStickClass;
//	SerialComClass oSerialCom;
//	int iComFd;
//	oSerialCom.open_port(iComFd,Com_Usb_Device_2,true);//B115200
//	oSerialCom.set_com_config(iComFd,115200, 8, 'N', 1, 0, 0);//115200
    int m_iJoyStickFd;
    if(!m_JoyStickClass.OpenJoyStickDevice(m_iJoyStickFd,strDevice)){
        cout<<"open failed.\n"< 0)
    {
    	  //i--;
        if(m_JoyStickClass.ReadJoyStickEvent(jse,m_iJoyStickFd)){
        #if DEBUG_OUT_PUT
        printf("Event: time %8u, value %d, type: %u, axis/button: %u\n",
        jse.time, jse.value, jse.type, jse.number);
        #endif
        }
        if(!m_JoyStickClass.GetJoyStickStatus(jse,jsUsing))
        {
            std::cout<<" Read JoyStick Data Bad ! "<


整个工程可以到这里:http://download.csdn.net/detail/sfe1012/8629735

你可能感兴趣的:(4轮移动小型机器人开发,C++高级语言,嵌入式)