Beginner Series Lesson 9: Analog Read and Controller Flight
In this lesson, we’ll learn how to write our own controller flight code.

IMG_1371

The CoDrone controller uses two analog joysticks to control the throttle, yaw, pitch and roll of the drone.  Here, we’ll explore the differences between analog and digital inputs, and learn how to translate the analog joystick into properly scaled values.

There are 3 steps to developing our controller program:

  1. Read the joysticks
  2. Convert joystick values to maneuver input values (-100 to 100)
  3. Send the converted values to the CoDrone

AnalogRead()

If you recall, to read our IR sensors we used the command

This would read a digital signal from the specified pin number.  For our joysticks, we’ll use a very similar command:

analogRead()

analogRead(pin number)

pin number – the pin number of the sensor that you want to read.

Reads the sensor value with a range of 0 to 1023.

Analog vs Digital

The difference between analog and digital in Arduino is

  • Analog signals can range from 0-1023
  • Digital signals can only be 1 or 0 (true or false)

There’s a lot of math and computer science to explain this, but it’s a bit too deep for this lesson.  Just keep in mind that digitalRead()  is for things that only have two states (0 or 1), and analogRead()  is for things that you might need more detail with.

Digital Examples

1. Digital IR Sensors
Some IR sensors, like the ones we have on the bottom of our CoDrone controllers, are only used for digital sensing.  These sensors work as switches, letting us know if they are either covered or not covered.

2. Buttons
Buttons are digital as well.  They are either pressed or not pressed.

3. Touch Sensors
Touch sensors work just like buttons.  They are either touched or not touched.

Analog Examples

1. Joysticks
Joysticks are analog because you need to know more than just “is the joystick pressed or not pressed”.  You want to know how far it’s pressed.

2. Analog IR Sensors
There are also 3 analog IR Sensors on our CoDrone controllers.  Instead of just telling you whether or not they are covered, they also tell you how far away the thing covering it is based on how much IR light it is receiving.  We will learn to use these in later lessons.

3. Thermometers
Thermometers tell you the exact temperature.  It would be pretty useless if a thermometer only told you “hot” or “not hot”.

 

Using AnalogRead()

To read our joysticks, we use analogRead() .  If your controller is wired following the wiring guide, then your joysticks are connected to pins 22-25 (Pin-Out Guide).

Joysticks Guide

Our code to read all of our joystick values would look like this

Each of your joysticks has two sets of wires coming out of it, making four inputs.  One set reads the left and right movement of your joystick, the other set reads the up and down movement.

To understand what each of our joysticks is seeing, we’ll look at our throttle joystick (connected to pin 23),

 

Since analog values range from 0 to 1023, pushing our throttle joystick all the way up will save the variable throttle  as 1023.  Pushing it all the way down would be 0.  It makes sense, then, that leaving it in the middle would be around 512 (halfway between 0 and 1023).

 

Scale Change

Now we have integer values saved for

that range from 0 to 1023, depending on where the joystick is.

We also know that our acceptable values for THROTTLE , YAW , PITCH , and ROLL  have to be from -100 to 100.  If we want to control our CoDrone with our controllers, we need to translate the analogRead()  values from 0 to 1023 to values from -100 to 100.

To do this, we perform what is called a map, or a scale change.  We remap the values from one scale to another.

Remap

In Arduino, we can use the function CoDrone.AnalogScaleChange()

CoDrone.AnalogScaleChange()

CoDrone.AnalogScaleChange(input)

input – The number that you want to be remapped from the 0 – 1023 scale to the -100 – 100 scale

Returns a value between -100 to 100.

We can now combine what we’ve learned and write the line

int yaw = CoDrone.AnalogScaleChange(analogRead(22));

This reads the analog sensor value of pin 22 and converts that reading to a scale of -100 to 100.   Since the scale is correct, instead of using int yaw  we can now just set YAW  directly

YAW = CoDrone.AnalogScaleChange(analogRead(22));

Correcting for Direction

You may or may not have noticed that the left and right motion of the joystick goes from 1023 to 0, instead of 0 to 1023

Joysticks Analog 3

If this was scale changed, it would look like this

Joysticks Analog 4

For our controller, we want the left side to be negative (so the CoDrone turns left) and the right side to be positive (so the CoDrone turns right).  To correct for this, all we have to do is multiply the line by -1 .

Our final code should look like this

The if-statement if (PAIRING == true){ only executes when the CoDrone is paired.  This is to prevent sending commands when the CoDrone disconnects.  This line is not necessary, but recommended.

Activity: In Control

Create and upload your own controller code!

  • Include a kill-switch
  • Include an extra maneuver switch
    • Ex:  If I cover the left sensor, my CoDrone will yaw to the right for 2 seconds, then yaw back to the left for 2 seconds.