0

I am able to drive the step motor by using wave_chain() method of the pigpio library. Now, I have to drive the servo motor with stepper motor at same time.

The documentation says that wave_chain() method will cancel the hardware_PWM() call. So If I use hardware_PWM() for servo controlling, while stepper is driving in other thread, I wont be able to drive the servo at the same time? Am I right?

If it is, can I run servo and stepper motor simultaneously ?

I see that there are also servo controlling methods in the pigpiod library. Are they cancels the waves created by wave_chain() ? Are these servo methods works well with wave_chain() method ?

Edit : I tried the drive the servo and step motor simulatenously. And while I am running at the same time, servo motor starts to jitter and missing steps. Sometimes it goes to the target position, sometimes not. When I comment the step motor drive methods, servo goes well.

Here is the code I have tried ;

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pigpiod_if2.h>
#include <pthread.h>


int pi;

void* servo_thread_f(void* arg)
{
    set_mode(pi, 27, PI_OUTPUT);
    set_servo_pulsewidth(pi, 27, 2500);

    sleep(5);
    printf("Servo starting\n");
    while(1)
    {
        set_servo_pulsewidth(pi, 27, 2500);
        printf("1) Servo triggered up\n");
        usleep(500000);
        // this sleep can be usleep(500000)
        set_servo_pulsewidth(pi, 27, 2000);
        printf("2) Servo triggered down\n");
        usleep(500000);
        // this sleep can be usleep(500000)
    }
}

void generate_ramp(int step_pin, int ramp0, int ramp1);

int main()
{
    // Global is just a extern struct that contains the global variables.
    pi = pigpio_start(NULL, NULL);
    printf("pi : %d\n", pi);
    set_mode(pi, 20, PI_OUTPUT);

    pthread_t servo_thread;
    pthread_create(&servo_thread,NULL, &servo_thread_f, NULL);
    int dir = 1;

    while(1)
    {
        // go target position
        generate_ramp(20 , 3200, 1500);

        // change direction
        gpio_write(pi, 21, dir);
        dir = 1 - dir;
    }


    return 0;
}

void generate_ramp(int step_pin, int ramp0, int ramp1)
{
    int wid = -1;
    int f = ramp0;
    int micros = (int)(500000/f);
    gpioPulse_t pulses[2] =  {{1 << step_pin, 0, micros},{0, 1 << step_pin,micros}};
    wave_add_generic(pi, 2, pulses);

    wid = wave_create(pi);

    int steps = ramp1;
    int x = steps & 255;
    int y = steps >> 8;
    char wave[7] = {255, 0, wid, 255, 1 , x, y};
    wave_chain(pi, wave , 7);
    while(wave_tx_busy(pi));

    wave_delete(pi, wid);
}

Is there something that I have done wrong? The raspberry pi, servo motor power and step motor power have common ground.

Edit 2 :

Compiled by : gcc example.c -o example -lpthread -lpigpiod_if2 -lrt

  • With just one thread, for example drive servos in the main thread, there is no problem.

  • I put a printf before servo pulsewidth command. When this printf executes, I see the message and the motor moving. But these print() messages does not seem frequently. Thats why I am thinking it is about thread scheduling issue.

  • With just one thread, sleeping with usleep(500000) or with a lower value like usleep(100000) for servo move up and down, there is no problem. Servos really does these movements fast.

8
  • I changed the sleep and target position. usleep to sleep(1) and decrease the distance between two positions. (edited the question also) But same problem still continues. But when I disable the stepper commands, there is no problem. Old usleep version also works without stepper driving.
    – tango-1
    Commented Apr 16, 2021 at 21:50
  • Can it be about thread scheduling ? Since I am working on non-real time system, there is no guarantee that my servo commands will be executed at the right timestamp, Am I think right ? Maybe there are some another reasosn but this is my first guess.
    – tango-1
    Commented Apr 16, 2021 at 22:00
  • I added the headers now.
    – tango-1
    Commented Apr 17, 2021 at 9:10
  • I corrected the errors and ran the program. It seems okay to me. The servo signals are clean and change between 2200 and 2500 at regular inervals.
    – joan
    Commented Apr 17, 2021 at 9:26
  • Thank you joan. So the problem is threading right ? if the signalling true.
    – tango-1
    Commented Apr 17, 2021 at 9:29

1 Answer 1

0

I will assume you are using the pigpio Python module.

You are correct to say that hardware_PWM can not be used at the same time as wave_chain.

I suggest you use the set_servo_pulsewidth method instead. This method can be used at the same time as wave_chain and is stable enough for servo operation.

6
  • I am using pigpiod C interface. Is it possible with using C interface? since you said that you are assuming that I am using Python interface. Is it safe to call these servo and wave methods in different threads ? Should I care synchronization ?
    – tango-1
    Commented Apr 16, 2021 at 3:22
  • Why not try? It would be the quickest way to find the answer.
    – joan
    Commented Apr 16, 2021 at 5:20
  • You are exactly right. But in the documentation, I see that if I give wrong & too much pulses to the servo motor through servo methods, it can burn the servo. Thats why I was thinking the stepper and servo pwm signals can conflict and damage the motor. Thats why I am approaching slowly. Thanks for your help.
    – tango-1
    Commented Apr 16, 2021 at 5:33
  • And you explicitly assume the language as python, so I think there is a difference behaviour between C interface and python intertace.
    – tango-1
    Commented Apr 16, 2021 at 5:41
  • As a final comment I will point out that the Python module uses the C library via the pigpio daemon.
    – joan
    Commented Apr 16, 2021 at 7:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.