基于BBB的4轮移动轮式机器人系统设计与实现(七)-- 遥控手柄 控制 类
2015-04-24 23:38
495 查看
遥控手柄类应用的北通神鹰2手柄
对这个手柄在 linux下的数据采集方案需要用到 PlayJoy 手柄框架,可以在
GraduationCommon类定义与实现
PlayJoy类定义与实现
方法
整个工程可以到这里:http://download.csdn.net/detail/sfe1012/8629735
北通(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
相关文章推荐
- 基于BBB的4轮移动轮式机器人系统设计与实现(三)--BeagleBone Black PWM 驱动
- 基于BBB的4轮移动轮式机器人系统设计与实现(二)-----硬件搭建
- 基于BBB的4轮移动轮式机器人系统设计与实现(五)--BeagleBone Black编码器开发应用
- 基于BBB的4轮移动轮式机器人系统设计与实现(八)-- SIM908 GPS 数据获取
- 基于BBB的4轮移动轮式机器人系统设计与实现(四)--BeagleBone Black PWM 应用程序开发
- 基于BBB的4轮移动轮式机器人系统设计与实现(一)-----开篇引言
- 基于BBB的4轮移动轮式机器人系统设计与实现(六)--网络通信类的开发
- 【转】移动机器人控制软件的设计与实现
- 【机器视觉】移动机器人控制软件的设计与实现
- 使用C#+socket实现用移动设备控制的虚拟手柄
- IP视频监控系统中云台控制模块的设计与实现 (转)
- 基于J2ME-J2EE的移动工资查询系统 的设计与实现
- 使用C#+socket实现用移动设备控制的虚拟手柄
- 使用C#+socket实现用移动设备控制的虚拟手柄
- 基于角色访问控制的OA系统的设计与实现
- 机器人系统设计与控制技术作业和考核说明