Hovering

i've been assigned to program my quad-copter to hover at a certain height when the throttler is max. However i am uncertain on how to programme the code. Can anyone help me with the codes best with explanation :smiley: Much help is appreciated.

Regards, Xsindraz

If you're uncertain how to program it, does that mean you do however know the algorithms required, ie do you know what the sensors and actuators actually need to do to hover?

http://robots.dacloughb.com/project-2/testing-hover-capability/

just try this

@jimboZA Nope, i am new to this field. My current assignment differ from what i am studying.

@AMPS-N Thanks. However, from my understanding my project require the use ultrasonic sensor arduino to be programmed.

xsindraz:
@jimboZA Nope, i am new to this field. My current assignment differ from what i am studying.

Then you need to rephrase your question: you need to ask for advice on the algorithms and the mechanics of flight and understand that all fully before you go anywhere near code.

I'd suggest that you:

  • Research the field outside of this forum.
  • In parallel with that you learn the Arduino, if you didn't already do that, perhaps starting here.
  • Then put your new quadcopter and Arduino knowhow together by trying to code the sketch you need to hover
  • And then if you get stuck, ask for specific help on the issues you encounter.

Alternatively, if it's a "turnkey" solution you need, you ought to post here.

Can anyone explain what these codes means to me?

#include <SoftwareSerial.h>

#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
LSM303::vector<int16_t> running_min = {
-1383, -2061, -3033}
, running_max = {
2229, 1974, 1206};

#define triggerZ 8
#define echoZ 9
#define ultrasonic_power 7

SoftwareSerial mySerial(2, 3); // RX, TX
int cx, cy;
unsigned long prevTime= millis();
unsigned long currTime;
unsigned long elapTime;

void setup() {
Serial.begin(9600);
mySerial.begin(9600);

pinMode(triggerZ, OUTPUT);
pinMode(echoZ, INPUT);
pinMode(ultrasonic_power, OUTPUT);
digitalWrite(ultrasonic_power, HIGH);
Wire.begin();
compass.init();
compass.enableDefault();

}
void loop() {
// if(calc_x() < -50 || calc_x() > 50){
// Serial.println(calc_x());
// }
//
// if(calc_y() < -50 || calc_y() > 50){
// Serial.println(calc_y());
// }
//

// currTime = millis();
// elapTime = currTime - elapTime;
// if(elapTime > 100){
// mySerial.print(calc_x());
// mySerial.print('\n');
// mySerial.print(calc_y());
// mySerial.print('\n');
// prevTime = millis();
// }
Serial.print(calc_x());
Serial.print('\t');
Serial.println(calc_y());
while(mySerial.available()){
if(mySerial.read() == 49){
mySerial.print(calc_x());
mySerial.print('\n');
mySerial.print(calc_y());
mySerial.print('\n');
}
}

// mySerial.print(calc_x());
// mySerial.print("\t");
// mySerial.print(calc_y());
// mySerial.print("\t");
// mySerial.println(calc_z());
//

}

int calc_x(){
compass.read();
int x;
x = compass.a.y;
// 0 - 1000, 0 -100 //
return(map(x, -16500, 16500, -250, 250));
}

int calc_y(){
compass.read();
int y;
y = compass.a.x;
return(map(y, -16500, 16500, -250, 250));
}

int calc_z() {
long duration, distance;
digitalWrite(triggerZ, LOW);
delayMicroseconds(2);
digitalWrite(triggerZ, HIGH);
delayMicroseconds(10);
digitalWrite(triggerZ, LOW);
duration = pulseIn(echoZ, HIGH, 12000);
distance = (duration / 2) / 29.1;
if (duration >11640){
distance = 200;
}
return (distance);

}

void send_data(){
mySerial.print(cx);
mySerial.print(cy);
}

void print_raw(){
Serial.print(calc_x());
Serial.print('\t');
Serial.print(cx);
Serial.print('\t');
Serial.print(calc_y());
Serial.print('\t');
Serial.println(cy);
}

xsindraz:
Can anyone explain what these codes means to me?

I have no idea what they mean to you. Mostly the sketch sends the Y and X magnetometer readings to Serial over and over and to SoftwareSerial when prompted with character code 49 ('1'). The code for the ultrasonic rangefinder (calc_z()) seems to never be used. Likewise for send_data() and print_raw().

#include <SoftwareSerial.h>
#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
LSM303::vector<int16_t> running_min = {
  -1383, -2061, -3033
}
, running_max = {
  2229, 1974, 1206
};

#define triggerZ 8
#define echoZ 9
#define ultrasonic_power 7

SoftwareSerial mySerial(2, 3); // RX, TX
int cx, cy;
unsigned long prevTime = millis();
unsigned long currTime;
unsigned long elapTime;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);

  pinMode(triggerZ, OUTPUT);
  pinMode(echoZ, INPUT);
  pinMode(ultrasonic_power, OUTPUT);
  digitalWrite(ultrasonic_power, HIGH);
  
  Wire.begin();
  compass.init();
  compass.enableDefault();
}

void loop() {
  Serial.print(calc_x());
  Serial.print('\t');
  Serial.println(calc_y());
  while (mySerial.available()) {
    if (mySerial.read() == 49) {
      mySerial.print(calc_x());
      mySerial.print('\n');
      mySerial.print(calc_y());
      mySerial.print('\n');
    }
  }
}

int calc_x() {
  compass.read();
  int x;
  x = compass.a.y;
  // 0 - 1000, 0 -100 //
  return (map(x, -16500, 16500, -250, 250));
}

int calc_y() {
  compass.read();
  int y;
  y = compass.a.x;
  return (map(y, -16500, 16500, -250, 250));
}

int calc_z() {
  long duration, distance;
  digitalWrite(triggerZ, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerZ, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerZ, LOW);
  duration = pulseIn(echoZ, HIGH, 12000);
  distance = (duration / 2) / 29.1;
  if (duration > 11640) {
    distance = 200;
  }
  return (distance);
}

void send_data() {
  mySerial.print(cx);
  mySerial.print(cy);
}

void print_raw() {
  Serial.print(calc_x());
  Serial.print('\t');
  Serial.print(cx);
  Serial.print('\t');
  Serial.print(calc_y());
  Serial.print('\t');
  Serial.println(cy);
}

I can tell you what that code means to you - nothing.

What it means to me is that you can't even be bothers to read the how to use this forum sticky post.