Servo prelling under G4P

edited November 2014 in Arduino

Hi everyone ! I made a GUI with GUI building tool, but i have some problems. My servos are prelling, and i don't know , where is the problem O.o. Can anybody help ?

arduino code: #include <Servo.h>

    Servo myservo1;              // create servo object to control a servo
    Servo myservo2;              // create servo object to control a servo
    Servo myservo3;              // create servo object to control a servo
    Servo myservo4;              // create servo object to control a servo


    void setup()
    {
      myservo1.attach(9);        
      myservo2.attach(10);
      myservo3.attach(11);
      myservo4.attach(3);
      Serial.begin(9600);       
    }

    void loop()
    {
      static int v = 0;
      if (Serial.available())  
      {
        char ch=Serial.read();
        switch(ch) 
        { 
        case '0'...'9': 
          v = v * 10 + ch - '0';
          break;
        case 'a':
          myservo1.write(v);
          v = 0;
          break;
        case 'b':
          myservo2.write(v);
          v = 0;
          break;
        case 'c':
          myservo3.write(v);
          v = 0;
          break;
        case 'd':
          myservo4.write(v);
          v = 0;
          break;
        }
      }

      delay(15);               
    }

processing code:

import g4p_controls.*;
import processing.serial.*;

Serial port;        // create serial port object
int val;            // variable to receive data from the serial port
float mx = 0.0;
boolean firstTime = true;

int angle_Servo1;
int angle_Servo2;
int angle_Servo3;
int angle_Servo4;
int Servo1_direction;
int Servo2_direction;
int Servo3_direction;
int Servo4_direction;
public void setup() {
  size(900, 600, JAVA2D);
  noStroke();
  frameRate(10);
  println(Serial.list());
  port = new Serial(this, Serial.list()[0], 9600);
  createGUI();
  customGUI();
  // Place your setup code here
}

public void draw() {
  if (firstTime) {
    delay(3000);
    firstTime = false;
  }
  background(230);

  if (Servo1_direction==0) {
    port.write(angle_Servo1+"a");
  }
  if (Servo1_direction==1) {
    port.write((180-angle_Servo1)+"a");
  }
  if (Servo2_direction==0) {
    port.write(angle_Servo2+"b");
  }
  if (Servo2_direction==1) {
    port.write((180-angle_Servo2)+"b");
  }
  if (Servo3_direction==0) {
    port.write(angle_Servo3+"c");
  }
  if (Servo3_direction==1) {
    port.write((180-angle_Servo3)+"c");
  }
  if (Servo4_direction==0) {
    port.write(angle_Servo4+"d");
  }
  if (Servo4_direction==1) {
    port.write((180-angle_Servo4)+"d");
  }
}

// Use this method to add additional statements
// to customise the GUI controls
public void customGUI() {
}

g4p code:

/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
 // It is safe to enter your event code here  
 } //_CODE_:button1:12356:

 * Do not rename this tab!
 * =========================================================
 */

public void slider_Servo_1_change1(GCustomSlider source, GEvent event) { //_CODE_:slider_Servo_1:297470:
  angle_Servo1=slider_Servo_1.getValueI();
} //_CODE_:slider_Servo_1:297470:

public void slider_Servo_2_change1(GCustomSlider source, GEvent event) { //_CODE_:slider_Servo_2:794899:
  angle_Servo2=slider_Servo_2.getValueI();
} //_CODE_:slider_Servo_2:794899:

public void slider_Servo_3_change1(GCustomSlider source, GEvent event) { //_CODE_:slider_Servo_3:595541:
  angle_Servo3=slider_Servo_3.getValueI();
} //_CODE_:slider_Servo_3:595541:

public void slider_Servo_4_change1(GCustomSlider source, GEvent event) { //_CODE_:slider_Servo_4:797455:
  angle_Servo4=slider_Servo_4.getValueI();
} //_CODE_:slider_Servo_4:797455:

public void f_b_1way_click1(GDropList source, GEvent event) { //_CODE_:f_b_1way:691544:
  Servo1_direction=f_b_1way.getSelectedIndex();
} //_CODE_:f_b_1way:691544:

