Gimbal servo output for horizontal vertical readout , kk2.0 +APC220+arduino

now its using one aux to switch state of mode on the Arduino part and wil send a signal to aux for autolevel on or off ,
const int auxset =1800; is the AUX value transmited to the kk2.0 for autolevel ON ,, if you prefer to change this to 1000 value transmited to the kk2.0 for autolevel will be OFF
const int height =100; wil be the height prefered to reach in cm (100 cm = 1 meter )!
when starting system it will check if throttle input is near 0 for safety reasons !
need to set throttle on 0 startup !
when you start the quad on the ground and put ON height sensor with AUX switch , it wil auto increase throttle till height is reached of 100 cm.
when in flight and you are above height of 1 meter =100 cm,, it will decent to 1 meter ,,
this is how it works ,,
you can adjust the decent and increase rate with the system = sys -10; meanse 10 steps down of throttle at the time ,
system = sys +10; means increase by 10 steps at the time !
you can change them starting from 1 ,!! and dont change the + and - signs !!
need to comment things out so first see script and make adjustment to your needs !
i tested the script , but without the height sensor values ,,
i need to do some testing on this part !! butt if i am right it wil work !!
the system works , need to check out the height sensor responses
for connection part i use a cheap hk 6 chanel v2 system rx tx conecting aux and throttle from the rx to the arduino pin 5 and pin 6 , and from the arduino it wil go back to the kk2.0 board bypassing 2 signals for use !..
other pins are used for height and tx/rx for APC220 serial true air !
need to change the Serial. to MySerial . for virtual serial port on other pins then pin 0,and 1

]:slight_smile:
]:slight_smile:

// dexters KK2.0 automatic flight system //
// this wil be the automatic height control //
// stil busy on this script using a sonic sensor ,433 mhz wireless comunication device APC220, and kk2.0 controled quadcopter //
// going to use some serial outputs for debug later ,and virtual serial for wireles debuging //
// start building this script on 29-7-2014 //
// have to putt the sonic range finder HCSR04 part in here and do some more testing on this stuff //
// not using this pins 4 now //
// pin 2,3,5,6,7,8,9,10,11,12 are used //
// 2 = sensor trigger pin .3 = sensor echo pin .5 = pwm input AUX .6 = pwm input THROTTLE.7 = pwm output AUX.8 = pwm output THROTTLE.9 =Gimbal input horizontal pwm.//
// 10 =Gimbal vertical input pwm.11 =.virtual serial port rx. 12 = virtual serial port tx. //
// !! need to add piezo on pin 4 for some hearing the flight modes bleeps !! //
// need to test and modify values to working digits //

#include <SoftwareSerial.h> // virtual serial port lib
#include "HCSR04.h" // sonic sensor lib
#include <Servo.h>
HCSR04 ultrasonic(2, 3); //Trig pin2 , Echo pin3;
// set height here //
const int height =100; // height set to 1 meter =100 cm ,for trying to stay on 1 meter height //
const int auxset =1800;// aux output to kk2.0 for setting auto level onboard on or off !
int cm =90; //incoming value of sensor
int safety =1; // safety switch value//
int sensor; // using this for sensoring inputs later on
int auxv ; // Here's where we'll keep our aux channel values 1400 < autolevel off 1400 > = on
int auxs ; // Here i store input aux to servo output numbers
int throtv ; // Here's where we'll keep our throttle channel values
int throts ; // Here i store input throttle to servo output numbers
int system ; // system for do stuf with throttle and height //
// gimbal to horizontal and vertical pos readout //
// normal when start flying on the kk 2.0 the gimbal wil center its pos if used right //
int horim ; // using this for boxing the horizontal value from kk2.0
int vertm ; // using this for boxing the vertical value from kk2.0
int horiz ; // converting the horizontal input from kk2.0 to mapped value
int vertic ; // converting the vertical input from kk2.0 to mapped value
//some testing mem and sys shit for calculating up and down sequence //
int mem1;
int mem2;
int sys;
//
Servo throttle; // make servo output to kk2.0 throttle input
Servo aux; // make servo output to kk2.0 aux input
int signal =0; // signal procces
SoftwareSerial mySerial(11, 12); // add extra virtual serial rx pin 11 tx pin 12 //

