我在将一些C ++代码转换为Arduino时遇到了麻烦。任何帮助将不胜感激。
修改
我已成功完成上述任务。然而,现在唯一的问题是Arduino代码我已经准确,正确地读取电压但没有其他寄存器。我也可以写油门了。如果我调用不同数量的Serial.println()
语句,其他寄存器上的读数会发生变化,在某些情况下,电压寄存器也会停止工作。这在我的代码中可以找到
Serial.print("Voltage: );
如果我打印出所有这些寄存器,答案就会改变。我无法弄清楚为什么会这样。
/* DEFINITIONS */
#include <math.h>
/* FLOATS */
uint8_t command[5];
uint8_t response[3];
/* INTEGERS */
byte deviceId = 0x17;
double throttleOut = 0;
double voltage = 0;
double rippleVoltage = 0;
double current = 0;
double power = 0;
double throttle = 0;
double pwm = 0;
double rpm = 0;
double temp = 0;
double becVoltage = 0;
double safeState = 0;
double linkLiveEnabled = 0;
double eStopStatus = 0;
double rawNTC = 0;
/* SETUP */
void setup() {
Serial1.begin(115200);
Serial.begin(115200);
}
void loop() {
flushPort();
ReadWriteRegister(128, 1000, true);//_throttleOut is 0[0%] to 65535[100%]
voltage = ReadWriteRegister(0, 0, false) / 2042.0 / 0.05;
rippleVoltage = ReadWriteRegister(1, 0, false) / 2042 / 0.25;
current = ReadWriteRegister(2, 0, false) / 204200 * 50;
power = voltage * current;
throttle = (ReadWriteRegister(3, 0, false) / 2042.0 / 1.0);
pwm = ReadWriteRegister(4, 0, false) / 2042.0 / 3.996735;
rpm = ReadWriteRegister(5, 0, false) / 2042.0 / 4.89796E-5;
int poleCount = 20;//Motor pole count
rpm = rpm / (poleCount / 2);
temp = ReadWriteRegister(6, 0, false) / 2042.0 * 30.0;
becVoltage = ReadWriteRegister(7, 0, false) / 2042 / 0.25;
safeState = ReadWriteRegister(26, 0, false);
linkLiveEnabled = ReadWriteRegister(25, 0, false);
eStopStatus = ReadWriteRegister(27, 0, false) == 0 ? false : true;
rawNTC = ReadWriteRegister(9, 0, false) / 2042.0 / 0.01567091;
rawNTC = 1.0 / (log(rawNTC * 10200.0 / (255.0 - rawNTC) / 10000.0 ) / 3455.0 + 1.0 / 298.0) - 273.0;
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.print("Current: ");
Serial.println(current);
}
void flushPort() {
command[0] = command[1] = command[2] = command[3] = command[4] = 0;
Serial1.write(command, 5);
while (Serial1.available() > 0) {
Serial1.read();
}
}
double ReadWriteRegister(int reg, int value, bool writeMode) {
// Send read command
command[0] = (byte)(0x80 | deviceId);
command[1] = (byte)reg;
command[2] = (byte)((value >> 8) & 0xFF);
command[3] = (byte)(value & 0xFF);
command[4] = (byte)(0 - command[0] - command[1] - command[2] - command[3]);
Serial1.write(command, 5);
// Read response
if(Serial1.available() == 3) {
response[0] = (byte)Serial1.read();
response[1] = (byte)Serial1.read();
response[2] = (byte)Serial1.read();
}
if ((byte)(response[0] + response[1] + response[2]) == 0)
{
return (double)((response[0] << 8) + (response[1]));
}
else
{
Serial.println("Error communicating with device!");
}
}
编辑2 一些usb逻辑分析仪拍摄的照片。 [] [] [] [] [] [] [] 并且所有数据包都在这一个: [] 也许这会有助于超时等。这是我所拥有的所有信息:。
答案 0 :(得分:4)
ReadWriteRegister
无法正常工作。在115200,每个角色大约需要发送或接收87us。在那段时间里,Arduino可以执行大约100行代码!
请看这个片段:
Serial1.write(command, 5);
// Read response
if(Serial1.available() == 3) {
write
函数仅将命令放入输出缓冲区并开始发送第一个字符。它在之前返回所有字符都已传输。这需要500us!
然后,您会查看是否收到了3个字符的回复。但是命令还没有完成传输,你当然不会等待258us(3次86us)。如果设备需要时间来执行命令,它甚至可能需要更长的时间。
您必须做两件事:等待命令发送,并等待接收响应。试试这个:
Serial1.write(command, 5);
Serial1.flush(); // wait for command to go out
// Wait for response to come back
while (Serial1.available() < 3)
; // waitin'....
// Read response
response[0] = (byte)Serial1.read();
response[1] = (byte)Serial1.read();
response[2] = (byte)Serial1.read();
这称为&#34;阻止&#34;,因为Arduino在您等待响应时不会做任何其他事情。
然而,如果遗漏了一个角色,你的节目可能会挂起&#34;如果没有正确发送/接收第二个字符(它发生),等待第四个字符。所以你应该在while循环中放置500us超时:
// Wait for response
uint32_t startTime = micros();
while ((Serial1.available() < 3) && (micros() - startTime < 500UL))
; // waitin'...
...或更长时间,如果您知道设备响应的速度有多快。然后你可以确定你是否真的得到了答复:
/* DEFINITIONS */
#include <math.h>
/* INTEGERS */
byte deviceId = 0x17;
uint8_t command[5];
uint8_t response[3];
/* FLOATS */
double throttleOut = 0.0;
double voltage = 0.0;
double rippleVoltage = 0.0;
double current = 0.0;
double power = 0.0;
double throttle = 0.0;
double pwm = 0.0;
double rpm = 0.0;
double temp = 0.0;
double becVoltage = 0.0;
uint8_t safeState = 0;
uint8_t linkLiveEnabled = 0;
bool eStopStatus = 0;
double rawNTC = 0.0;
/* SETUP */
void setup() {
Serial1.begin(115200);
Serial.begin(115200);
Serial.println( F("---------------------------") );
// According to the spec, you can synchronize with the device by writing
// five zeroes. Although I suspect this is mostly for the SPI and I2c
// interfaces (not TTL-level RS-232), it won't hurt to do it here.
Serial1.write( command, 5 );
delay( 250 ); // ms
while (Serial1.available())
Serial1.read(); // throw away
// Set the throttle just once
ReadWriteRegister(128, 1000);//_throttleOut is 0[0%] to 65535[100%]
}
// For 12-bit A/D conversions, the range is 0..4096. Values at
// the top and bottom are usually useless, so the value is limited
// to 6..4090 and then shifted down to 0..4084. The middle of this
// range will be the "1.0" value: 2042. Depending on what is being
// measured, you still need to scale the result.
const double ADC_FACTOR = 2042.0;
void loop() {
uint32_t scanTime = millis(); // mark time now so we can delay later
voltage = ReadWriteRegister( 0, 0 ) / ADC_FACTOR * 20.0;
rippleVoltage = ReadWriteRegister( 1, 0 ) / ADC_FACTOR * 4.0;
current = ReadWriteRegister( 2, 0 ) / ADC_FACTOR * 50.0;
power = voltage * current;
throttle = ReadWriteRegister( 3, 0 ) / ADC_FACTOR * 1.0;
pwm = ReadWriteRegister( 4, 0 ) / ADC_FACTOR * 0.2502;
rpm = ReadWriteRegister( 5, 0 ) / ADC_FACTOR * 20416.66;
const int poleCount = 20;//Motor pole count
rpm = rpm / (poleCount / 2);
temp = ReadWriteRegister( 6, 0 ) / ADC_FACTOR * 30.0;
becVoltage = ReadWriteRegister( 7, 0 ) / ADC_FACTOR * 4.0;
safeState = ReadWriteRegister( 26, 0 );
linkLiveEnabled = ReadWriteRegister( 25, 0 );
eStopStatus = ReadWriteRegister( 27, 0 );
rawNTC = ReadWriteRegister( 9, 0 ) / ADC_FACTOR * 63.1825;
const double R0 = 1000.0;
const double R2 = 10200.0;
const double B = 3455.0;
rawNTC = 1.0 / (log(rawNTC * R2 / (255.0 - rawNTC) / R0 ) / B + 1.0 / 298.0) - 273.0;
Serial.print( F("Voltage: ") );
Serial.println(voltage);
Serial.print( F("Current: ") );
Serial.println(current);
Serial.print( F("Throttle: ") );
Serial.println(throttle);
Serial.print( F("RPM: ") );
Serial.println(rpm);
// These prints do not actually send the characters, they only queue
// them up to be sent gradually, at 115200. The characters will be
// pulled from the output queue by a TX interrupt, and given to the
// UART one at a time.
//
// To prevent these interrupts from possibly interfering with any other
// timing, and to pace your program, we will wait *now* for all the
// characters to be sent to the Serial Monitor.
Serial.flush();
// Let's pace things a little bit more for testing: delay here until
// it's time to scan again.
const uint32_t SCAN_INTERVAL = 1000UL; // ms
while (millis() - scanTime < SCAN_INTERVAL)
; // waitin'
}
int16_t ReadWriteRegister(int reg, int value) {
// Flush input, as suggested by Gee Bee
while (Serial1.available() > 0)
Serial1.read();
// Send command (register number determines whether it is read or write)
command[0] = (byte)(0x80 | deviceId);
command[1] = (byte)reg;
command[2] = (byte)((value >> 8) & 0xFF);
command[3] = (byte)(value & 0xFF);
command[4] = (byte)(0 - command[0] - command[1] - command[2] - command[3]);
Serial1.write(command, 5);
// The command bytes are only queued for transmission, they have not
// actually gone out. You can either wait for command to go out
// with a `Serial1.flush()` *OR* add the transmission time to the
// timeout value below. However, if anything else has queued bytes
// to be sent and didn't wait for them to go out, the calculated
// timeout would be wrong. It is safer to flush now and guarantee
// that *all* bytes have been sent: anything sent earlier (I don't
// see anything else, but you may change that later) *plus*
// these 5 command bytes.
Serial1.flush();
// Now wait for response to come back, for a certain number of us
// The TIMEOUT could be as short as 3 character times @ the Serial1
// baudrate: 3 * (10 bits/char) / 115200bps = 261us. This is if
// the device responds immediately. Gee Bee says 20ms, which would
// be 20000UL. There's nothing in the spec, but 1ms seems generous
// for reading the raw NTC value, which may require an ADC conversion.
// Even the Arduino can do that in 100us. Try longer if you get
// timeout warnings.
const uint32_t TIMEOUT = 2000UL;
uint32_t startTime = micros();
while ((Serial1.available() < 3) && (micros() - startTime < TIMEOUT))
; // waitin'...
int16_t result;
if (Serial1.available() >= 3) {
response[0] = (byte)Serial1.read();
response[1] = (byte)Serial1.read();
response[2] = (byte)Serial1.read();
// Verify the checksum
if (response[0] + response[1] + response[2] != 0) {
Serial.print( reg );
Serial.println( F(" Checksum error!") );
Serial.flush(); // optional, use it for now to stay synchronous
}
// Cast to 16 bits *first*, then shift and add
result = (((int16_t) response[0]) << 8) + (int16_t) response[1];
} else {
// Must have timed out, because there aren't enough characters
Serial.print( reg );
Serial.println( F(" Timed out!") );
Serial.flush(); // optional, use it for now to stay synchronous
result = 0;
}
return result; // You must always return something
}
评论:
double
会导致你输掉前八位。如上计算应给出正确的答案。ReadWriteRegister
函数中使用的校验和。该函数可以告诉您它是否超时或校验和是否错误。这也意味着可能需要更长的超时。目前尚不清楚您的设备是否等待最多480毫秒来获取最新值,或者是否持续缓存它们并立即响应从ESC接收的最后一个值。但是,写入不会反映在最长480ms的读取值中,因为ESC需要花费时间接收命令然后发送新值。见ESC Castle Link protocol。ReadWriteRegister
函数返回一个16位整数,效率更高。比较浮点数永远不会好。 BTW,double
只是8位Arduinos上的单float
。ReadWriteRegister
函数不需要writemode
参数,因为寄存器编号决定了您是在写还是正在读取设备。throttle
值的编写。<小时/>
您的 L ogic A 分析镜头似乎显示&#34;扫描&#34;对于ESC。它正在尝试每个设备ID,其中一些回复非零电压。此外,它似乎运行在9600,不 115200.这是来自不同的设置吗?
无论如何,它确认了控制器规范所说的内容:写入5个字节,读取3.校验和值如预期。但是,它运行速度比您的程序慢10倍,因此它不会提供有关超时的大量新信息。这可能意味着在设备响应之前 是一个小延迟,可能是大约1位时间,或大约100us。
您是否阅读过控制器规格?您应该将程序与规范进行比较,以确保您了解控制器的工作原理。
我已将上述程序修改为:
setup
中的控制器同步(写入5个零字节,等待250毫秒),double
(请参阅safeState
,linkLiveStatus
和eStopStatus
),reg
号码。如果您想在这方面取得成功,您必须学会阅读规范并将其要求转换为符合的代码。你开始的程序在最坏的情况下是非保形的,或者说最好是误导。对于说&#34; INTEGERS&#34;的评论我特别感到好笑。和&#34; FLOATS&#34;,但这些部分包含相反的内容。
也许这是修复别人代码的一课?它确实会遇到许多问题。如果我每次说的话都有镍:
......我会成为一个非常富有的人。 :)
(更新结束)
<小时/>
这也符合您描述的症状:因为您没有等待传输完成,所以第一次读取0个字节(read
将返回-1或0xFF字节)。 / p>
在您多次调用此例程(并将几个命令排入输出缓冲区)之后,已经过了500us并且最终发送了第一个命令。设备通过开始发送3个字符来响应。 87us之后,Arduino终于收到了第一个角色。它会被read
语句的一个读取,但谁知道哪一个?它将是随机的,基于经过的时间。
发送更多命令,并通过其中一个语句接收和读取单个字符,直到64个字节的命令或Serial.println chars 排队。然后write
命令或Serial.print 阻塞,直到输出有最新命令的空间。 (这解决了你问题的标题。)
当最终发送足够的命令字节或调试消息字符时,Serial1.write
或Serial.print
返回。同时,收到的字符进入输入缓冲区。 (这是他们存储的地方,直到你拨打read
。)
此时,连续的三个read
语句实际上会获得设备发送的字符。但是由于之前字符的随机消耗,它可能是一个响应的最后一个字符,后面是下一个响应的前两个字符。你&#34;不同步&#34;使用3字节响应。
保持&#34;同步&#34;使用该设备,您必须等待发送完成flush
,然后等待回复while
。
答案 1 :(得分:-1)
很抱歉,但此代码存在一些问题。你假设
在ReadWriteRegister中使用以下模式:
在代码中:
double ReadWriteRegister(int reg, int value, bool writeMode) {
// Send read command
command[0] = (byte)(0x80 | deviceId);
command[1] = (byte)reg;
command[2] = (byte)((value >> 8) & 0xFF);
command[3] = (byte)(value & 0xFF);
command[4] = (byte)(0 - command[0] - command[1] - command[2] - command[3]);
// discard any input
while (Serial1.available() > 0) {
Serial1.read();
}
// write out command
Serial1.write(command, 5);
// wait for 3 bytes
uint32_t startTime = micros();
while ((Serial1.available() < 3) && (micros() - startTime < 20000UL))
; // waiting for 20 ms for the answer
// Read response
if(Serial1.available() >= 3) { // >=3 accept also more bytes
response[0] = (byte)Serial1.read();
response[1] = (byte)Serial1.read();
response[2] = (byte)Serial1.read();
}
if ((byte)(response[0] + response[1] + response[2]) == 0)
{
return (double)((response[0] << 8) + (response[1]));
}
else
{
Serial.println("Error communicating with device!");
return 0;
}
}