public void f_b_2way_click1(GDropList source, GEvent event) { //_CODE_:f_b_2way:449380:
  Servo2_direction=f_b_1way.getSelectedIndex();
} //_CODE_:f_b_2way:449380:

public void f_b_3way_click1(GDropList source, GEvent event) { //_CODE_:f_b_3way:685694:
  Servo3_direction=f_b_1way.getSelectedIndex();
} //_CODE_:f_b_3way:685694:

public void f_b_4way_click1(GDropList source, GEvent event) { //_CODE_:f_b_4way:449430:
  Servo4_direction=f_b_1way.getSelectedIndex();
} //_CODE_:f_b_4way:449430:



// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI() {
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  if (frame != null)
    frame.setTitle("ServoControllv1.2");
  slider_Servo_1 = new GCustomSlider(this, 126, 18, 378, 54, "grey_blue");
  slider_Servo_1.setShowValue(true);
  slider_Servo_1.setShowLimits(true);
  slider_Servo_1.setLimits(0.0, 0.0, 180.0);
  slider_Servo_1.setNbrTicks(90);
  slider_Servo_1.setShowTicks(true);
  slider_Servo_1.setNumberFormat(G4P.DECIMAL, 0);
  slider_Servo_1.setOpaque(false);
  slider_Servo_1.addEventHandler(this, "slider_Servo_1_change1");
  lservo1label = new GLabel(this, 18, 18, 108, 54);
  lservo1label.setText("Servo: 1");
  lservo1label.setTextBold();
  lservo1label.setOpaque(false);
  lservo2label = new GLabel(this, 18, 72, 108, 54);
  lservo2label.setText("Servo: 2");
  lservo2label.setTextBold();
  lservo2label.setOpaque(false);
  slider_Servo_2 = new GCustomSlider(this, 126, 72, 378, 54, "grey_blue");
  slider_Servo_2.setShowValue(true);
  slider_Servo_2.setShowLimits(true);
  slider_Servo_2.setLimits(2.0, 1.0, 180.0);
  slider_Servo_2.setNbrTicks(90);
  slider_Servo_2.setShowTicks(true);
  slider_Servo_2.setNumberFormat(G4P.DECIMAL, 0);
  slider_Servo_2.setOpaque(false);
  slider_Servo_2.addEventHandler(this, "slider_Servo_2_change1");
  lservo3label = new GLabel(this, 18, 126, 108, 54);
  lservo3label.setText("Servo: 3");
  lservo3label.setTextBold();
  lservo3label.setOpaque(false);
  slider_Servo_3 = new GCustomSlider(this, 126, 126, 378, 54, "grey_blue");
  slider_Servo_3.setShowValue(true);
  slider_Servo_3.setShowLimits(true);
  slider_Servo_3.setLimits(0.0, 0.0, 180.0);
  slider_Servo_3.setNbrTicks(90);
  slider_Servo_3.setShowTicks(true);
  slider_Servo_3.setNumberFormat(G4P.DECIMAL, 0);
  slider_Servo_3.setOpaque(false);
  slider_Servo_3.addEventHandler(this, "slider_Servo_3_change1");
  f_b_1way = new GDropList(this, 504, 18, 126, 162, 2);
  f_b_1way.setItems(loadStrings("list_691544"), 1);
  f_b_1way.addEventHandler(this, "f_b_1way_click1");
  f_b_2way = new GDropList(this, 504, 72, 126, 162, 2);
  f_b_2way.setItems(loadStrings("list_449380"), 1);
  f_b_2way.addEventHandler(this, "f_b_2way_click1");
  f_b_3way = new GDropList(this, 504, 126, 126, 162, 2);
  f_b_3way.setItems(loadStrings("list_685694"), 1);
  f_b_3way.addEventHandler(this, "f_b_3way_click1");
  lservo4label = new GLabel(this, 18, 180, 108, 54);
  lservo4label.setText("Servo: 4 ");
  lservo4label.setTextBold();
  lservo4label.setOpaque(false);
  slider_Servo_4 = new GCustomSlider(this, 126, 180, 378, 54, "grey_blue");
  slider_Servo_4.setShowValue(true);
  slider_Servo_4.setShowLimits(true);
  slider_Servo_4.setLimits(0.0, 0.0, 180.0);
  slider_Servo_4.setNbrTicks(90);
  slider_Servo_4.setShowTicks(true);
  slider_Servo_4.setNumberFormat(G4P.DECIMAL, 0);
  slider_Servo_4.setOpaque(false);
  slider_Servo_4.addEventHandler(this, "slider_Servo_4_change1");
  f_b_4way = new GDropList(this, 504, 180, 126, 162, 2);
  f_b_4way.setItems(loadStrings("list_449430"), 1);
  f_b_4way.addEventHandler(this, "f_b_4way_click1");
}

