怎样快速的将arduino读到的数据从串口输出?
小白学生党一枚,最近做课题开始用到arduino,把霍尔元件传感器连到anolog的管脚想要读数然后进行处理,但是用serial.print读数太慢,用示波器测试能画出好...
小白学生党一枚,最近做课题开始用到arduino,把霍尔元件传感器连到anolog的管脚想要读数然后进行处理,但是用serial.print读数太慢,用示波器测试能画出好看的方波,但用arduino取到的数据太慢,画不出波形,求指点怎样更快速地读进来数据?
代码是这样的(有一部分是读imu板子的),不胜感激
#include <TextFinder.h>
#define PERIOD 1 // [ms]
/*** Vars for IMU ***/
TextFinder finder(Serial3);
const int NUMBER_OF_FIELDS = 3;
float rpy[NUMBER_OF_FIELDS];
int Pina = 0;
int Pinb = 1;
int va = 0;
int vb = 0; /
void setup()
{
Serial.begin(57600);
Serial3.begin(57600); // init the Serial3 port to get data from the IMU
pinMode(Pina, INPUT);
pinMode(Pinb, INPUT);
delay(500);
initIMU();
}
void loop()
{
// print manager timer
static unsigned long timer = 0;
static unsigned long currentTime = 0;
/************************************************************/
/*** Request after a specific period for the data
/************************************************************/
currentTime = millis();
if(currentTime - timer >= PERIOD)
{
// Request one output frame from the IMU
// #f only requests one reply, replies are still bound to the internal 20ms (50Hz) time raster.
// So worst case delay that #f can add is 19.99ms.
va = analogRead(Pina);
vb = analogRead(Pinb);
Serial3.write("#f");
/************************************************************/
/*** Get the IMU values
/************************************************************/
int fieldIndex = 0;
// search the Serial Buffer as long as the header character is found
boolean found_HeaderChar = finder.find("#YPR=");
if (found_HeaderChar)
{
while(fieldIndex < NUMBER_OF_FIELDS)
{
rpy[fieldIndex++] = finder.getFloat();
}
}
Serial.print(va);
Serial.print(",");
Serial.print(vb);
Serial.print(",");
/************************************************************/
/*** Print out the values
/*** Format: yaw, pitch, roll, left_Encoder, right_Encoder
/************************************************************/
if (found_HeaderChar)
{
// print IMU values
for(fieldIndex=0; fieldIndex < NUMBER_OF_FIELDS; fieldIndex++)
{
Serial.print(rpy[fieldIndex]);
Serial.print(",");
}
Serial.println("");
}
timer = millis();
}
}
/********************************/
/*** Initialize Functions
/********************************/
void initIMU()
{
// Output CALIBRATED SENSOR data of all 9 axes in TEXT format. & Turn off continuous streaming output & Disable error message output
Serial3.write("#ot#o0#oe0");
Serial3.flush();
} 展开
代码是这样的(有一部分是读imu板子的),不胜感激
#include <TextFinder.h>
#define PERIOD 1 // [ms]
/*** Vars for IMU ***/
TextFinder finder(Serial3);
const int NUMBER_OF_FIELDS = 3;
float rpy[NUMBER_OF_FIELDS];
int Pina = 0;
int Pinb = 1;
int va = 0;
int vb = 0; /
void setup()
{
Serial.begin(57600);
Serial3.begin(57600); // init the Serial3 port to get data from the IMU
pinMode(Pina, INPUT);
pinMode(Pinb, INPUT);
delay(500);
initIMU();
}
void loop()
{
// print manager timer
static unsigned long timer = 0;
static unsigned long currentTime = 0;
/************************************************************/
/*** Request after a specific period for the data
/************************************************************/
currentTime = millis();
if(currentTime - timer >= PERIOD)
{
// Request one output frame from the IMU
// #f only requests one reply, replies are still bound to the internal 20ms (50Hz) time raster.
// So worst case delay that #f can add is 19.99ms.
va = analogRead(Pina);
vb = analogRead(Pinb);
Serial3.write("#f");
/************************************************************/
/*** Get the IMU values
/************************************************************/
int fieldIndex = 0;
// search the Serial Buffer as long as the header character is found
boolean found_HeaderChar = finder.find("#YPR=");
if (found_HeaderChar)
{
while(fieldIndex < NUMBER_OF_FIELDS)
{
rpy[fieldIndex++] = finder.getFloat();
}
}
Serial.print(va);
Serial.print(",");
Serial.print(vb);
Serial.print(",");
/************************************************************/
/*** Print out the values
/*** Format: yaw, pitch, roll, left_Encoder, right_Encoder
/************************************************************/
if (found_HeaderChar)
{
// print IMU values
for(fieldIndex=0; fieldIndex < NUMBER_OF_FIELDS; fieldIndex++)
{
Serial.print(rpy[fieldIndex]);
Serial.print(",");
}
Serial.println("");
}
timer = millis();
}
}
/********************************/
/*** Initialize Functions
/********************************/
void initIMU()
{
// Output CALIBRATED SENSOR data of all 9 axes in TEXT format. & Turn off continuous streaming output & Disable error message output
Serial3.write("#ot#o0#oe0");
Serial3.flush();
} 展开
1个回答
2014-08-13
展开全部
你把串口速度升上去应该就没有问题了 你试试 115200吧
更多追问追答
追问
我改了,不行呢,因为imu板子的输出是57600,这里改成115200以后度数变得特别慢了TAT
追答
忽然想起来,你是怎么看输出数据的?是那种串口助手一行行显示出来的么?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询