因时电动夹爪使用代码
查看串口设备连接的端口:
ll /dev/ttyUSB*
更改权限
sudo chmod 777 /dev/ttyUSB0
查看串口信息
1.进入序列化中
cd serial/
ll
cd by-id/ 或 cd by-path/
ll
可以看到有一个串口线连接到了 ttyUSB0
sudo apt install cutecom
搜索 cutecom
python版:
抓起
释放
做动态获取夹爪力度与速度
00000000 00001010 & 0x00ff
00000000 11111111 == 0x00ff
00000000 00001010 & 00000000 11111111 = 00000000 00001010
01F4对应2个字节,1个字节8位, 01对应高位字节,F4对应低位字节,转成2进制表示 00000001 11110100
speed 速度;power:力度
speed=500,power=100
如把速度500 ;转成 b5,b6 ,
b5放的是低位字节数组 F4,b6 放的是高位字节数组 01
1.需要把获取500的高位及低位
1.1 取低位
#取speed=500 的低位,转成字节数组
#取speed=500 的低位,00000001 11110100 & 0x00ff==00000000 11111111分离去除高位 =00000000 11110100 转成字节数组
b5 = bytearray([speed&0x00ff]) #b5放的是低位字节数组 F4
1.2 取高位,将数据右移8位即可得到高位字节
#取speed=500 的高位,00000001 11110100 右移8位分离去除低位 =00000000 0000001 转成字节数组
b6 = bytearray([speed>>8])#b6放的是高位字节数组 01
Jaw.py
# 导入串口库
from serial import Serial
class Gripper:
def __init__(self,port):
self.serial = Serial(port, 115200)
#speed 速度;power:力度
def catch(self,speed=500,power=100):
# 创建串口通讯类
# 参数1:串口端口 /dev/ttyUSB0
# 0xEB 0x90
# 0x01
# 0x05
# 0x10
# 0xF4 0x01 0x64 0x00
# 校验和
# B9
# (1 Byte)
# 0x6F
#'\ddd' 这个bai形式,指的是8进制转义, 一个du\后面跟着最多三个8进制字符比如:zhi
\111 \177 \023等等,代表dao的是8进制值ascii码对应的字符
#'\xhh' 这个bai是16进制转义,\x后面跟两个合法的16进制字符,比如:
\x12 \xab等等
# '\x'是\xhh 16进制转义的简写; \xEB:代表一个字节; \x90:代表第二个字节; b'\xEB\x90':前面加上b把字符串转成字节数组;
b0b1 = b'\xEB\x90'
b2 = b'\x01'
b3 = b'\x05'
b4 = b'\x10'
#b5 = b'\xF4'
#b6 = b'\x01'
#b7 = b'\x64'
#b8 = b'\x00'
# 获取低位 bytearray([speed&0x00ff]): int整形转字节数组
#取speed=500 的低位,00000001 11110100 & 0x00ff==00000000 11111111分离去除高位 =00000000 11110100 转成字节数组
b5 = bytearray([speed&0x00ff]) #b5放的是低位字节数组 F4
#取speed=500 的高位,00000001 11110100 右移8位分离去除低位 =00000000 0000001 转成字节数组
b6 = bytearray([speed>>8])#b6放的是高位字节数组 01
b7 = bytearray([power&0x00ff])
b8 = bytearray([power>>8])
# 把b2+b8 取低字节 校验和
#数据校验 字节数组是不能加的,需要转换成数值在累加; 用ord可以把字节转换成编码 比如 a:97;加完后取低字节,然后转字节数组
b9 =
bytearray([(ord(b2)+ord(b3)+ord(b4)+ord(b5)+ord(b6)+ord(b7)+ord(b8))&0x00ff])
#字节数组拼接成发送的数据
data = b0b1+b2+b3+b4+b5+b6+b7+b8+b9
# 写入串口数据 字节数组
self.serial.write(data)
def release(self,speed=500):
# 0xEB 0x90
# 0x01
# 数据体长度
# B3
# (1 Byte)
# 0x03
# 指令号
# B4
# (1 Byte)
# 0x11
# 数据内容
# B5B6
# (1 Byte)
# 0xF4 0x01
# 校验和
# B7
# (1 Byte)
# 0x0A
b0b1 = b'\xEB\x90'
b2 = b'\x01'
b3 = b'\x03'
b4 = b'\x11'
b5 = bytearray([speed & 0x00ff])
b6 = bytearray([speed >> 8])
b7 = bytearray([(ord(b2)+ord(b3)+ord(b4)+ord(b5)+ord(b6))&0x00ff])
data = b0b1 + b2 + b3 + b4 + b5 + b6 + b7
# 写入串口数据 字节数组
self.serial.write(data)
def close(self):
self.serial.close()
gripper = Gripper('/dev/ttyUSB0')
# gripper.catch(speed=100,power=80) #抓取 speed速度 power力度
gripper.release(speed=100) #释放
# catch()
# release()
c++版
rs232.h 库
#ifndef github_com_kranfix_rs232_rs232_h
#define github_com_kranfix_rs232_rs232_h
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
namespace kfx {
const char Comports[22][13] = {"/dev/ttyACM0",
"/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6",
"/dev/ttyS7", "/dev/ttyS8", "/dev/ttyS9",
"/dev/ttyS10", "/dev/ttyS11", "/dev/ttyS12",
"/dev/ttyS13", "/dev/ttyS14", "/dev/ttyS15",
"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2",
"/dev/ttyUSB3", "/dev/ttyUSB4", "/dev/ttyUSB5"};
class RS232 {
char devname[13]; // Device Name
int baudr, port; // Baudrate and Port Number
bool available;
struct termios ops; // old port settings
public:
RS232(char *, int);
int IsAvailable() { return available; }
char *GetDeviceName() { return devname; }
int Read(unsigned char);
int Read(unsigned char *, int);
int Write(unsigned char);
int Write(unsigned char *, int);
void Print(const char *);
void Close();
int IsCTSEnabled();
};
}
#endif // giihub_com_kranfix_rs232_rs232_h
rs232.cpp
#ifndef kranfix_rs232_rs232_cc
#define kranfix_rs232_rs232_cc
#include "rs232.h"
static int error;
static struct termios nps;
kfx::RS232::RS232(char *dev_name, int baudrate) : available(false) {
// asigning device name
strcpy(devname, dev_name);
// Chossing baudrate
switch (baudrate) {
case 50 : baudr = B50;
break;
case 75 : baudr = B75;
break;
case 110 : baudr = B110;
break;
case 134 : baudr = B134;
break;
case 150 : baudr = B150;
break;
case 200 : baudr = B200;
break;
case 300 : baudr = B300;
break;
case 600 : baudr = B600;
break;
case 1200 : baudr = B1200;
break;
case 1800 : baudr = B1800;
break;
case 2400 : baudr = B2400;
break;
case 4800 : baudr = B4800;
break;
case 9600 : baudr = B9600;
break;
case 19200 : baudr = B19200;
break;
case 38400 : baudr = B38400;
break;
case 57600 : baudr = B57600;
break;
case 115200 : baudr = B115200;
break;
case 230400 : baudr = B230400;
break;
case 460800 : baudr = B460800;
break;
case 500000 : baudr = B500000;
break;
case 576000 : baudr = B576000;
break;
case 921600 : baudr = B921600;
break;
case 1000000 : baudr = B1000000;
break;
default : printf("invalid baudrate\n");
return;
}
port = open(devname, O_RDWR | O_NOCTTY | O_NDELAY);
if (port == -1) {
perror("unable to open comport ");
return;
}
error = tcgetattr(port, &ops);
if (error == -1) {
close(port);
perror("unable to read portsettings ");
return;
}
memset(&nps, 0, sizeof(nps)); /* clear the new struct */
nps.c_cflag = baudr | CS8 | CLOCAL | CREAD;
nps.c_iflag = IGNPAR;
nps.c_oflag = 0;
nps.c_lflag = 0;
nps.c_cc[VMIN] = 0; /* block untill n bytes are received */
nps.c_cc[VTIME] = 0; /* block untill a timer expires (n * 100 mSec.) */
error = tcsetattr(port, TCSANOW, &nps);
if (error == -1) {
close(port);
perror("unable to adjust portsettings ");
return;
}
available = true;
}
int kfx::RS232::Read(unsigned char byte) {
return read(port, &byte, 1);
}
int kfx::RS232::Read(unsigned char *buf, int size) {
#ifndef __STRICT_ANSI__ /* __STRICT_ANSI__ is defined when the -ansi option is used for gcc */
if (size > SSIZE_MAX) size = (int) SSIZE_MAX; /* SSIZE_MAX is defined in limits.h */
#else
if(size>4096) size = 4096;
#endif
return read(port, buf, size);
}
int kfx::RS232::Write(unsigned char byte) {
return write(port, &byte, 1);
}
int kfx::RS232::Write(unsigned char *buf, int size) {
return write(port, buf, size);
}
void kfx::RS232::Close() {
close(port);
tcsetattr(port, TCSANOW, &ops);
}
/*
Constant Description
--------------------------------------------
TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD Synonym for TIOCM_CAR
TIOCM_RNG RNG (ring)
TIOCM_RI Synonym for TIOCM_RNG
TIOCM_DSR DSR (data set ready)
*/
int kfx::RS232::IsCTSEnabled() {
int status;
status = ioctl(port, TIOCMGET, &status);
return (status & TIOCM_CTS) ? 1 : 0;
}
// Sends a string to serial port till finding a '\0'
void kfx::RS232::Print(const char *text) {
while (*text != 0) Write(*(text++));
}
#endif // kranfix_rs232_rs232_cc
Gripper.cpp
//
// Created by wt on 2020/7/11.
//
#include "Gripper.h"
Gripper::Gripper(char *port,int btl):rs232(port,btl) {
}
Gripper::~Gripper() {
}
void Gripper::catchJaw(int speed, int power) {
//无符号的char类型 有符号的char是-128 到127 ,而我们需要的是16进制数是一个255的数据,所以需要无符号的类型
unsigned char data[10];
data[0] = 0xEB;
data[1] = 0x90;
data[2] = 0x01;
data[3] = 0x05;
data[4] = 0x10;
//取speed=500 的低位,00000001 11110100 & 0x00ff==00000000 11111111分离去除高位 =00000000 11110100
data[5] = speed&0x00ff;//b5放的是低位 F4
//取speed=500 的高位,00000001 11110100 右移8位分离去除低位 =00000000 0000001
data[6] = speed>>8; //b6放的是高位 01
data[7] = power&0x00ff;
data[8] = power>>8;
//不用像python那样处理,直接把char类型相加即可
data[9] = data[2]+data[3]+data[4]+data[5]+data[6]+data[7]+data[8];
rs232.Write(data,sizeof(data)/sizeof(unsigned char));
}
void Gripper::release(int speed) {
unsigned char data[8];
data[0] = 0xEB;
data[1] = 0x90;
data[2] = 0x01;
data[3] = 0x03;
data[4] = 0x11;
data[5] = speed&0x00ff;
data[6] = speed>>8;
data[7] = data[2]+data[3]+data[4]+data[5]+data[6];
rs232.Write(data,sizeof(data)/sizeof(unsigned char));
}
Gripper.h
//
// Created by wt on 2020/7/11.
//
#ifndef JAWCPP_GRIPPER_H
#define JAWCPP_GRIPPER_H
#include "rs232.h"
class Gripper {
private:
kfx::RS232 rs232;
public:
Gripper(char *port, int btl);
~Gripper();
/**
* 夹取
* @param speed
* @param power
*/
void catchJaw(int speed = 500, int power = 100);
/**
* 释放
* @param speed
*/
void release(int speed = 500);
};
#endif //JAWCPP_GRIPPER_H
main.cpp
#include <iostream>
using namespace std;
#include "Gripper.h"
int main(int argc,char *argv[]){
Gripper gripper("/dev/ttyUSB0",115200);
// gripper.catchJaw(100,100); //抓取 (速度,力度)
gripper.release(100); //释放
return 0;
}
因时电动夹爪-V1.04,c++库
链接:https://pan.baidu.com/s/1DM_F9FGKF8ofgeqjLexA3Q
提取码:4sm7
==============================版本2 唐=======================================
/home/sk/workspas/VisionGrab/src/jaws/CMakeLists.txt
add_library(jaws Gripper.cpp rs232.cpp)
add_executable(GripperMain GripperMain.cpp)
target_link_libraries(GripperMain jaws)
/home/sk/workspas/VisionGrab/src/jaws/Gripper.cpp
#include "Gripper.h"
#include <iostream>
using namespace std;
/// sudo chmod 777 /dev/ttyUSB0
Gripper::Gripper(char *port) : port(port) {
rs232 = new kfx::RS232(port, 115200);
if (!rs232->IsAvailable()) {
cout << "serial port is not available" << endl;
return;
}
}
Gripper::~Gripper() {}
void Gripper::connect() {
}
void Gripper::disconnect() {
rs232->Close();
}
void Gripper::gripperCatch(int speed, int power) {
if (speed > 1000) {
speed = 1000;
}
if (speed < 1) {
speed = 1;
}
if (power > 1000) {
power = 1000;
}
if (power < 30) {
power = 30;
}
std::cout << "夹取: speed-" << speed << " power-"<< power << std::endl;
int sendSize = 10;
u_char *data = new u_char[sendSize];
//B0-B1: 帧头
data[0] = 0xEB;
data[1] = 0x90;
//B2: ID号
data[2] = 0x01;
//B3: 数据体长度(b4-b8)
data[3] = 0x05;
//B4: 指令号
data[4] = 0x10;
//B5-B8
data[5] = speed & 0x00ff;
data[6] = speed >> 8;
data[7] = power & 0x00ff;
data[8] = power >> 8;
//B9(校验位 (b2 + b3 + ... + b8) & 0xFF)
data[9] = (data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8]) & 0xFF;
rs232->Write(data, sendSize);
}
void Gripper::gripperRelease(int speed) {
if (speed > 1000) {
speed = 1000;
}
if (speed < 1) {
speed = 1;
}
std::cout << "放开: speed-" << speed << std::endl;
int sendSize = 8;
u_char *data = new u_char[sendSize];
//B0-B1: 帧头
data[0] = 0xEB;
data[1] = 0x90;
//B2: ID号
data[2] = 0x01;
//B3: 数据体长度(b4-b8)
data[3] = 0x03;
//B4: 指令号
data[4] = 0x11;
//B5-B8
data[5] = speed & 0x00ff;
data[6] = speed >> 8;
//B9(校验位 (b2 + b3 + ... + b8) & 0xFF)
data[7] = (data[2] + data[3] + data[4] + data[5] + data[6]) & 0xFF;
rs232->Write(data, sendSize);
}
void Gripper::getEGParam() {}
/home/sk/workspas/VisionGrab/src/jaws/Gripper.h
#ifndef LASER_PKG_SRC_GRIPPER_H_
#define LASER_PKG_SRC_GRIPPER_H_
#include "rs232.h"
class Gripper {
public:
Gripper(char *port);
~Gripper();
private:
char *port;
kfx::RS232 *rs232;
public:
void connect();
void disconnect();
void gripperCatch(int speed = 500, int power = 100);
void gripperRelease(int speed = 500);
void getEGParam();
};
#endif //LASER_PKG_SRC_GRIPPER_H_
/home/sk/workspas/VisionGrab/src/jaws/GripperMain.cpp
/**
* @Author: PoplarTang
* @CreateTime: 2019-12-24
* @Description:
*/
#include "Gripper.h"
int main(int argc, char **argv) {
// sudo chmod 777 /dev/ttyUSB0
Gripper gripper("/dev/ttyUSB0");
gripper.gripperCatch(500, 200);
usleep( 2 * 1000 * 1000);
gripper.gripperRelease();
return 0;
}
/home/sk/workspas/VisionGrab/src/jaws/rs232.cpp
#ifndef kranfix_rs232_rs232_cc
#define kranfix_rs232_rs232_cc
#include "rs232.h"
static int error;
static struct termios nps;
kfx::RS232::RS232(char *dev_name, int baudrate) : available(false) {
// asigning device name
strcpy(devname, dev_name);
// Chossing baudrate
switch (baudrate) {
case 50 : baudr = B50;
break;
case 75 : baudr = B75;
break;
case 110 : baudr = B110;
break;
case 134 : baudr = B134;
break;
case 150 : baudr = B150;
break;
case 200 : baudr = B200;
break;
case 300 : baudr = B300;
break;
case 600 : baudr = B600;
break;
case 1200 : baudr = B1200;
break;
case 1800 : baudr = B1800;
break;
case 2400 : baudr = B2400;
break;
case 4800 : baudr = B4800;
break;
case 9600 : baudr = B9600;
break;
case 19200 : baudr = B19200;
break;
case 38400 : baudr = B38400;
break;
case 57600 : baudr = B57600;
break;
case 115200 : baudr = B115200;
break;
case 230400 : baudr = B230400;
break;
case 460800 : baudr = B460800;
break;
case 500000 : baudr = B500000;
break;
case 576000 : baudr = B576000;
break;
case 921600 : baudr = B921600;
break;
case 1000000 : baudr = B1000000;
break;
default : printf("invalid baudrate\n");
return;
}
port = open(devname, O_RDWR | O_NOCTTY | O_NDELAY);
if (port == -1) {
perror("unable to open comport ");
return;
}
error = tcgetattr(port, &ops);
if (error == -1) {
close(port);
perror("unable to read portsettings ");
return;
}
memset(&nps, 0, sizeof(nps)); /* clear the new struct */
nps.c_cflag = baudr | CS8 | CLOCAL | CREAD;
nps.c_iflag = IGNPAR;
nps.c_oflag = 0;
nps.c_lflag = 0;
nps.c_cc[VMIN] = 0; /* block untill n bytes are received */
nps.c_cc[VTIME] = 0; /* block untill a timer expires (n * 100 mSec.) */
error = tcsetattr(port, TCSANOW, &nps);
if (error == -1) {
close(port);
perror("unable to adjust portsettings ");
return;
}
available = true;
}
int kfx::RS232::Read(unsigned char byte) {
return read(port, &byte, 1);
}
int kfx::RS232::Read(unsigned char *buf, int size) {
#ifndef __STRICT_ANSI__ /* __STRICT_ANSI__ is defined when the -ansi option is used for gcc */
if (size > SSIZE_MAX) size = (int) SSIZE_MAX; /* SSIZE_MAX is defined in limits.h */
#else
if(size>4096) size = 4096;
#endif
return read(port, buf, size);
}
int kfx::RS232::Write(unsigned char byte) {
return write(port, &byte, 1);
}
int kfx::RS232::Write(unsigned char *buf, int size) {
return write(port, buf, size);
}
void kfx::RS232::Close() {
close(port);
tcsetattr(port, TCSANOW, &ops);
}
/*
Constant Description
--------------------------------------------
TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD Synonym for TIOCM_CAR
TIOCM_RNG RNG (ring)
TIOCM_RI Synonym for TIOCM_RNG
TIOCM_DSR DSR (data set ready)
*/
int kfx::RS232::IsCTSEnabled() {
int status;
status = ioctl(port, TIOCMGET, &status);
return (status & TIOCM_CTS) ? 1 : 0;
}
// Sends a string to serial port till finding a '\0'
void kfx::RS232::Print(const char *text) {
while (*text != 0) Write(*(text++));
}
#endif // kranfix_rs232_rs232_cc
/home/sk/workspas/VisionGrab/src/jaws/rs232.h
#ifndef github_com_kranfix_rs232_rs232_h
#define github_com_kranfix_rs232_rs232_h
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
namespace kfx {
const char Comports[22][13] = {"/dev/ttyACM0",
"/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3",
"/dev/ttyS4", "/dev/ttyS5", "/dev/ttyS6",
"/dev/ttyS7", "/dev/ttyS8", "/dev/ttyS9",
"/dev/ttyS10", "/dev/ttyS11", "/dev/ttyS12",
"/dev/ttyS13", "/dev/ttyS14", "/dev/ttyS15",
"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2",
"/dev/ttyUSB3", "/dev/ttyUSB4", "/dev/ttyUSB5"};
class RS232 {
char devname[13]; // Device Name
int baudr, port; // Baudrate and Port Number
bool available;
struct termios ops; // old port settings
public:
RS232(char *, int);
int IsAvailable() { return available; }
char *GetDeviceName() { return devname; }
int Read(unsigned char);
int Read(unsigned char *, int);
int Write(unsigned char);
int Write(unsigned char *, int);
void Print(const char *);
void Close();
int IsCTSEnabled();
};
}
#endif // giihub_com_kranfix_rs232_rs232_h