Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / PID + H-Bridge on: March 20, 2013, 08:18:29 pm
Hi

I'm trying to use an encoder and the servo library.  Where 90 on the servo is 0 movement - ie PID  has finished it's loop can br3ttb's PID be used in this fashion.  I'm using a polulu twin H-bridge which uses RC servo pulses like in servo out to control the H-bridge 90 is all stop and 180 full one direction and 0 full the other.  Here is the code so far.  Any help much appreciated.
Code:
/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html

 *Yn = k1 * (Xc - Xn) + k2 * (Xn - Xn-1),

Yn is the control output,
Xc is the control input,
Xn is the position feedback sample,
Xn-1 is the previous position feedback sample, and
k1 and k2 are the PID coefficients.
 * This example code is in the public domain.
 */

#include <Encoder.h>
#include <PID_v1.h>
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,1,5,1,DIRECT);



// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
 // inputString.reserve(200);
  Serial.println("Basic Encoder Test:");
 //  Input = analogRead(0);
 // Setpoint = 500;
 Output = 0;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  myPID.SetOutputLimits(0, 180);
  myPID.SetSampleTime(20);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

long oldPosition  = -999;
long newPosition = 0;
void loop() {

    // Read serial input:
 //    if (stringComplete = false) {
     //  Serial.println("next");
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    Serial.println(inChar);
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inputString += (char)inChar;
      Serial.println(inputString);
    }
    if (inChar == '*') {
      stringComplete = true;
    }
  //}
  while (stringComplete) {
  
 //   Serial.println(inputString);
 Setpoint = inputString.toInt();
   newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
     Input = newPosition;
  myPID.Compute();
 
Output = Output + 90;
//Output = constrain(Output, 90,180);
Output = map(Output, 0, 270, 0, 179); // scale it to use it with the servo (value between 0 and 180)
  myservo.write(Output);                  // sets the servo position according to the scaled value
//  analogWrite(4,Output);
  Serial.println(Output);
  if (newPosition == Setpoint) {
    // clear the string:
    myservo.write(90);
    inputString = "";
   stringComplete = false;
   Serial.println("enter new value");
  myEnc.write(0);
   Serial.println(newPosition);
  }
 }
  }
 
}
}


Moderator edit: [code] [/code] tags added.
2  Development / Other Software Development / Re: MAX7456 library update on: December 15, 2011, 12:49:06 am
thanks, compiled and uploaded, have fixed the dud character in the max unit. Still have a little maybe hardware issue where the characters only turn up after the video source is turned off, but the serial output shows that the voltage keeps on being read.  Thanks again will keep on playing
3  Development / Other Software Development / Re: MAX7456 library update on: December 14, 2011, 12:57:58 am
Thanks for the quick response.  The character set is stuck in the old one with the first space character corrupted so if you change the original d fraser code with the mapping in to char32 = any other character in the old table it will put in the correct character.  Using teraterm to up load and it takes the three minutes as suggested.
Downloaded the two files thank you and created a directory in the libraries folder and ran one of your first sample sketches but get this
error as we said fairly low skilled in C.  Any help appreciated

MAX7456_lib_test.cpp: In function 'void setup()':
MAX7456_lib_test:14: error: no matching function for call to 'MAX7456::begin()'
C:\Users\......\Downloads\arduino-0022\arduino-0022\libraries\MAX7456/MAX7456.h:105: note: candidates are: void MAX7456::begin(byte, boolean)
MAX7456_lib_test.cpp: In function 'void loop()':
MAX7456_lib_test:60: error: 'class MAX7456' has no member named 'noblink'
MAX7456_lib_test:67: error: 'class MAX7456' has no member named 'noblink'
MAX7456_lib_test:68: error: 'class MAX7456' has no member named 'noinvert'
4  Development / Other Software Development / Re: MAX7456 library update on: December 12, 2011, 11:41:15 pm
Hello,
We have been trying for two days to get the sparkfun max7456 going on some of D Frasers code and have up loaded the new character set.
The funny characters in the picture were there from the start before the new character sets.  we downloaded your library but winzip free trail version barffs on the file format.  Any chance of pointing towards a windows zip file of your work or please if anyone has a clue as to why the characters please let us know.  Skill levels fairly low on our side.  Thank you for your time
Pages: [1]