Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //g++ -o -std=C++11 servo servo.cc -lwiringPi -lpthread
- #include <iostream>
- #include <wiringPi.h>
- #include <softPwm.h>
- using namespace std;
- #define INIT_VALUE 4//Valeur initial servo moteur
- #define RANGE 200//Valeur final servo moteur
- #define OPEN 13 //Valeur 90 degrees servo 13
- #define CLOSE 4 //Valeur 0 degrees servo
- class servo{
- public:
- servo()=delete;//Pas de constructeur par default
- servo(int pin);//Constructeur avec num. pin GPIO
- void test(int pin);//Test Servo Moteur
- void testAngle(int pin,int angle);
- private:
- int create(int pin);
- void open (int pin);
- void close (int pin);
- };
- servo::servo(int pin){ create(pin);}
- int servo::create(int pin){return softPwmCreate(pin,INIT_VALUE,RANGE);}//Cree pwm gpio pins
- void servo::open(int pin){softPwmWrite(pin,OPEN);} //90 degrees
- void servo::close(int pin){softPwmWrite(pin,CLOSE);}//0 degrees
- void servo::test (int pin){close (pin);delay(1000);open(pin);delay(1000);close(pin);delay(1000);} //Test 0-90 degrees
- void servo::testAngle(int pin,int pas){
- int angle(CLOSE);
- while (angle<=OPEN){
- cout <<"angle ="<<angle<<endl;
- softPwmWrite(pin,angle);
- delay(1000);
- angle+=pas;
- }
- close(pin);
- delay(1000);
- }
- int main (){
- int pin(0);
- if (wiringPiSetupPhys()==-1){cout <<"Error !"<<endl;return 1;}//Gpio ping
- cout <<"Pin. Num Raspberry"<<endl;
- cin >> pin;
- servo servos(pin);//Cree servo
- servos.test(pin);//Test servo 0-90
- servos.testAngle(pin,1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement