2016-06-21 65 views
0

我一直在研究一个项目,我需要根据保存在SD卡上的数据来控制伺服电机。到目前为止它进展顺利,但是我在控制伺服电机运动的时间和速度方面存在问题。我会解释我想要完成的事情和一些示例代码。带有SD卡数据的Arduino伺服电机控制

我正在使用Arduino Mega并附加了一个SD模块。在SD卡上,我有四个不同的.txt文件。每个文件包含30个整数值,每行包含一个整数,并以(,)结尾。这仅仅是测试数据,所以我可以扫描一系列角度来检查我正在阅读并将值转换得很好。但是当我尝试使用定时器,延迟等来减慢伺服器时,它会像代码一样在代码中加速。在我的情况下,代码如下所示:

string dataRead ="";     // String object to read bytes from 
unsigned long int motorTime = 250; // Refresh time of the motor (250 ms)  
unsigned long int lastMotor = (long) micros(); 
while (entry.available()) {   // While there are bytes in the file to be read 
    dataRead = entry.readStringUntil(','); 
    while((long) micros() - lastMotor <= (motorTime * 1000)); // Do nothing until refresh time has elapsed 

    myServo.write(dataRead.toInt()); 
    lastMotor = (long) micros(); 
} 

的数据读取罚款和电机根据数据移动,但显示时间代码只是似乎是否定的某些原因。我怀疑这是因为在Arduino IDE中的所有抽象层下都会启用和禁用各种硬件功能,并且由于某些原因导致延迟被取消。

有没有人有过这方面的经验?任何提示以设定的速度驱动伺服器?我的替代解决方案是将数据加载到数组中;但我不想冒着烧毁所有RAM的风险并导致其他问题。

在此先感谢!

回答

0

我最后修复了它。我在读取数据时禁用了中断,并且与定时器函数(如micros()和millis())混淆了;他们依靠中断来追踪所经过的时间。 分离中断服务例程而不是在默认情况下禁用它们可能更有意义。