Reputation: 111
I connected the motors from the quadcopter to the Raspberry Pi Zero. But when I try to rotate the motor, i.e. setting the speed, it turns out, the motors start spinning very quickly, they can also turn off periodically, and then start spinning again at high speeds. How can I change the speed? And why do they turn off sometimes?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <wiringPi.h>
#define RIGHT_MOTOR 1
#define LEFT_MOTOR 4
void init_ESC(int num);
void set_speed(int num, int speed);
int main ()
{
int speed = 3320;
wiringPiSetup();
pinMode(RIGHT_MOTOR, PWM_OUTPUT);
pinMode(LEFT_MOTOR, OUTPUT);
//pwmSetMode(PWM_MODE_MS);
//pwmSetClock(50 * pow(10, 6) / 1920 / 1024);
init_ESC(RIGHT_MOTOR);
delay(1000);
while (1)
{
set_speed(RIGHT_MOTOR, speed);
//set_speed(LEFT_MOTOR, speed);
//printf("speed = %d\n", speed);
}
return 0;
}
void init_ESC(int num){
pwmWrite(num, 0);
delay(20);
pwmWrite(num, 1024);
delay(1);
}
void set_speed(int num, int speed){
pwmWrite(num, 0);
delay(20);
pwmWrite(num, 1024);
pwmWrite(num, 0);
delay(20);
pwmWrite(num, 1024);
delayMicroseconds(1100);
}
Upvotes: 0
Views: 274
Reputation: 111
Thanks a lot, I used the pigpio.h library.
Compile comand: gcc pulse.c -pthread -lpigpio -lrt
It is necessary to use the gpioServo function (pin, pulse);
The values should be in the range from 1000 to 2000, since when working with these motors a signal with a different time interval is needed, the speed depends on it. From 1000 to 2000 are microseconds. Here is a small diagram:
But with PWM SIGNAL, I didn’t quite understand how to change the PWM frequency, or is it 50 by default?
Scheme ESC to Arduino or Raspbarry Pi
VERY IMPORTANT!!! WARGNING !!! I highly recommend setting microsecond values (speeds) to no more than 1000-1200, as the speed is very high and you can injure yourself. Be careful.
There are still different libraries and other languages, they are presented on this site:
#include <stdio.h>
#include <pigpio.h>
int main(){
if(gpioInitialise() < 0){
fprintf(stderr, "pigpio initialisation faled\n");
return 1;
}
gpioSetMode(23, PI_OUTPUT);
//Unlock ESC
gpioServo(23, 1500);
time_sleep(1);
int speed = 1200;
while(1){
//for(int speed = 1100; speed < 2000; speed+=100){
//for(int i = 0; i < 20; i++){
gpioServo(23, speed);
//time_sleep(0.2);
//}
//printf("us = %d\n", speed);
//}
}
return 0;
}
Upvotes: 0
Reputation: 3494
It is hard to achieve good servo/esc control with a busy loop. Even if your program is otherwise correct, the os will at times run other processes and your timing will suffer.
I would recommend using the servoblaster software : https://github.com/richardghirst/PiBits/tree/master/ServoBlaster . It uses interrupts and dma hardware in the pi to maintain good accuracy.
Upvotes: 0