pid control helicopter?
hello i have digital potentiometer mcp1642 wichi have pid library setvalue on arduino which varry betwwen 0-256 and give resistance from 0-5k and i connected it to remote control which has 400-4.2k ohm on manual resistance which 400 is max throttle and 4.2k is off motor and i have sonar sensore ez0 on helicopter thatread atlitude by resoloution of inch and send to arduino and make i used to calculate error and then make pid control i make simple code but i start to tune p but i think i should start d , can any one help me how can i start to tune this thing and is it true my scale in code i saw that at setvalue(111) helicopter hold good in sky so i decide to make it stable point which pid go around it ,or maybe the problem is the sonar resoloution ..g?
ecode
#include <SoftwareSerial.h>
#include "SPI.h" // necessary library
int ss=53; // using digital pin 10 for SPI slave select
int f=0;
char *buffer;
byte x;
char array[27];
int counter=0;
unsigned char r;
int p=0;
int last;
int error=0;
int wew;
int required ;
int kpipo;
int n;
void setup() {
pinMode(ss, OUTPUT); // we use this for SS pin
SPI.begin(); // wake up the SPI bus.
SPI.setBitOrder(MSBFIRST);
Serial.begin(9600);
Serial1.begin(9600);
}
void setValue(int value)
{
digitalWrite(ss, LOW);
SPI.transfer(0); // send command byte
SPI.transfer(value); // send value (0~255)
digitalWrite(ss, HIGH);
}
void loop()
{
while (Serial1.available())
{
x= Serial1.readBytes(buffer,1);
if(*buffer==0x52){
x= Serial1.readBytes(buffer,1);
array[0]=*buffer;
x= Serial1.readBytes(buffer,1);
array[1]=*buffer;
x= Serial1.readBytes(buffer,1);
array[2]=*buffer;
int Final_inch=(array[0]-48)*100 + (array[1]-48)*10 +(array[2]-48) ;
float Final_cm=Final_inch*2.54;
//Serial.print(Final_inch);
//Serial.println(" Inch ");
if (f==0){
//Serial.println(" ana hoon ");
setValue(35);
}
required = 50;
error = required-Final_inch;
Serial.print(error);
Serial.println(" ana hoon ");
p=abs(error);
if(Final_inch == 21){
f=1;}
if(Final_inch == 22){
f=1;}
if(Final_inch == 23){
f=1;}
if(f==1){
Serial.println(" ana hnak ");
if(error >0){
setValue(111-(error));}
if(error <0){
setValue(111+abs(error));}
}
}
}
}
Discussions
7 years ago
How high are you flying ? Bear in mind that sonar signals travel about 150mm in 1msec, so at some realistic flying height, the transit delay to decide the height may be a problem.
Also, as I said in your previous question, Arduino HAS an excellent PID library, with autotuning too. Use it.