该型号陀螺仪只能通过RS232连接,连接如下图,红色线是接9—35V直流电源正极,传感器上黄色线(TX)接串口线的2号端口(RX),绿色线(RX)接串口的3号口(TX),黑色接串口上的5号口,同时与电源负极连接。这里要注意RS232线转usb线的问题,区分公头和母头,不要接反了,还有不能用老款的线。具体接线如下
usb连接电脑,开启电源,在设备管理器里装好驱动,打开北微传感器的通用协议上位机(可以去北微官网下载),一般会自动识别COM口,默认波特率9600,点击打开——获取,如果接线正确会自动获取设备地址00,再点击开始,晃动陀螺仪就能看到飞机转动了,左边也有对应的数据。如图
RS232转USB详细接线图
安装pyserial。
pip install pyserial
https://blog.csdn.net/qq_34935373/article/details/103583269
https://blog.csdn.net/huayucong/article/details/48729907,查看下功能即可。
具体的串口读写的16进制代号和原理 不一致,这里制作简单介绍:读者自己根据需求修改代码:
先看下原理:
下面就是代码了:
import serial
import time #延时使用
import binascii
s = serial.Serial("COM3", 9600) #初始化串口
while True:
#发送读取角度/加速度/陀螺仪/四元数的命令码
Hex_str=bytes.fromhex('77 04 00 59 5D')
s.write(Hex_str)
n=s.inWaiting()
try:
if n:
data= str(binascii.b2a_hex(s.read(n)))[2:-1] #Hex转换成字符串
#print('************************************')
print(data)
#pitch单位°
sign=int(data[8])
pitch=int(data[9])*100+int(data[10])*10+int(data[11])+int(data[12])/10+int(data[13])/100
if sign:
pitch=-1*pitch
print('yaw is ',pitch)
#roll
sign=int(data[14])
roll=int(data[15])*100+int(data[16])*10+int(data[17])+int(data[18])/10+int(data[19])/100
if sign:
roll=-1*roll
print('roll is',roll)
#x_acc单位g 9.8
sign=int(data[26])
x_acc=int(data[27])+int(data[28])/10+int(data[29])/100+int(data[30])/1000+int(data[31])/10000
if sign:
x_acc=-1*x_acc
print('x_acc is',x_acc)
#y_acc
sign=int(data[32])
y_acc=int(data[33])+int(data[34])/10+int(data[35])/100+int(data[36])/1000+int(data[37])/10000
if sign:
y_acc=-1*y_acc
print('y_acc is',y_acc)
#z_acc
sign=int(data[38])
z_acc=int(data[39])+int(data[40])/10+int(data[41])/100+int(data[42])/1000+int(data[43])/10000
if sign:
z_acc=-1*z_acc
print('z_acc is',z_acc)
#x_ang_acc 单位°/s
sign=int(data[44])
x_ang_acc=int(data[45])*100+int(data[46])*10+int(data[47])+int(data[48])/10+int(data[49])/100
if sign:
x_ang_acc=-1*x_ang_acc
print('x_ang_acc is',x_ang_acc)
#y_ang_acc
sign=int(data[50])
y_ang_acc=int(data[51])*100+int(data[52])*10+int(data[53])+int(data[54])/10+int(data[55])/100
if sign:
y_ang_acc=-1*y_ang_acc
print('y_ang_acc is',y_ang_acc)
#z_ang_acc
sign=int(data[56])
z_ang_acc=int(data[57])*100+int(data[58])*10+int(data[59])+int(data[60])/10+int(data[61])/100
if sign:
z_ang_acc=-1*z_ang_acc
print('x_ang_acc is',z_ang_acc)
#q1
sign=int(data[62])
q1=int(data[63])+int(data[64])/10+int(data[65])/100+int(data[66])/1000+int(data[67])/10000+int(data[68])/100000+int(data[69])/1000000
if sign:
q1=-1*q1
print('q1 is',q1)
#q2
sign=int(data[70])
q2=int(data[71])+int(data[72])/10+int(data[73])/100+int(data[74])/1000+int(data[75])/10000+int(data[76])/100000+int(data[77])/1000000
if sign:
q2=-1*q2
print('q2 is',q2)
#q3
sign=int(data[78])
q3=int(data[79])+int(data[80])/10+int(data[81])/100+int(data[82])/1000+int(data[83])/10000+int(data[84])/100000+int(data[85])/1000000
if sign:
q3=-1*q3
print('q3 is',q3)
#q4
sign=int(data[86])
q4=int(data[87])+int(data[88])/10+int(data[89])/100+int(data[90])/1000+int(data[91])/10000+int(data[92])/100000+int(data[93])/1000000
if sign:
q4=-1*q4
print('q4 is',q4)
time.sleep(0.1)
except:
continue
对应的c
//#include
//#include
#include
#include
#include
using namespace std;
struct ANG{
string SXXXYY;//SX XX YY
};
struct ACC{
string SXYYYY;//SX YY YY
};
struct ANG_VEL{
string SXXXYY;//SX XX YY
};
struct QUAR{
string SXYYYYYY;//SX YY YY YY
};
struct encode{
string _start;
struct ANG ang_data[3];
struct ACC acc_data[3];
struct ANG_VEL agv_data[3];
struct QUAR q;
string _end;
};
typedef unsigned char uint8;
const char hex_table[]={
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
void to_hex_str(uint8 *s,int len,char* d){
while(len--){
*(d++)=hex_table[*s>>4];
*(d++)=hex_table[*(s++)&0x0f];
}
}
int main()
{
uint8 buffer[48]={
0x77,0x2F,0x00,0x59,0x10,0x00,0x60,0x10,0x03,0x06,0x00,0x00,0x00,
0x10,0x01,0x07,0x10,0x05,0x43,0x01,0x01,0x54,
0x10,0x00,0x13,0x10,0x00,0x04,0x00,0x00,0x09, 0x10,0x87,0x06,0x35,0x00,0x01,0x76,0x91,0x00,0x02,0x06,0x94,0x00,0x49,0x11,0x75,0x5C
};
char a[97];
a[96]='\0';
to_hex_str(buffer,48,a);
//cout<