1

I'm working in a project with a quadrotor and mavlink. I have successfully installed mavproxy in my Ubuntu PC and and ran it without problems from terminal. When I run mavproxy.py from the terminal and connected a quadrotor with support for mavlink (APM autopilot), mavproxy detects the quadrotor and everything is ok.

When you execute mavproxy.py the program in the terminal begin to send and receive several parameters. You can write in the terminal some parameter to access for any configuration. For example, the command help in the terminal:

$ mavlink.py
.
.data beging
.
STABILIZE>  "when the program finish the configuration, allowed to you for doing an input any parameter, for example help"

STABILIZE>help
show all helps.

I have a code to execute mavlink.py from C++

include <iostream>
include <stdio.h>

using namespace std;

int main() {
FILE *in;
char buff[512];

if(!(in = popen("mavlink.py", "r"))){
    return 1;
}

while(fgets(buff, sizeof(buff), in)!=NULL){
    cout << buff;
}
pclose(in);

return 0;
}

When I run this C++ program the terminal shows the same things that would appear if I were running mavproxy.py from the terminal, but I don´t know how can I send a command such as help in the C++ code.

If you read the program, the while statement allows me to capture the parameters generated from the program mavproxy.py and cout in the terminal, but mavlink.py never ends until you write something in the terminal exit or press CTRL + C so the while loop never ends.

I have been reading about the Popen function, but I haven't found the correct form to do this.

I know that I can use the mavlink.h library in my program and send parameters to the quadrotor, but don't want do this with mavlink.h.

tomix86
  • 1,336
  • 2
  • 18
  • 29
  • If you want to both read and write have a search for popen2. It isn't part of the standard library but there are implementations out there. – Jerry Jeremiah Apr 26 '14 at 01:16
  • i was trying to do something with pepen2 with c++; i can see it is also used in python, do you have an example in phyton? an example where you call another phyton scrip from your principal py program? thanks – user3574324 May 13 '14 at 03:50
  • @user3574324 did you manage to solve this? I am facing the same issue.. Please get back ASAP! Thanks.. – sleeping_dragon Jan 18 '15 at 21:39

1 Answers1

0

I am not sure I understand your question, but I think you want to send commands to mavlink.py as well as read its output.

If that is the case, you must change the open mode of popen() from "r" to "w" so you can write, then you can send commands to it like this:

FILE *fp;
char *command="HELP";

if(!(fp = popen("mavlink.py", "w"))){
    return 1;

fwrite(command, sizeof(char), strlen(command), fp);
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you for your answer. I have been reading about this function, popen, and all i could found is that this function is used for, read and write files, so, if i use the code you wrote, i think it will write inside the source code of mavlink.py. My purpose is, run mavlink.py from c++ code and when it is running, i want to be able to send commands , like "help". It is posible with your code? with this code i can send the command help when mavlink.py is running?. there is another option, it's change c++ to python, but i don't have experience in python – user3574324 May 08 '14 at 15:13