- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将一些 C++ 代码转换为 Arduino 时遇到问题。任何帮助,将不胜感激。
编辑
我已经成功完成了上述操作。然而,现在唯一的问题是我的 Arduino 代码准确而正确地读取了电压,但没有其他寄存器。我也可以写 throttle 。如果我拨打不同数量的 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!");
}
}
最佳答案
没办法,ReadWriteRegister
将工作。在 115200 处,发送或接收每个字符大约需要 87us。那时,Arduino 可以执行大约 100 行代码!
看看这个片段:
Serial1.write(command, 5);
// Read response
if(Serial1.available() == 3) {
write
函数只将命令放入输出缓冲区并开始发送第一个字符。它在传输所有字符之前返回。这将需要 500us!
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();
// 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
我认为,外加法导致您丢失了前 8 位。如上所述计算应该给出正确的答案。 ReadWriteRegister
中使用的校验和。以上功能。该函数可以告诉您它是否超时或校验和是否错误。这也意味着可能需要更长的超时时间。不清楚您的设备是等待 480 毫秒来获取最新值,还是持续缓存它们并立即使用从 ESC 接收到的最后一个值进行响应。但是,在长达 480 毫秒的时间内,写入不会反射(reflect)在读取值中,因为 ESC 需要时间来接收命令然后发送新值。见 ESC Castle Link protocol . ReadWriteRegister
函数返回一个 16 位整数,这样效率会更高。比较浮点数从来都不是好事。顺便说一句,double
只是单例float
在 8 位 Arduinos 上。 ReadWriteRegister
函数不需要 writemode
参数,因为寄存器编号决定了您是在写入还是读取设备。 throttle
值仅在设置中执行。 setup
(写入 5 个零字节并等待 250 毫秒),double
(参见 safeState
、 linkLiveStatus
和 eStopStatus
)、 reg
发生错误时的编号。 read
将返回 -1 或 0xFF 字节)。
read
声明,但谁知道是哪一个?它将是随机的,基于耗时。
write
命令
或 Serial.print 阻塞直到输出有空间容纳最新的命令。 (这解决了您问题的标题。)
Serial1.write
或
Serial.print
返回。同时,接收到的字符将进入输入缓冲区。 (这就是它们的存储位置,直到您调用
read
。)
read
一行中的语句实际上将获取设备发送的字符。但是由于之前随机消耗字符,它可能是一个响应的最后一个字符,然后是下一个响应的前两个字符。您与 3 字节响应“不同步”。
flush
,然后等待响应返回
while
.
关于c++ - Serial.println() 影响 Serial1 读数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451657/
我在将一些 C++ 代码转换为 Arduino 时遇到问题。任何帮助,将不胜感激。 编辑 我已经成功完成了上述操作。然而,现在唯一的问题是我的 Arduino 代码准确而正确地读取了电压,但没有其他寄
我需要能够从 HealthKit 读取所有 HRV 读数,并根据它们的创建日期对它们的值进行排序。 我可以使用 SampleQuery 从 HealthKit 读取特定时间间隔内的所有读数,如下所示:
我正在尝试使用 arduino uno R3 从 DHT-11 传感器读取温度和湿度 #include #include #define DHTPIN A3 #define DHTTYPE DHT
伙计们,我是 Meteor 的新手。对于我目前的应用程序,我正在使用 openlayer,因此对于 openlayer,我调用 Template.map.onRendered 事件,该事件将加载一个
我有一个设备可以读取电气设备的 kw 值,以测量它们在特定时间的(能量消耗率)。然后将这些值发送到轮询器(它定期向设备询问这些值),并插入到数据库中。 例子: 1st reading - 10 kw
我是一名优秀的程序员,十分优秀!