360度虚拟摇杆控制机器人前进后退转弯(二)-Socket

上篇文章搞定了虚拟摇杆以及左右轮速度算法,接下来就是连接硬件以及向硬件提交信息

获得串口通信串儿

(1)串口通讯
(2)通讯规则:
1、协议格式:(8字节)
[startbyte] [data1][data2]…[data22][flags][endbyte]
startbyte=0x0f;
endbyte=0x00;
flags标志位我没有用到;
data1…data22:对应16个通道(ch1-ch16),每个通道11bit

2、需要的是data数据 22*8位
[startbyte] [data1] [data2] … [data22] [flags][endbyte]
startbyte = 11110000 b (0xF0)

//228==1611
[data1] [data2] … [data22] = [ch1,11bit] [ch2,11bit] … [ch16,11bit](ch#= 0 bis )

需要16个通道 规定ch1=左轮速度,ch2=右轮速度,ch5=武器速度
[ch1,11bit] [ch2,11bit] … [ch16,11bit](ch#= 0 bis )
通道为11位二进制数据
例如1200对应的二进制通道数据为10010110000

获取对应的data串儿代码

public static void main(code1Int:Int,code2Int:Int,code5Int:Int) throws IOException {
		String code = "00000000000";
		String code1 = Integer.toBinaryString(code1Int); //通道1--二进制
		String code2 = Integer.toBinaryString(code2Int); //通道2
		String code5 = Integer.toBinaryString(code5Int); //通道5
		System.out.println(code1);
		System.out.println(code2);
		System.out.println(code5);
		Integer i = 0;
		String io = code1 + code2;
		while(i < 14){
			i++;
			if(i.equals(2)){
				io += code5;
				continue;
			}
			io += code;
		}
		System.out.println(io);
		i = 0;
		Integer beginIndex = 0;
		String[] str = new String[22];
		while(i < 22){
			Integer endIndex = beginIndex + 8;
			str[i] = io.substring(beginIndex, endIndex);
			beginIndex = endIndex;
			i++;
		}
		System.out.println(Arrays.toString(str));
		String endString = "";
		for(String c : str){
			Integer six = Integer.parseInt(c, 2);
			String ipi = Integer.toHexString(six);
			if(ipi.equals("0")){
				ipi = "00";
			}
			endString += ipi;
			System.out.println(ipi);
			
		}
		endString = "0f" + endString + "0000";
		System.out.println(endString);
		Log.i("App_Tcp==", "endString=" + endString);
	
	}

== endString==
0fa95fcea80035f424000003f310f0540000000000000000000000000000000000

通信

由于项目要求实时性比较高,所以采取App直接连接硬件的方案
采用Socket直接连接IP和端口号
实现方法:
安卓Socket的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值