Wireless control of a stepper motor/servo ?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

the if seems to only be able to ignore one to three 0s
You mean one to 3 in a row? If there are too many in a row, the servo signal won't get refreshed in time.
This code will continue to refresh using the last good value:

void loop()
{
readPWM(READ_PIN);
Serial.println(freq);
Serial.println(duty);

if (duty != 0) // if value is non-zero - map to a servo position
{
val = map(duty, 0, 50, 0, 90); // scale the duty cycle to the range of servo motion
}
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

that helped a lot, can I now average "val" to smooth the signal to the servo even further ?

float val = val2; ?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

Yes. Keeping a running average will act as a low-pass filter and minimize the jitter even further.

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

However the above statement does not work, I tried it several times and the servo does not work. ?
int val;
int val2;
float val =val2
myservo.write(val2);

the above lines were in the appropriate locations in the sketch

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

Where are you calculating the average?

Code: Select all

int val;
int val2;
float val =val2
myservo.write(val2);
val2 is not initialized anywhere in that code. The value is undefined.

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

int val;
int val2; up at the very beginning

val = map(duty, 0, 50, 0, 90);
float val = val2;
myservo.write(val2);

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

I still don't see where you are assigning a value to val2 or calculating an average.

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

I will post up the sketch when I get home so you can have a look.

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

This made sense in my head and dont know what I have wrong

Code: Select all

#include <Servo.h>
    #define READ_PIN 13
    #define PWM_OUTPUT 10

   
    int potpin = 13;  // analog pin used to connect the potentiometer
    int val;    // variable to read the value from the analog pin
    int val2; 



    static double duty;
    static double freq;
    static long highTime = 0;
    static long lowTime = 0;
    static long tempPulse;
     
    Servo myservo;  // create servo object to control a servo
     
    void setup()
    {
      myservo.attach(10);  // attaches the servo on pin 9 to the servo object
      pinMode(READ_PIN,INPUT);
      Serial.begin(9600);
      analogWrite(PWM_OUTPUT,230);
    }
     
    void loop()
    {   
      readPWM(READ_PIN);
      //Serial.println(freq);
      //Serial.println(duty);
      Serial.println(val);
      if (duty != 0) // if value is non-zero - map to a servo position
{
      val = map(duty, 0, 50, 0, 90); // scale the duty cycle to the range of servo motion
      float val = val2;

}
      myservo.write(val2); // sets the servo position according to the scaled value
      delay(15); 
      
     
    
      //delay(20); 
    }
    //Takes in reading pins and outputs pwm frequency and duty cycle.
    void readPWM(int readPin)
    {
       highTime = 0;
       lowTime = 0;

       tempPulse = pulseIn(readPin,HIGH);
       if(tempPulse>highTime)
       {
          highTime = tempPulse;
       }

       tempPulse = pulseIn(readPin,LOW);
       if(tempPulse>lowTime)
       {
          lowTime = tempPulse;
       }

       freq = ((double) 1000000)/(double (lowTime+highTime));
       duty = (100*(highTime/(double (lowTime+highTime))));
       
     
  }


User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

Here you calculate 'val' then over-write it with 'val2' which is never initialized or assigned to anywhere:

Code: Select all

      val = map(duty, 0, 50, 0, 90); // scale the duty cycle to the range of servo motion
      float val = val2;
This should give you a running average of the last 8 reads:

Code: Select all

 if (duty != 0) // if value is non-zero - map to a servo position
{
      val -= val/8;
      val += map(duty, 0, 50, 0, 90); // scale the duty cycle to the range of servo motion
      val2 = val/8
}
myservo.write(val2); // sets the servo position according to the scaled value

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

I thought I initialized it at the top with int val2;

That actually made it worse,jitters and reset jerk persists.
May have to rethink this as the results dont seem useable. Funny how the servo example works so darn good

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

I thought I initialized it at the top with int val2;
That just declares it as an integer. It is never initialized with any value.
That actually made it worse,jitters and reset jerk persists.
Hmmm. Can you print out val2 and post the results here?

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

After going round and round I have found that the jerk was because the power supply couldnt deliver when I moved the pot quickly causing the jerk and 0 value.
I am going to order some Buck regulators in both 3.3 and 5v to power everything properly

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Wireless control of a stepper motor/servo ?

Post by adafruit_support_bill »

That makes sense. Let us know how it works with more power.

User avatar
mjpcarbon
 
Posts: 436
Joined: Fri Nov 29, 2013 8:57 pm

Re: Wireless control of a stepper motor/servo ?

Post by mjpcarbon »

Got my Buck Regulators, installed and made a world of difference slight jitter but very acceptable. No more crazy jerk.

Now I have another question, can the xBee do more than one thing.
I tried to configure the TX and RX following the sample but dont understand how it knows what pin on the TX is talking to what pin on the RX ?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”