constrain ,, wil this be in the hole sketch or also in parts ,,


i ask because use constrain throttle ,, when switch other mode stil use constrain used in sketch ,

i post sketch , , mean in below part ,, 1 constrain
this one   sys = constrain(system,1000,1725); ,,, 
this 1 onley need in part in , seems run true hole sketch ,,
:smiley-roll:

code: [select]
// dexters kk2.0 automatic flight system //
// debug sketch 5-8-2014 //

#include <servo.h>
#include <newping.h>
#define trigger_pin  3  // arduino pin tied trigger pin on ultrasonic sensor.
#define echo_pin     2  // arduino pin tied echo pin on ultrasonic sensor.
#define max_distance 600 // max sensor value 400 means 4 meter
newping sonar(trigger_pin, echo_pin, max_distance); // newping setup of pins , maximum distance.
// set height here //
const int height =6000; // digital value height
const int auxset =1800; // aux output kk2.0 setting auto level onboard on or off !
const int decent =400; // decent delay time hard part depents on survace , height set //
// above values need tuned //
int cm ; //incoming value of sensor 0-100000
int sensor;// sensor cm values in centimeter
int safety =1; // safety switch value//
int auxv ; // here's we'll keep our aux channel values 1400 < autolevel off 1400 > = on
int auxs ; // here store input aux servo output numbers 
int throtv ; // here's we'll keep our throttle channel values
int throts ; // here store  input throttle servo output numbers
int system ; // system stuf throttle , height //
int mem1;
int sys;
int led = 13;
int piezo = 10;
// gimbal horizontal , vertical pos readout //
// normal when start flying on kk 2.0 gimbal wil center pos if used right //
int horim ; // using boxing horizontal value kk2.0
int horiz ;
servo throttle;  // make servo output kk2.0 throttle input
servo aux;       // make servo output kk2.0 aux input
int signal =0; // signal procces

void setup() {
  serial.begin(57600); // starting serial port apc220 wireles tx rx baud rate 57600 
  aux.attach(7);   // servo conection out pin 7 kk2.0 aux input //
  throttle.attach(8); // servo conection out pin 8 kk2.0 throttle input //
  aux.writemicroseconds(auxset);   // set aux output autolevel on on kk2.0 //
  pinmode(led, output);
  pinmode(piezo,output);
  pinmode(4, input); // new horiz input channel
  pinmode(5, input); // set input channel aux on pin 5 rx //
  pinmode(6, input); // set input channel throttle on pin 6 rx //
  pinmode(9, input); // set input channel gimbal horizontal pin 9 rx reading level //
  pinmode(10, input); // set input channel gimbal vertical pin 10 rx reading level //
  // indication light or piezo
  digitalwrite(led, low);   // turn led on
  digitalwrite(piezo, low);   // turn piezo on
  delay(1000);               // wait second
  digitalwrite(led, high);   // turn led on
  digitalwrite(piezo, high);   // turn piezo on
  delay(1000);               // wait second
  digitalwrite(led, low);
  // 
  system = 0;
  throtv = pulsein(6, high, 20000); //checking throttle input rx
  auxv = pulsein(5, high, 20000); //checking throttle input rx
  if (throtv > 1025){ // if throttle higher 1025 safety goes first //
    throttle.writemicroseconds(1000); // throttle on 0 before start
    delay (1000); 
    safety =1; // safety wil fase 1 = disable trhottle input , , output 0 throttle  = disabled mode //
    return;
  }
  else if (throtv < 1025)
  {
    safety =2; // if input throttle = near 0 safety wil fase 2 = working quadcopter going safety fase 3 later = flight mode //
  }
}
void loop() {
  sensor = cm/ us_roundtrip_cm;
  serial.flush();
  serial.print((char)1);
  serial.print (throts);
  serial.print((char)2);
  serial.print (throtv);
  serial.print((char)3);
  serial.print (height);
  serial.print((char)4);
  serial.print (auxv);
  serial.print((char)5);
  serial.print (auxset);
  serial.print((char)6);
  serial.print (cm);
  serial.print((char)7);
  serial.print (sensor);
  serial.print((char)8);
  serial.print (safety);
  serial.print((char)9);
  serial.print (horiz);
  serial.print((char)10);
  serial.print (0);
  serial.print((char)11);
  serial.print (decent);
  serial.flush();
  // vertm = pulsein(10, high, 20000); //checking gimbal vertical  input
  horim = pulsein(4, high, 20000); //checking gimbal horizontal input
  throtv = pulsein(6, high, 20000); //checking throttle input
  auxv = pulsein(5, high, 20000); //checking throttle input
  horiz =map(horim, 1600, 1400,100,0);
  cm = sonar.ping();
  if (safety ==1){ // checking startup safety fase ,if fase = 1 throttle wil 0 , wil loop till reset button //
    delay (3000);
    return;
  }
  aux.writemicroseconds(auxset);  // aux on autolevel kk2.0 conected aux input //
  throtv = pulsein(6, high, 20000); //checking throttle input pwm
  auxv = pulsein(5, high, 20000); // read pulse width of aux input on pin 5 , reads input pwm//
  if (auxv<1300){
    digitalwrite(led, high); 
    system =throtv ; // telling system value of throtle input
    throttle.writemicroseconds(throtv);  // write system speed servo // 
    // if aux off input throttle = equal input //
    sys =throtv;
  }
  mem1 =sys;
  if(height<cm){ // if sensor lower height value //
    system = sys -1;// take speed down 1 digit of system //
    throts = mem1;
    sys = constrain(system,1000,2000);
    throttle.writemicroseconds(sys);  // write system speed servo //
    delay (decent);
  }
  if(height>cm){ // if sensor lower height value //     
    system = sys +4  ;// take speed 4 digit of system //
    throts = mem1;
    sys = constrain(system,1000,1725);
    throttle.writemicroseconds(sys);  // write system speed servo //
  }
}



quote
this one   sys = constrain(system,1000,1725); ,,, 

return input value if between lower , upper limit. otherwise return lower limit if value below lower limit or return upper limit if value above upper limit.

you store value in different variable. problem? value of system not affected constrain() function.


Arduino Forum > Using Arduino > Programming Questions > constrain ,, wil this be in the hole sketch or also in parts ,,


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support