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.