首页 > 解决方案 > Tivaware Project - 需要帮助返回计数变量

问题描述

感谢您帮助我并尝试帮助我完成深夜的怪物燃料编码会议。现在我正在使用 Tivaware 板和步进电机以及步进电机驱动板。我遇到的问题是我设置了名为 step() 的状态函数,它将通过步进电机的一步。我想编写一个函数,当我输入一个角度时,它会将角度转换为步数,电机会转动多少度。我用步进电机转 1 度的转换是 6 步到 1.059921 度,所以步数 = ((600* 所需角度)+53)/(106) 加 53 是由于整数数学和舍入。我目前的代码面临的问题是,当我将其置于角度模式时,它会卡住并且不想执行这些步骤。我认为 for 循环会起作用,但它没有 t,我不确定为什么。连续模式工作得很好,但不是角度模式。我也不想使用指针,因为如果它只是基于整数的,我会获得额外的功劳。你们可以提供的任何帮助都会很棒,并且非常感谢您帮助我了解我为什么愚蠢。另外,请注意,g_clock 是在其他地方定义的,它只是一个布尔值,它告诉将哪个 if-else 分支用于有限状态机。另外,我对编码还是很陌生,我不确定我是在写 c 还是 C++,我只知道我在用某种不是 C# 的 C 语言编写。再次感谢您为我提供的任何帮助。s 只是基于整数的。你们可以提供的任何帮助都会很棒,并且非常感谢您帮助我了解我为什么愚蠢。另外,请注意,g_clock 是在其他地方定义的,它只是一个布尔值,它告诉将哪个 if-else 分支用于有限状态机。另外,我对编码还是很陌生,我不确定我是在写 c 还是 C++,我只知道我在用某种不是 C# 的 C 语言编写。再次感谢您为我提供的任何帮助。s 只是基于整数的。你们可以提供的任何帮助都会很棒,并且非常感谢您帮助我了解我为什么愚蠢。另外,请注意,g_clock 是在其他地方定义的,它只是一个布尔值,它告诉将哪个 if-else 分支用于有限状态机。另外,我对编码还是很陌生,我不确定我是在写 c 还是 C++,我只知道我在用某种不是 C# 的 C 语言编写。再次感谢您为我提供的任何帮助。我用某种不是 C# 的 C 语言编写。再次感谢您为我提供的任何帮助。我用某种不是 C# 的 C 语言编写。再次感谢您为我提供的任何帮助。

void Step()
{

    static uint32_t state;
    bool cw;
    cw = g_clockwise;

    //Your finite state machine code for moving the motor one step

    if(cw)
    {
        if (state == 0){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_A|phase_notB);
            state = 1;
        }

        else if (state == 1){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_B|phase_notB);
            state = 2;
        }
        else if (state == 2){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_B|phase_notA);
            state = 3;
        }
        else if (state == 3){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_A|phase_notA);
            state = 0;
        }

    }

    if (!cw)
    {
        if (state == 0){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_B|phase_notA);
            state = 3;
        }

        else if (state == 1){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_A|phase_notA);
            state = 0;
        }
        else if (state == 2){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_A|phase_notB);
            state = 1;
        }
        else if (state == 3){
            GPIOPinWrite(GPIO_PORTE_BASE,stepper_pins,phase_B|phase_notB);
            state = 2;
        }

    }


}

void StepperModeControl()
{

    static int count;
    static int angle_step_count;
    if (g_new_angle_step_flag)     //New angle mode A command received from keyboard reset angle_step_count
    {
        if (g_stepper_mode == g_angle_mode){
            //Step 4 Your code for calculating number of steps for given angle g_angle

            angle_step_count = (((600*g_angle)+53)/ 106); // i chose to multiply g_angle by 600 because it takes 6 steps to move the
            // motor 1.059921 degrees. I divided the whole thing by 106 because I rounded
            // 1.059921 to 106 and scaled 6 and 1.059921 by 100. I added 53 because half of
            // 106 and I'm wanting integers to be returned to the closest half after it's been rounded

        }
    }
    g_new_angle_step_flag = false;

    if (g_stepper_mode == g_angle_mode)
    {

        //Angle Mode
        //Step 5 Your code for stepping to the desired angle

        for(; count <= angle_step_count;)
        {
            Step();
            count++;
        }
    }
    else
    {

        //Continuous Mode
        Step();
    }
}

标签: c++c

解决方案


推荐阅读