Pages: [1]   Go Down
Author Topic: Coding Assistance (Arduino ) for a Dual Rotor Copter  (Read 40 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

So I'm currently involved with building and testing a viable Dual Rotor Copter. I'm handling the collision avoidance of the UAV. Using an Arduino Uno board and an Ultrasonic Rangefinder from Maxbotix, I've sourced some basic code to obtain readings from the sensor whilst it is connected to the UAV. Please see below. I'm going to take the readings and set a distance for the UAV to approach, whilts telling the motors to decrease speed until the UAV is a cetain distance from the object and it is only hovering in place.

if(pulse  >= 0){
  if(distance <= 10) && (distance >=10)){
     motor command line goes here;
  }
  else if ((distance <= ##) && (distance >= ##)){
      motor action command goes here;
 }
  else{
      Final motor command;
  }
}

I'm struggling to write the motor command to ficilitate this. Can anyone please advise me on this?

In addition to this I then need to link this code to a serial communication code that has been written. Please see below:

Function: Wireless Communication Between the Gesture control software and cruis board for Drone control
Code: Code will be done using Keyboard input for all intensive purposes*/
#include <Servo.h>

int GTE_Data =0;
//int PWM_P =6; //Channel 2 Pitch - this will assign the pin 2 to be the channel which will control pitch
Servo PWM_T;//this sorts out the mapping problem thus it is safe to say that the embedded timer in the arduino did not correlate in the-- thus causing the PWM signals to be mapped incorrectly
Servo PWM_R;// The time interval for the PWM signal was set correct by hacking the servo library and adapting it to the current code sturcture.
Servo PWM_Y;
Servo PWM_P; // This function was used in an attempt to map function to transmit in accordance to the 1000us to 2000us Brand so that the GUI can make sense of it.
//reference to this was done using the servo library which contains these timers in the servo.h library.....so I expect this to work.
/*int PWM_T =3; //Channel 3 Throttle - this will assign pin 3 to be the channel which will control throttle
int PWM_Y =4; //Channel 4 Yaw - this will assign pin 4 to be the channel which will control yaw
int PWM_R =5; //Channel 1 Roll - this will asswign pin 5 to be the channel which will control roll*/
int L_Count=0; // Lift off counter ?? how is this the lift off counter and the incomingByte pin (pin 0)??
int T_Count=192; //ch3 - what these variable are just monitoring variables - or intermidate variables
int Y_Count=192; //ch4
int R_Count=192; //ch1
int P_Count=192; //ch2



void setup () {
Serial.begin(9600);// setting the baudrate of the serial communication
/*pinMode(PWM_T, OUTPUT); // sets the digital pin as output //this will assign the definition pin allocation as output pins.
pinMode(PWM_Y, OUTPUT); // sets the digital pin as output
pinMode(PWM_R, OUTPUT); // sets the digital pin as output
//pinMode(PWM_P, OUTPUT); // sets the digital pin as output*/
PWM_P.attach(6);
PWM_T.attach(3);
PWM_R.attach(5);
PWM_Y.attach(4);

//pinMode(7, INPUT);
}
void loop(){
if (Serial.available() > 0){ //Read the serial data incoming on the rX pin
GTE_Data = Serial.read();}
delay(100);
switch(GTE_Data) {// assign values dependinding on the key board button pressed >>> This might be the code which will need to be altered <<<
//Speed steps introduced to lower or increase the speed gradually >> this is done to insure as smooth rise and decline in control of the drone not sharp movement
//Pitch
case 97 : //"a" pressed >> decrease Pitch
P_Count=P_Count-5; // this function decrease pitch hence the use of -10 decrement
if (P_Count<=0){//limit the pitch time to positive values as this is the only information which can be interpreted
P_Count=0;}// this also sets the minimum value of the pitch to be zero......
 Serial.println(P_Count);// Use this to monitor is code is working
break;
case 100 : //"d" pressed>> max Pitch
P_Count=P_Count+5; // this function is to increase pitch hence the use of +10 increments
if (P_Count>=141){//limit the pitch time to positive values less than 255
P_Count=141;} // so max value 255 and min value is 0
Serial.println(P_Count);// Use this to monitor is code is working
break;
//Roll
case 122 : //"z" pressed >> decrease Roll // the control suggest that the key need to be pressed multiple times??? this could be a wrong asmt.
R_Count=R_Count-10;
if (R_Count<=0){//limit the Roll time to positive values 0
R_Count=0;}
Serial.println(R_Count);// Use this to monitor is code is working
break;
case 119 : //"w" pressed>> max Roll
R_Count=R_Count+10;
if (R_Count>=255){//limit the pitch time to positive values less than 255
R_Count=255;}
Serial.println(R_Count);// Use this to monitor is code is working
break;
//Throttle
case 109 : //"m" pressed >> decrease Throttle
T_Count=T_Count-10;
if (T_Count<=0){//limit the Roll time to positive values
T_Count=0;}
Serial.println(T_Count);// Use this to monitor is code is working
//RollTime = map(RollTime, 1000, 2000, 0, 255);
break;
case 105 : //"i" pressed>> max Throttle
T_Count=T_Count+10;
if (T_Count>=255){//limit the pitch time to positive values less than 255
T_Count=255;}
Serial.println(T_Count);// Use this to monitor is code is working
break;
//Yaw
case 106 : //"j" pressed >> decrease Yaw
Y_Count=Y_Count-10;
if (Y_Count<=0){//limit the pitch time to positive values
Y_Count=0;}
Serial.println(Y_Count);// Use this to monitor is code is working
break;
case 108 : //"l" pressed>> max Yaw
Y_Count=Y_Count+10;
if (Y_Count>=255){//limit the pitch time to positive values less than 255
Y_Count=255;}
Serial.println(Y_Count);// Use this to monitor is code is working
break;
//Clear or Reset all >> mid value
case 99 : //Pressed "c" // this could also be interpretted as a state of hover???******************Need to edit this to work properly
R_Count=128; //ch1
P_Count=128; //ch2
T_Count=T_Count; //ch3//I must Reference the throttle from the other case this is done by the T_Count at this Point in time-----This Has been Completed
Y_Count=128; //ch4
 Serial.println(T_Count);
break;
//Arm The Motors
case 49: //press "1" >>character one to arm the motors
//This is done by pushing the throttle/yaw stick to the minimum throttle and max yaw
//Where both values are maximum
T_Count=44; //ch3
Y_Count=141; //ch4
break;
//Disarm The Motors
case 48: //press "0" >>character zero to disarm the motors
//This is done by pushing the throttle/yaw stick to the bottom left corner
//Where both values are minimum
T_Count=44; //ch3
Y_Count=44; //ch4
break;
case 57:// press "9" >>automatic lift off //
for (L_Count=44;L_Count<=141;L_Count++)//?????****Need to edit this to work prooperly
T_Count=T_Count+5;
Serial.println(T_Count);
//analogWrite(PWM_T, T_Count);
PWM_T.write(T_Count);
delay(230); //delay for 230 milliseconds to have the UAV in air in about 5 seconds[5290ms]
// I cant have this delay function here it can potentially cut of serial comms and cause damage to the UAV
break;
//Need to make a landing Case.
/*default:
R_Count=128; //ch1
P_Count=128; //ch2
T_Count=T_Count; //ch3//I must Reference the throttle from the other case this is done by the T_Count at this Point in time-----This Has been Completed
Y_Count=128; //ch4
 Serial.println(T_Count);*/
}
/*analogWrite(PWM_T, T_Count);
analogWrite(PWM_Y, Y_Count);
analogWrite(PWM_R, R_Count);*/
PWM_T.write(T_Count);
PWM_P.write(P_Count);
PWM_Y.write(Y_Count);
PWM_R.write(R_Count);
//End Point of the 140 PWM correlates to a level read out of 1989, Mid Point - 92 correlates to logic read out of 1494 and Finally Low Point - 44 correlates to a logic read out of 1010
//analogWrite(PWM_P, P_Count);
}

Please help me guys!

Thank you



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26296
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: