LibrePilot Forum

Users => Applications - Autonomous Flight => Topic started by: M3troiX on October 04, 2018, 08:12:52 pm

Title: Controlling CC3D with Arduino
Post by: M3troiX on October 04, 2018, 08:12:52 pm
Hello Guys!

I've got some problems controlling the CC3D with an Arduino Mega 2560
Im using Visual Studio Code with PlatformIO with the library LibrePilotSerial from https://github.com/MarcProe/LibrePilot.arduino
I changed some code in this lib. that it is abled to use HardwareSerial, because of any reason it does not work with SoftwareSerial in my case.

I am using UAVTalk via the Main Port of the CC3D. I am able to receive a MANUALCONTROLCOMMAND_OBJID and I can also configure the Throttle. When I request the same Object again, I am able to see that the value of the float "Throttle" changed. But in the System tab of the GCS Software nothing has changed and the drone is doing nothing.

My configuration:
The Main Port of the CC3D is set to telementry and the Input-types are set to DSM(MainPort), it also works if I set it to GCS. The Rx and Tx Pins are connected to the Mega2560(Serial2) pins. I am using a HC-05 bluetooth-module on the Mega2560(Serial3) pins to send an Integer for my switch-case in the code.

My question:
What do i have to change/do, to give the throttle a value, that the drone lifts up?
 

Code: [Select]
#include <Arduino.h>
#include <LibrePilotSerial.h>
#include <manualcontrolsettings.h>
#include <manualcontrolcommand.h>

LibrePilotSerial lps(&Serial2);
char state = '0';

void setup() {

  lps.serial->begin(9600);

  Serial3.begin(9600);

  Serial.begin(9600);
 
}
 
void loop() {
   
    lps.request(MANUALCONTROLCOMMAND_OBJID);

    if(lps.receive(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr)) {
   
        while(state == '0')
        {
            if(Serial3.available()) {
                state = (char)Serial3.read();
                Serial.println(state);
            }
        }

        switch(state) {
            case '1':
                //Serial.println("State wurde auf 1 gesetzt");
                ManualControlCommandDataUnion.data.Throttle = 0.7;
                lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);
                state = '0';
                //Serial.println("State wurde auf 0 zurückgesetzt, warte auf nächste abfrage");
                break;
            case '2':
                //Serial.println("State wurde auf 2 gesetzt");
                ManualControlCommandDataUnion.data.Throttle = -0.2;
                lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);
                //Serial.println("State wurde auf 0 zurückgesetzt, warte auf nächste abfrage");
                state = '0';
                break;
        }
    }
}
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 04, 2018, 10:07:18 pm
Hi,

Prior to increase Throttle you may need to arm the board or the motors cannot start.

This can be achieved using the AccessoryDesired uavo and a Accessory0 configured as arming switch.
You can also do a arming sequence using Yaw left or something similar. (Both options need a negative value of Throttle)
Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 05, 2018, 08:33:36 pm
Hi,

thank you for your help. I wrote a simple sequenz to try if I can arm it with Yaw-left. Unfortunately it did not solve the problem. The drone still does not react on my commands.

Enclosed you can find some more screenshots an the code.

