我正在尝试使用 esp32 控制各种 Dynamixel XL320 电机 (https://emanual.robotis.com/docs/en/dxl/x/xl320/)。我希望我的程序做三件事:
向电机传输数据(控制模式、速度目标...)
从电机读取数据(当前位置、当前速度...)
通过蓝牙与esp32通信
各种库都实现了第 1 点和第 2 点,尽管在没有外部硬件的情况下,第 2 点似乎是最困难的。我发现的两个主要库是 this one 和 this one。
通过第一个,我可以使用
BluetoothSerial
esp 库完成第 1 点和第 3 点。 XL320_servo_example.ino 运行良好,并且在不使用 SoftwareSerial
库时与 BLE 通信兼容(注释第 15、18 和 35 行)。从我在互联网上看到的情况来看,第一个 xl320 库似乎无法通过 RX 串行引脚读取数据。
另一方面,第二个 xl320 库确实通过 RX 实现接收任务,但大量使用了
SoftwareSerial
库。我可以通过示例来实现第 1 点和第 2 点。正如这个问题中所解释的,SoftwareSerial
和BluetoothSerial
库不兼容。我得到与这个问题完全相同的错误,即:
C:\Users\nicol\AppData\Local\Temp\arduino_build_322778\libraries\EspSoftwareSerial\SoftwareSerial.cpp.o: In function `std::function<void ()>::operator()() const': C:\Users\nicol\Documents\Arduino\libraries\EspSoftwareSerial\src/SoftwareSerial.h:188:(.iram1.54[delegate::detail::DelegateImpl<void*, void>::operator()() const]+0x22): dangerous relocation: l32r: literal placed after use: .literal._ZNK8delegate6detail12DelegateImplIPvvEclEv collect2.exe: error: ld returned 1 exit status Usando librería BluetoothSerial con versión 1.0 en la carpeta: C:\Users\nicol\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\BluetoothSerial Utilizando biblioteca Arduino-Dynamixel-XL320-master en carpeta: C:\Users\nicol\Documents\Arduino\libraries\Arduino-Dynamixel-XL320-master (legacy) Usando librería EspSoftwareSerial con versión 7.0.1 en la carpeta: C:\Users\nicol\Documents\Arduino\libraries\EspSoftwareSerial exit status 1 Error compilando para la tarjeta ESP32 Dev Module.
用于生成此错误的最小 Arduino 代码如下:
// ###########BLE COMM##########
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
//############SERVO COMM#############
#include "Dynamixel-XL320.h"
DynamixelXL320 xl320(21, 19);
void setup()
{
Serial.begin(9600);
////// BLE
SerialBT.begin("Robot_centipede"); //Bluetooth device name
////// servos
xl320.begin(DynamixelXL320::Baud::BAUD_57600);
}
void loop()
{
}
我尝试通过修改 (second 库来解决我的问题。我尝试删除对
SoftwareSerial
的每个调用并使用 HardwareSerial
代替,但 SoftwareSerial 中的一些函数对我来说很难理解,尤其是以下内容:
void SoftwareSerial::enableRx(bool on) {
if (m_rxValid && on != m_rxEnabled) {
if (on) {
m_rxLastBit = m_pduBits - 1;
// Init to stop bit level and current tick
m_isrLastTick = (microsToTicks(micros()) | 1) ^ m_invert;
if (m_bitTicks >= microsToTicks(1000000UL / 74880UL))
attachInterruptArg(digitalPinToInterrupt(m_rxPin), reinterpret_cast<void (*)(void*)>(rxBitISR), this, CHANGE);
else
attachInterruptArg(digitalPinToInterrupt(m_rxPin), reinterpret_cast<void (*)(void*)>(rxBitSyncISR), this, m_invert ? RISING : FALLING);
}
else {
detachInterrupt(digitalPinToInterrupt(m_rxPin));
}
m_rxEnabled = on;
}
}
我尝试注释对此函数的调用并使用 esp32 的第三个串行端口(引脚 16 和 17),但我无法从电机读取数据。
我正在拼命寻找一个 esp32 库,该库允许从 XL320 电机传输和接收数据,并且不使用
SoftwareSerial
库,因此它与 BluetoothSerial
兼容。
请随时提出任何您需要帮助我的问题。
关于我如何最终解决问题的一些新闻。
在深入了解这个库之后,我设法实现了一些小的更改,可以从伺服系统读取数据。我创建了 this fork 来发布小的更改。它不使用 SoftwareSerial 库,因此我没有兼容性问题。
数据读取仍然不完美,因为电机有时会返回越界测量值。我怀疑这部分是由我的 esp 和 XL320s 之间的长连接线造成的。
根据经验,我建议在通信总线上的每次调用(读或写)之间实现 20ms 的延迟,并且同一线路中串联的 XL320 数量不要超过 4 个。在我使用 8 个 XL320 的应用中,我将它们连接成 2 条,每条 4 个电机,其中一个中央节点直接连接到我的 esp 的 RX/TX 端口。
郑重声明,XL320 电机应该由 74LVC2G241 或 NC7WZ241 控制,它们用于“合并”ESP32 的 RX 和 TX 引脚。本文中描述的解决方案只需连接这两个引脚即可工作,但这并不是最佳选择。