0

Currently I am having issues with a program running a freertos program. The purpose of the program is to control a stepper motor as well as an led. Implementing the motor control without microstepping does not have any issues as the two tasks take no parameters and call no functions.

However, when I introduce microstepping which requires two nested functions to be called by the move_routine task, the program will not do anything (no led flashing, no motor turning) when it did before. Does anyone have any solutions for this or any reasons on why this shouldn't work? From what I can see it should be fine to call a function from a freertos task.


#include <Arduino.h>
#include <Stepper.h>

/*================PIN DEFINITIONS================*/
#define LEDC_CHANNEL_0  0
#define LEDC_CHANNEL_1  1
#define LEDC_CHANNEL_2  2
#define LEDC_CHANNEL_3  3

const int A1A =  14;
const int A1B = 27;
const int B1A = 26;
const int B2A = 25;
const int ledPin = 33;

/*================VARIABLE DEFINITIONS================*/
int stepnumber = 0;
int Pa; int Pb;

const int stepsPerRev = 200;

Stepper myStepper(stepsPerRev, 14,27,26,25);


/*================Function Definitions================*/
  
//Analogwrite using LEDC capabilities
  void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {

    //calculation of duty cycle
    uint32_t duty = (4095/valueMax)*min(value, valueMax); 
    ledcWrite(channel,duty);
  }

  
  
void move(int stepnumber, int MAXpower, int wait) {

    Pa = (sin(stepnumber*0.098174)*MAXpower);
    Pb = (cos(stepnumber*0.098174)*MAXpower);

    if (Pa>0)
    { 
      ledcAnalogWrite(LEDC_CHANNEL_0,Pa);
      ledcAnalogWrite(LEDC_CHANNEL_1,0);
    }
    else
    {
      ledcAnalogWrite(LEDC_CHANNEL_0,0);
      ledcAnalogWrite(LEDC_CHANNEL_1,abs(Pa));
    }
    
    if (Pb>0)
    {
      ledcAnalogWrite(LEDC_CHANNEL_2,Pb);
      ledcAnalogWrite(LEDC_CHANNEL_3,0);
    }

    else
    {
      ledcAnalogWrite(LEDC_CHANNEL_2,0);
      ledcAnalogWrite(LEDC_CHANNEL_3,abs(Pb));
    }
}
 


void move_routine(void *parameters) {
  while(1) {
    for (int i=0; i<3199; i++)
    {
      stepnumber++;
      move(stepnumber,255,250);
      }

      vTaskDelay(3000/portTICK_PERIOD_MS);
      for (int i=0; i<1599; i++)
      { 
        stepnumber--;
        move(stepnumber,255,1000);
        }

      vTaskDelay(3000/portTICK_PERIOD_MS);
  }
}

void led_task(void * parameters){
  while(1){
    digitalWrite(ledPin, HIGH); 
    vTaskDelay(500/portTICK_PERIOD_MS); 
    digitalWrite(ledPin, LOW); 
    vTaskDelay(500/portTICK_PERIOD_MS);
  }
}

void setup(){
  myStepper.setSpeed(60);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);


  xTaskCreatePinnedToCore(
    move_routine, 
    "Move Routine",
    8192,
    NULL,
    1,
    NULL, 
    1
  );


  xTaskCreatePinnedToCore(
    led_task, 
    "LED Task", 
    1024, 
    NULL, 
    1, 
    NULL, 
    1
  );

}

void loop(){

}

Expected to see flashing led and motor turning but nothing witnessed

2
  • Hi @finsmy, welcome to Stack Overflow. What debugging have you done? Have you added code to output a message to confirm that the tasks are running? Have you tried a simpler program without the tasks that does nothing but blink the LED in loop() or turn the motor? That would help isolate the problem. Commented Dec 2, 2022 at 16:57
  • Hi there @romkey. Yes I have included Serial.print() commands in both of the tasks to show when different things happen but program will not do anything (serial prints or otherwise) using the code above. I can get the program to work by just flashing the led but when the motor control is implemented something goes wrong. With the thought that there was not enough memory allocated I also upped the allocation for move_routine to 8192. Thank you for your reply Commented Dec 2, 2022 at 17:31

1 Answer 1

0

Your two tasks work as it were defined, the problem is your ledcAnalogWrite() function where you are calling ledcWrite() function with nowhere in your code initialise the LEDC with ledcSetup() and ledcAttachPin().

Read the documentation on LED Control(LEDC).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.