Code: [Select]
    char sequence = '0'
    while(sequence != '3')
    {
        if(sequence == '0')
        {
        lps.request(MANUALCONTROLCOMMAND_OBJID);
        if(lps.receive(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200))
            {
                Serial.println("Throttle auf negativ");
                ManualControlCommandDataUnion.data.Throttle = -1.0;
                lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);
                sequence = '1';
            }
        }
        lps.request(MANUALCONTROLCOMMAND_OBJID);
        if(sequence == '1')
        {
            if(lps.receive(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200))
            {
                Serial.println("Yaw negativ");
                ManualControlCommandDataUnion.data.Yaw = -1;
                lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);
                sequence = '2';
            }
        }
       
        lps.request(MANUALCONTROLCOMMAND_OBJID);
        if(sequence == '2'){
            delay(5000);
            if(lps.receive(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200))
                {
                    Serial.println("Yaw neutral, Throttle neutral");
                    ManualControlCommandDataUnion.data.Yaw = 0.0;
                    ManualControlCommandDataUnion.data.Throttle = 0.0;
                    lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);
                    sequence = '3';
                }
            }
    }
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 05, 2018, 11:41:12 pm
Red alarm (Critical) with INPUT will prevent arming.
Try setting some valid settings in Input tab (like PPM+some channel for all controls + flightmode) and get at least INPUT Orange alarm.

Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 05, 2018, 11:46:24 pm
You mean like this?
That also does not help  :(
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 05, 2018, 11:49:49 pm
Did you set PPM input in Hardware tab and reboot ?
INPUT alarm still Red or is Orange ?
Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 05, 2018, 11:52:09 pm
It is still Red. I am using the main port, there is no option for ppm
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 05, 2018, 11:57:14 pm
Set PPM_Pin8+OneShot for Receiver port or PPM for FlexiPort in Hardware tab and Reboot.
Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 06, 2018, 12:01:03 am
If I try to use the Flexi-Port with this configuration, the light change to orange. The new problem is, if I use this configuration, the UAVTalk does not work
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 06, 2018, 12:25:09 am
I think you may need some code changes in armhandler.c and change the okToArm() behaviour to allow arming with Input alarm.
What GCS version do you use ?
Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 06, 2018, 12:32:16 am
Now i moved a step forward ^^
I set the flexi-port to ppm and also the inputs, as you said. The lamp is orange.
I can still use UAVTalk via the main-port.

But the drone still does not arm ^^.

I recognized that in the System-tab I get some values now.

Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 06, 2018, 02:44:08 am
I think the UAVTalk objects that I send to the cc3d from my arduino gets ignored.

For example:

If I send an object like bellow and I receive another ManualControlCommand_OBJ, the value changed to 1.

ManualControlCommandDataUnion.data.Throttle = 1;
lps.send(MANUALCONTROLCOMMAND_OBJID, ManualControlCommandDataUnion.arr,200);

But in the GCS System-tab the value is still -1, nothing changed.

Since the alarm lamp changed to orange I am able to arm the drone if I use always armed.
But if I do an arming sequenz in my code, nothing happens.

Do you know what causes the problem?
Title: Re: Controlling CC3D with Arduino
Post by: TheOtherCliff on October 06, 2018, 05:20:58 am
I have not coded things like this before, but I imagine that whatever color the input is, that you have two sources of input that are fighting.  Your Arduino commands may be received, but right after that the normal input overwrites the ManualCommand.

You should read the input code.  I recall that there is a ManualCommandReadOnly() that is used to tell it to avoid reading input and setting ManualCommand:
flight/modules/Receiver/receiver.c:        if (ManualControlCommandReadOnly()) {
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 06, 2018, 11:27:49 am
ManualCommandReadOnly() do not set something, this is a check function returning the uavo status

You will need something similar to the python example script here (https://bitbucket.org/librepilot/librepilot/src/aba11f0e10b4d063fdfc30d16124dcfc75793309/python/examples/example.py?at=next&fileviewer=file-view-default#example.py-164) to take control of ManualControlCommand.

Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 06, 2018, 11:43:32 am
Quote
I have not coded things like this before, but I imagine that whatever color the input is, that you have two sources of input that are fighting.  Your Arduino commands may be received, but right after that the normal input overwrites the ManualCommand.

That could be a possibillity, but why do I receive a changed value when I send and receive the package again, if the other source overrides it?

Quote
You will need something similar to the python example script here to take control of ManualControlCommand.

Is there no other way?
Title: Re: Controlling CC3D with Arduino
Post by: TheOtherCliff on October 06, 2018, 05:16:38 pm
ManualCommandReadOnly() do not set something, this is a check function returning the uavo status

Yes.  My point was that he needs to look at this code.
Title: Re: Controlling CC3D with Arduino
Post by: TheOtherCliff on October 06, 2018, 05:31:47 pm
That could be a possibillity, but why do I receive a changed value when I send and receive the package again, if the other source overrides it?

I am making a guess, but normal input runs maybe 50 times a second.  You set, then 0.1ms later you get, and the value is what you set.  10ms later the normal input runs and overwrites your set.  You could wait 50ms (really only 20ms is probably needed) or so between set and get to see if it has changed back.

Quote
Is there no other way?
You don't have to use the LP framework, but you do need to do the things that it does.  In particular, you need to set that "readonly" property to turn off the normal input processing.  I suggest you look at the C code referred to look at what is exectuted when the readonly property is set.  :)
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 06, 2018, 06:26:29 pm
Quote
I suggest you look at the C code referred to look at what is exectuted when the readonly property is set.  :)
Not sure you are able to play with metadata from arduino (https://github.com/MarcProe/LibrePilot.arduino/blob/master/src/LibrePilotSerial/LibrePilotSerial/LibrePilotSerial.cpp), without additional code. :)

Quote
Is there no other way?
As a test, you can flash the firmware attached (16.09) without receiver module.
You should also consider using Next with matching arduino templates if you plan to use others UAVOs.
Arduino target was added after the 16.09 release, not sure all the UAVO Object IDs defined in firmware are the same as the ones used in arduino templates you are using.
Title: Re: Controlling CC3D with Arduino
Post by: M3troiX on October 08, 2018, 03:04:28 pm
Quote
You should also consider using Next with matching arduino templates if you plan to use others UAVOs.

I've tried to build the GCS from the "next" branch, but I there is an error with the coptercontrol.elf. Enclosed you can find a screenshot of the error.
Title: Re: Controlling CC3D with Arduino
Post by: f5soh on October 08, 2018, 05:45:04 pm
Seems a missing python2 package