void setup() {
Serial.begin(9600); // starting my virtual serial port for APC220 wireles tx rx baud rate 9600
Serial.println ("AUTOMATIC HEIGHT FLIGHT SYSTEM KK2.0 QUADCOPTER"); // sending this test string true the virtual serial for testing the apc cconection
Serial.println (" SYSTEM BOOTING PLEASE HOLD"); // sending this test string true the virtual serial for testing the apc cconection
delay (2000);
aux.attach(7); // servo conection out pin 7 to kk2.0 aux input //
throttle.attach(8); // servo conection out pin 8 to kk2.0 throttle input //
aux.writeMicroseconds(auxset); // set aux output to autolevel on on kk2.0 //
pinMode(5, INPUT); // Set input channel for aux on pin 5 from rx //
pinMode(6, INPUT); // Set input channel for throttle on pin 6 from rx //
pinMode(9, INPUT); // Set input channel from gimbal horizontal pin 9 from rx for reading level //
pinMode(10, INPUT); // Set input channel from gimbal vertical pin 10 from rx for reading level //
//Serial.begin(9600); // start serial stuff build later on //
system = 0;
throtv = pulseIn(6, HIGH, 20000); //checking throttle input
auxv = pulseIn(5, HIGH, 20000); //checking throttle input
if (throtv > 1025){ // if throttle is higher then 1000 then //
throttle.writeMicroseconds(1000); // throttle on 0 before start
delay (1000); // this wil check if the throttle is above 0 then it wil delay 1 sec and prompt warning on serial output //
Serial.println("WARNING THROTTLE LEVEL NOT 0"); // warning mesage serial when
safety =1; // safety wil be fase 1 = disable trhottle input , and output 0 throttle = disabled mode //
return;
}
else if (throtv < 1025)
{
safety =2; // if input throttle = near 0 then safety wil be fase 2 = working quadcopter going to safety fase 3 later = flight mode //
}
}
void loop() {
throtv = pulseIn(6, HIGH, 20000); //checking throttle input
auxv = pulseIn(5, HIGH, 20000); //checking throttle input
// vertical and horizontal readout from gimbal output kk2.0 //
// horim = pulseIn(9, HIGH, 20000); //checking gimbal horizontal input
// vertm = pulseIn(10, HIGH, 20000); //checking gimbal vertical input
// horiz = map(vertm, 0, 20000, 0, 180); // mapping the horizontal value
// vertic = map(horim, 0, 20000, 0, 180); // mapping the vertical value
// this is the sonic sensor part for now , using the values for 433 mhz serial data and horizontal and vertical uav station //
//cm = ultrasonic.Ranging(CM);
sensor = cm ;// converted duration from sensor to sensor cm value //
// output sequence for debug serial stuff //

Serial.print ("Height settings set at = ");
Serial.print (height);
Serial.println (" cm ");
Serial.print ("Throttle output = ");
Serial.println (throts);
Serial.print ("Throttle input = ");
Serial.println (throtv);
Serial.print ("Throttle system = ");
Serial.println (sys);
Serial.print ("Sensor data output values = ");
Serial.print (sensor);
Serial.println (" = cm heigth ");
Serial.print ("quadcopter fase = ");
Serial.println (safety);
Serial.print ("Horizontal data = ");
Serial.println (horiz);
Serial.print ("Vertical data = ");
Serial.println (vertic);
Serial.print ("AUX1 INPUT VALUE = ");
Serial.println (auxv);
Serial.println (" ");

if (safety ==1){ // checking startup safety fase ,if fase = 1 then throttle wil be 0 and wil loop till reset button //
// throttle.writeMicroseconds(1000); // throttle wil be 0 and
delay (3000);
Serial.println("WARNING THROTTLE LEVEL NOT SAFE"); // warning mesage serial when
return;
}
// checking startup safety fase ,if fase = 2 then throttle wil be 0 and wil loop till reset button //
//if (safety =2){ // going in to fase 3 //
aux.writeMicroseconds(auxset); // aux ON for autolevel kk2.0 conected to aux input //
throtv = pulseIn(6, HIGH, 20000); //checking throttle input
auxv = pulseIn(5, HIGH, 20000); // Read the pulse width of AUX input on pin 5 and store it in auxv int //
auxs = auxv; // mapping the input of aux //
throts = throtv ; // mapping the input of throttle //
if (auxv>1300)Serial.println("AUTO LEVEL ON ");
if (auxv<1300)Serial.println("AUTO LEVEL OFF");
if (auxv<1300){
system =throtv ; // telling the system the value of the throtle input
throttle.writeMicroseconds(throtv); // write system speed to servo //
// if aux is off the input throttle = equal to input //
sys =throtv;
}
mem1 =sys;
if(height<sensor){ // if sensor is higher then 1 meter //
system = sys -10;
throts = mem1;
sys = constrain(system,1000,2000);
throttle.writeMicroseconds(sys); // write system speed to servo //

}
else if(height>sensor){ // if sensor is lower then 1 meter //
// throtv = pulseIn(6, HIGH, 20000); //checking throttle input
system = sys +10;
throts = mem1; // take speed DOWN by 1 digit of the system //
sys = constrain(system,1000,2000);
throttle.writeMicroseconds(sys); // write system speed to servo //
}
else
if(height==sensor){
Serial.println("AUTO LEVEL STRAIGHT");
throttle.writeMicroseconds(system); // write system speed to servo when it good height it wil be constant , //
//have to check this out it has to work maybe woble trying to stay on 1 meter //
//later on i can ad some more stuff butt first need to ad the sonic range finder , testing the stuf , and checking the failures //
}
}

still testing ,, wil make video soon 8)

Dexterbot:
serial true air wil this be safe enough ?

This is the only part of your post that looks anything like a question, and I have no idea what you're trying to ask.

If you just want to showcase your accomplishments, you would be better off posting in the Exhibition / Gallery section - or perhaps on a drone forum.