因时电动夹爪使用代码

                                         因时电动夹爪使用代码

python版:

Jaw.py

# 导入串口库
from serial import Serial

class Gripper:
    def __init__(self,port):
        self.serial = Serial(port, 115200)


    def catch(self,speed=500,power=100):
        # 创建串口通讯类
        # 参数1:串口端口  /dev/ttyUSB0
        # 0xEB 0x90
        # 0x01
        # 0x05
        # 0x10
        # 0xF4 0x01 0x64 0x00
        # 校验和
        # B9
        # (1 Byte)
        # 0x6F
        b0b1 = b'\xEB\x90'
        b2 = b'\x01'
        b3 = b'\x05'
        b4 = b'\x10'

        # 获取低位
        b5 = bytearray([speed&0x00ff])
        b6 = bytearray([speed>>8])
        b7 = bytearray([power&0x00ff])
        b8 = bytearray([power>>8])
        # 把b2+b8 取低字节
        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 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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) {
    unsigned char data[10];
    data[0] = 0xEB;
    data[1] = 0x90;
    data[2] = 0x01;
    data[3] = 0x05;
    data[4] = 0x10;
    data[5] = speed&0x00ff;
    data[6] = speed>>8;
    data[7] = power&0x00ff;
    data[8] = power>>8;
    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 
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

你可能感兴趣的:(c++,python,ros)