我正在制作的程序应该控制一个连接到梯子的步进电机。我想要计算的变量告诉我梯子的百分比。当步进电机步进23400步时,梯子处于100%。
步进变量在我的ISR程序中递增,其中正在发送电机的步进信号。
这里可以看到ISR例程。
a[href]:after { content: none !important; }
由于变量@echo off
:Main
cls
echo Press 1 or 2!
choice /c 12 /N >nul
if errorlevel 2
goto :Option2
if errorlevel 1
goto :Option1
:::::::::::::::::::::::::::
:Option2
cls
echo You pressed the number 2!
pause >nul
goto :Main
::::::::::::::::::::::::::::::::::::::
:Option1
cls
echo You pressed the number 1!
pause >nul
goto :Main
是静态变量,因此只能在ISR的范围内查看,这就是我将值存储在ISR(TIMER1_COMPA_vect)
{
// Holds next delay period.
unsigned int new_first_step_delay;
// Remember the last step delay used when accelrating.
static int last_accel_delay;
// Counting steps when moving.
static unsigned int step_count = 0;
// Keep track of remainder from new_step-delay calculation to incrase accurancy
static unsigned int rest = 0;
OCR1A = profile.first_step_delay;
switch (profile.run_state)
{
case STOP:
profile.moved_steps = step_count;
// Stop the timer, No clock source
break;
case ACCEL:
digitalWrite(step_pin,!digitalRead(step_pin));
step_count++;
profile.accel_count++;
// Do something
// Chech if we should start decelration.
if (step_count >= profile.decel_start)
{
profile.accel_count = profile.decel_length;
profile.run_state = DECEL;
}
// Chech if we hitted max speed.
else if (new_first_step_delay <= profile.min_time_delay)
{
last_accel_delay = new_first_step_delay;
new_first_step_delay = profile.min_time_delay;
rest = 0;
profile.run_state = RUN;
}
break;
case RUN:
digitalWrite(step_pin,!digitalRead(step_pin));
step_count++;
new_first_step_delay = profile.min_time_delay;
// Do something
if (step_count >= profile.decel_start)
{
profile.run_state = DECEL;
}
break;
case DECEL:
digitalWrite(step_pin,!digitalRead(step_pin));
step_count++;
profile.accel_count++;
// Do something
if(profile.accel_count >= 0)
{
digitalWrite(en_pin,!digitalRead(en_pin));
profile.moved_steps = step_count;
moved_to_position=true;
profile.run_state = STOP;
}
break;
}
profile.first_step_delay = new_first_step_delay;
if(moved_to_position==true)
{
moved_to_position = false;
}
}
中的原因,这样我就可以在范围之外使用它;
step_count
是struct profile.moved_steps = step_count
的实例化,它被实例化为全局变量,可以在任何地方读取。
结构定义如下:
profile
speed_profile
用于计算百分比,它会在此函数中的ros主题上不断发布。
typedef struct
{
volatile unsigned char run_state : 3; // Determining the state of the speed profile
volatile unsigned char dir: 1; // Determining the direction the motor has to move - Start being CCW
volatile unsigned int first_step_delay; // Period time for the next timer_delay, determines the acceleration rate.
volatile unsigned int decel_start; // Determines at which step the deceleration should begin.
volatile signed int decel_length; // Set the deceleration length
volatile signed int min_time_delay; //minium time required time_delay for achieving the wanted speed.
volatile signed int accel_count; // counter used when computing step_delay for acceleration/decelleration.
volatile unsigned int moved_steps; // Used by ros to publish current tipper position
volatile unsigned int current_step_position; // contains the current step_position
volatile unsigned int step_counter; //used for debug purpose
}speed_profile;
两个线程之间切换(一个global_state = state或false)。 当一个ros节点向它所订阅的主题发送消息时,global_state的状态变为true,并且在电机移动之后,global_state将返回false状态并等待外部消息。
所以问题是我想要计算百分比
profile.moved_steps
似乎计算没有任何影响。它不断返回电机步进的步数而不是百分比? - 我很不确定为什么会这样。
答案 0 :(得分:1)
从我可以收集的调试信息中我怀疑第二次从未到达。
您可以通过在第一个和第二个while
循环中从第一个和第二个发布调用/消息中发布两个不同(但常量)的值来确认/拒绝这一点。例如,整数1
和2
。
也许更好地发布global_state
变量的值,看看你是否得到true
。
status_step_count.data = global_state;
chatter.publish( &status_step_count);