Arduino 的多速率多传感器读取
Multi-rate Multi-sensors reading for Ardruino
我刚买了一个新的 Arduino 来玩几个传感器来获取他们的数据。我从这里得到这段代码:
typedef void (*command)();
template <unsigned long wait_ms, command c>
void repeat() {
static unsigned long start = millis();
if (millis()-start >= wait_ms) {
start += wait_ms;
c();
}
}
void task1()
{
}
void task2()
{
}
void task3()
{
}
void setup(){
Serial.begin(4800);
}
void loop(){
int aba=1000;
repeat<1000, task2>();
repeat<5000, task1>();
repeat<3000, task3>();
}
上面的代码运行完美,没有任何问题。但是,当我像这样进行更改时
void loop(){
int aba=1000;
repeat<aba, task2>();
repeat<5000, task1>();
repeat<3000, task3>();
}
执行它时遇到问题。
局部变量aba
不是编译时常量。
如果您想要的是 wait_ms
的变量值(在 运行 时更改),那么您就不走运了。您需要实现一个可以使用 运行 时间值的非模板版本。
将编译时常量与模板一起使用通常会更有效,但是您使用不同的模板参数值对每个实例重复代码。
对于变量等待,你可以这样做:
typedef void (*command)();
void repeat(unsigned long wait_ms, command c, unsigned long &start);
void repeat(unsigned long wait_ms, command c, unsigned long &start) {
if (millis()-start >= wait_ms) {
start += wait_ms;
c();
}
}
static unsigned long task1Start, task2Start, task3Start;
void task1() {Serial.println("task 1");}
void task2() {Serial.println("task 2");}
void task3() {Serial.println("task 3");}
void setup(){
Serial.begin(9600);
task1Start = task2Start = task3Start = millis();
}
void loop(){
int aba=1000;
repeat( aba, task1, task1Start );
repeat( random(5000, 10000), task2, task2Start );
repeat( random(50, 500), task3, task3Start );
}
编辑:由于该函数不再是模板,该函数仅生成一个在每次调用之间共享的静态 start
。该模板基本上为每组输入重复代码,为每个任务提供唯一的 start
。
这可以通过添加第三个参数来解决,该参数为每个特定任务传递对 start
变量的引用。
如果您要使用许多任务,一个 Task
对象来封装每个任务可能 useful/easier 来处理。
我刚买了一个新的 Arduino 来玩几个传感器来获取他们的数据。我从这里得到这段代码:
typedef void (*command)();
template <unsigned long wait_ms, command c>
void repeat() {
static unsigned long start = millis();
if (millis()-start >= wait_ms) {
start += wait_ms;
c();
}
}
void task1()
{
}
void task2()
{
}
void task3()
{
}
void setup(){
Serial.begin(4800);
}
void loop(){
int aba=1000;
repeat<1000, task2>();
repeat<5000, task1>();
repeat<3000, task3>();
}
上面的代码运行完美,没有任何问题。但是,当我像这样进行更改时
void loop(){
int aba=1000;
repeat<aba, task2>();
repeat<5000, task1>();
repeat<3000, task3>();
}
执行它时遇到问题。
局部变量aba
不是编译时常量。
如果您想要的是 wait_ms
的变量值(在 运行 时更改),那么您就不走运了。您需要实现一个可以使用 运行 时间值的非模板版本。
将编译时常量与模板一起使用通常会更有效,但是您使用不同的模板参数值对每个实例重复代码。
对于变量等待,你可以这样做:
typedef void (*command)();
void repeat(unsigned long wait_ms, command c, unsigned long &start);
void repeat(unsigned long wait_ms, command c, unsigned long &start) {
if (millis()-start >= wait_ms) {
start += wait_ms;
c();
}
}
static unsigned long task1Start, task2Start, task3Start;
void task1() {Serial.println("task 1");}
void task2() {Serial.println("task 2");}
void task3() {Serial.println("task 3");}
void setup(){
Serial.begin(9600);
task1Start = task2Start = task3Start = millis();
}
void loop(){
int aba=1000;
repeat( aba, task1, task1Start );
repeat( random(5000, 10000), task2, task2Start );
repeat( random(50, 500), task3, task3Start );
}
编辑:由于该函数不再是模板,该函数仅生成一个在每次调用之间共享的静态 start
。该模板基本上为每组输入重复代码,为每个任务提供唯一的 start
。
这可以通过添加第三个参数来解决,该参数为每个特定任务传递对 start
变量的引用。
如果您要使用许多任务,一个 Task
对象来封装每个任务可能 useful/easier 来处理。