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

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

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

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


GraduationCommon类定义与实现

#ifndef QGRADUATIONCOMMON_H
#define QGRADUATIONCOMMON_H
#include <sstream>
#include <iostream>
#include <string>
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 <string>
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 <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#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:"<<ibytes<<std::ends;
    return false;
}
bool JoyStickClass::CloseJoystickDevice(const int &iJoyStickFd)const
{
    if(0 != close(iJoyStickFd))
    {
      return false;
    }
    return true;
}
bool JoyStickClass::GetJoyStickStatus(const stJoyStickEvent &o_stJoyStickEvent,	stJoyStickUsing &o_stJoyStickUsing)const
{
    stJoyStickEvent stJoyStickEventTemp = o_stJoyStickEvent;
    stJoyStickEventTemp.type &= ~JS_EVENT_NEGATION;
    o_stJoyStickUsing.strTime = GraduationCommon::UnIntToString(stJoyStickEventTemp.time);
    o_stJoyStickUsing.strValue = GraduationCommon::ShortToString(stJoyStickEventTemp.value);
    if (stJoyStickEventTemp.type == JS_EVENT_TYPE_FIRST){
           o_stJoyStickUsing.strType = "1";
           switch (o_stJoyStickEvent.number){
            case 0:
                o_stJoyStickUsing.strNumber = "0" ;
            break;
            case 1:
                o_stJoyStickUsing.strNumber = "1" ;
            break;
            case 2:
                o_stJoyStickUsing.strNumber = "2" ;
            break;
            case 3:
                o_stJoyStickUsing.strNumber = "3" ;
            break;
            case 4:
                o_stJoyStickUsing.strNumber = "4" ;
            break;
            case 5:
                o_stJoyStickUsing.strNumber = "5" ;
            break;
            case 6:
                o_stJoyStickUsing.strNumber = "6" ;
            break;
            case 7:
                o_stJoyStickUsing.strNumber = "7" ;
            break;
            case 8:
                o_stJoyStickUsing.strNumber = "8" ;
            break;
            default:
                //return false;
            break;

        }
    }
    else if (stJoyStickEventTemp.type == JS_EVENT_TYPE_SECOND){
        o_stJoyStickUsing.strType = "2";
        switch (o_stJoyStickEvent.number){
        case 0:
               o_stJoyStickUsing.strNumber = "0" ;
        break;
        case 1:
               o_stJoyStickUsing.strNumber = "1" ;
        break;
        case 2:
               o_stJoyStickUsing.strNumber = "2" ;
        break;
        case 3:
               o_stJoyStickUsing.strNumber = "3" ;
        break;
        case 4:
               o_stJoyStickUsing.strNumber = "4" ;
        break;
        case 5:
               o_stJoyStickUsing.strNumber = "5" ;
        break;
        case 6:
               o_stJoyStickUsing.strNumber = "6" ;
        break;
        case 7:
               o_stJoyStickUsing.strNumber = "7" ;
        break;
        default:
            //return false;
        break;
        }
    }
    else
        return false;

    return true;
}
bool JoyStickClass::PackingJoyStickData(const stJoyStickUsing &o_stJoyStickUsing,SFEString &strPackingData)const
{
//    strPackingData  =  "T";//""Time";
//    strPackingData += o_stJoyStickUsing.strTime;
    strPackingData += "Y";//"Type";
    strPackingData += o_stJoyStickUsing.strType;
    strPackingData += "N";//"Number";
    strPackingData += o_stJoyStickUsing.strNumber;
    strPackingData += "V";//"Value";
    strPackingData += o_stJoyStickUsing.strValue;
    return true;
}


方法

/*
 * main.cpp
 *
 *  Created on: 2015-1-18
 *      Author: root
 */
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<termios.h> //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"<<endl;
        return 0;
    }
	MyTcpClient oMyTcpClient;
	string strIP("192.168.1.150");
	oMyTcpClient.ClientControl(strIP.c_str(),4567);

    JoyStickClass::stJoyStickEvent jse;
    JoyStickClass::stJoyStickEvent FormerJse;
    JoyStickClass::stJoyStickUsing jsUsing;
    SFEString strPackingData;
    int i = 50;
    std::cout<<"Begin"<<std::endl;
    while(i > 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 ! "<<std::endl;
            break;
        }
        #if DEBUG_OUT_PUT
        std::cout<<"using :"<< jsUsing.strTime<<" "<<jsUsing.strValue<<" "<< jsUsing.strType
        <<" "<<jsUsing.strNumber<<std::endl;
        #endif
        if(!m_JoyStickClass.PackingJoyStickData(jsUsing,strPackingData))
        {
            std::cout<<" Packing JoyStick Data  Error! "<<std::endl;
            break;
        }
        #if DEBUG_OUT_PUT
            cout<<"Befor Packing Joy Stick Class Data:"<<strPackingData<<endl;
            cout<<"Befor Packing Joy Stick Class Size:"<<strPackingData.size()<<endl;
        #endif
		while(strPackingData.length() < 12)
		{
			strPackingData += 's';
		}
		#if DEBUG_OUT_PUT
			cout<<"After Repair Joy Stick Class Data:"<<strPackingData<<endl;
			cout<<"After Repair Joy Stick Class Size:"<<strPackingData.size()<<endl;
		#endif
      if(!TwoCommandIsSame(FormerJse,jse))
       {
    	  if(CommandFilter(jse))
    	  {
			FormerJse = jse;
			cout<<"After Repair Joy Stick Class Data:"<<strPackingData<<endl;
			cout<<"After Repair Joy Stick Class Size:"<<strPackingData.size()<<endl;
			oMyTcpClient.ClientSend((const unsigned char*)strPackingData.c_str(),strPackingData.size());
			//oSerialCom.WriteSerialPortData(iComFd,strPackingData.c_str(),strPackingData.size());
    	  }
       }
      strPackingData.clear();
    }
	return 0;
}


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值