【从零开始的ROS四轴机械臂控制】(七)- ROS与arduino连接

本文详细介绍如何在虚拟机环境下实现ROS与Arduino的连接,包括虚拟机与Arduino的连接、PCA9685模块支持与测试、ROS与Arduino连接测试及实际连接过程,涉及节点创建、批处理命令设置及程序运行。

十、ROS与arduino连接

回到许久没有动的硬件上来,考虑到我使用的是虚拟机,所以首先要测试的是虚拟机与arduino的连接,然后才能考虑ROS与arduino的连接。

写这部分的时候不知道为什么markdown编辑器“炸”掉了,编辑好的文本成了一堆乱码,关键这堆乱码还被保存了。由于之前没有出现过这种情况,我习惯性的用编辑器做记录,现在基本上都没了。于是就导致了一个问题,我可能在重新编辑的时候忘记某些步骤,还是希望能注意一下。

本部分完成效果如下
在这里插入图片描述

1.虚拟机与arduino的连接

(1)arduino连接与IDE

首先将Arduino的连接到虚拟机,只要在Vmware打开的情况下,它都会询问是否连接到虚拟机,或者直接在Player菜单–>可移动设备中连接也可。

然后在虚拟机中安装arduino IDE,在Linux安装的具体方法可以百度或者Google上,在此不做详细说明。安装之后可以打开如下。

在这里插入图片描述

(2)PCA9685模块支持与测试

在这里插入图片描述
以上为原理图,连接好线后我们要下载关于PCA9685模块的支持库进行测试。

在arudino IDE中打开库管理,搜索Adafruit pwm Servo Driver Library,然后进行安装

在这里插入图片描述
在该库的示例下打开servo示例如下,

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// you can also call it with a different address and I2C interface
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
#define USMIN  600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX  2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates

// our servo # counter
uint8_t servonum = 0;

void setup() {
   
   
  Serial.begin(9600);
  Serial.println("8 channel Servo test!");

  pwm.begin();
  // In theory the internal oscillator is 25MHz but it really isn't
  // that precise. You can 'calibrate' by tweaking this number till
  // you get the frequency you're expecting!
  pwm.setOscillatorFrequency(27000000);  // The int.osc. is closer to 27MHz  
  pwm.setPWMFreq(SERVO_FREQ);  // Analog servos run at ~50 Hz updates

  delay(10);
}

// You can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise!
void setServoPulse(uint8_t n, double pulse) {
   
   
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= SERVO_FREQ;   // Analog servos run at ~60 Hz updates
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000000;  // convert input seconds to us
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
   
   
  // Drive each servo one at a time using setPWM()
  Serial.println(servonum);
  for (
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Stan Fu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值