// Variable declarations 
// autogenerated do not edit
GCustomSlider slider_Servo_1; 
GLabel lservo1label; 
GLabel lservo2label; 
GCustomSlider slider_Servo_2; 
GLabel lservo3label; 
GCustomSlider slider_Servo_3; 
GDropList f_b_1way; 
GDropList f_b_2way; 
GDropList f_b_3way; 
GLabel lservo4label; 
GCustomSlider slider_Servo_4; 
GDropList f_b_4way; 

Sorry for pasting the whole code , but i new in processing, and i learn by myself . I really don't know what the matter is . Thanks for help :)

Tagged:

Answers

  • What does prelling mean?

  • It is not rotating , it is jumping beetwen to angle position... I will upload a video if it is necessary.

  • edited November 2014

    When does the prelling occur?

    I can't see anything in the G4P code to cause concern so it is probably in the logic somewhere.

    I notice in line 79 you set the slider limits to 1-180 (starting with 2) when all the other limits are 0-180 (starting with 0) is that correct?

    I have a couple of ideas but they depend on the meaning of

    if (Servo1_direction==0) {
      port.write(angle_Servo1+"a");
    }
    

    does this rotate to server TO the angle angle_Servo1 i.e. absolute, or BY the angle angle_Servo1 i.e. relative to its current position?

  • edited November 2014

    Thanks for your help :) !Sorry 'cause my English is not the best ... I think the G4P gDropList cause the problem . Maybe there is something problem with the "time"or with the gui framerate or writing to the serial com. Maybe , in everyframe the code is initializing the droplist . So this is why my servos are jumping between two position . Now: i set the framerate to 5 , and i have made some changes in the gui code :

     public void f_b_1way_click1(GDropList source, GEvent event) { //_CODE_:f_b_1way:691544:
            if(Servo1_direction!=f_b_1way.getSelectedIndex())
             { Servo1_direction=f_b_1way.getSelectedIndex();}
            } //_CODE_:f_b_1way:691544:
    
    • so now it is working , but still not the best . The processing code write the angle to the serial communication ,and the arduino code is reading the serial. The port.write(angle_Servo1+"a"); is the output data . And the "myservo?.write(x) " function set the servos angle to the x value . This is an absolute position . There is no problem in the line 79 , it is not influencing the logic of the code. :)
  • edited November 2014 Answer ✓

    The problem is not the GDropList it is the draw method which is executed 60 fps or in your case 5 fps so is send huge amounts of data to the serial port.

    I would remove these statements from draw and modify the event handlers like this.

    public void f_b_1way_click1(GDropList source, GEvent event) { //_CODE_:f_b_1way:691544:
      if (Servo1_direction!=f_b_1way.getSelectedIndex()) {
        Servo1_direction=f_b_1way.getSelectedIndex();
        if (Servo1_direction==0) {
          port.write(angle_Servo1+"a");
        }
        else if (Servo1_direction==1) {
          port.write((180-angle_Servo1)+"a");
        }
      }
    } //_CODE_:f_b_1way:691544:
    

    Then repeat with the other 3 (b, c & d)

    The draw method would become

    public void draw() {
      background(230);
    }
    

    and move the delay(3000); to the last line in setup since this method is executed just once.

  • Thank you very much :)

Sign In or Register to comment.