Pages: [1]   Go Down
Author Topic: Controlling servos with ServoTimer2 and ServoDecode  (Read 561 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am currently trying to have the receiver and the servos of my rc car interface with arduino in order to be able to then convert it to an autonomous vehicle.
I have finally managed (with the help of AWOL) to decode the ppm signal of my receiver with the following sketch made by mem.

Code:
// ServoDecodeTest

#include <ServoDecode.h>
#undef round

char * stateStrings[] = {
  "NOT_SYNCHED", "ACQUIRING", "READY", "in Failsafe"};

void setup()   // run once, when the sketch starts
{
  Serial.begin(38400); 
  ServoDecode.begin();

    //setFailsafe()  can set a pulse width for a channel if the Tx signal is lost.
    //By default all channels not explicitly set will hold the last good signal
  ServoDecode.setFailsafe(3,1234); // set channel 3 failsafe pulse  width
}


void loop()
{
  int pulsewidth;

  // print the decoder state
  if( ServoDecode.getState()!= READY_state) {
    Serial.print("The decoder is ");
    Serial.println(stateStrings[ServoDecode.getState()]);
    for ( int i =0; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
Serial.print("Cx");
Serial.print(i);
Serial.print("= ");
pulsewidth = ServoDecode.GetChannelPulseWidth(i);
Serial.print(pulsewidth);
Serial.print("  ");
    }
    Serial.println("");
  }
  else {
    // decoder is ready, print the channel pulse widths
    for ( int i =1; i <=MAX_CHANNELS; i++ ){ // print the status of the first four channels
Serial.print("Ch");
Serial.print(i);
Serial.print("= ");
pulsewidth = ServoDecode.GetChannelPulseWidth(i);
Serial.print(pulsewidth);
Serial.print("  ");
    }
    Serial.println("");
  }
  delay(500); // update 2 times a second  
}

The next step is to control two servos based on this. I have found this sketch also by mem, but the servos aren't responding to any inputs, they just jerk occasionally to one side every 15 - 20 seconds.
Here is the sketch:
Code:
// ServoDecodeTest
#include <ServoDecode.h>
#include <ServoTimer2.h>// the servo library
#undef round


// define the servo pins
#define servo1Pin  5   // the pin the servo is attached to
#define servo2Pin  6

ServoTimer2 servo1;    // declare variables for the servos
ServoTimer2 servo2;

void setup()
{
  Serial.begin(38400);
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  ServoDecode.begin();
}


void loop()
{
  int pulsewidth[MAX_CHANNELS];

  if( ServoDecode.getState()!= READY_state) {
    Serial.println("The decoder is not ready");
  }
  else {
    // decoder is ready, do somthing with the channel pulse widths
    if( ServoDecode.GetChannelPulseWidth(1) > 1500){  // run script 1 if pulse width greater than 1500 ;
int argumentA = (2250 - ServoDecode.GetChannelPulseWidth(2)) / 200; // some values proportional to stick position
boolean argumentB = ServoDecode.GetChannelPulseWidth(2) < 1500; // true if channel 2 less than than 1500
runScript1(argumentA, argumentB);
    }
  }
  delay(100); // update 10 times a second
}

void  runScript1(int argumentA, boolean argumentB){
  // do something here to control servo...
  servo1.write(2000);
  delay(5000);  // note that all servo are pulsed by the interrupt handler with their current value every 20ms
  if(argumentA)
     servo2.write(2000);
  delay(argumentB * 10); //delay a time determined by the transmitter stick position
  servo1.write(1000);
  servo2.write(1000);
}

Thanks a lot!
Arthur
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bump
Logged

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8967
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
bump

Have you been successful at controlling two servos independent of the RC decoding?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply, sorry for the late anwser...
Yes I have, but not using servotimer2 but with the original servo library. I'll give it a try with servotimer2
Thanks a lot!
Arthur
Logged

Pages: [1]   Go Up
Jump to: