MAVLink and Arduino: step by step

Dear Sir,

I am working on a project that is building an autonomous full-scale sailboat using Pixhawk. In my project, I suppose to use 2 stepper motor to control the sail and the rudder, but I don’t know how to create the communication between the Pixhawk and the Arduino as well as from Arduino and the stepper motor. So, I wonder if the code you provided will help me to solve the problem of communicating between the Pixhawk and Arduino for the boat?
Thank you so much, Sir.
Duy.

Dear @duynp89:

I do not fully understand your question. “Communicate” is a broad term. MAVLink is a simple way to send a receive certain data (streams). Depending on your needs, the answer to your question can be yes or no. Please explain a bit more, give details on your cabling and layout and maybe we could help.

Regarding the control of the stepper motors, here the answer is yes, you can use Arduino to control them, but you need a driver to be able to provide enough power to the motors. Again, tell us about the model of the motors, the nominal power to provide. For small steppers you can use the well known L298N (h-brigde), but you mention a “full-scale” boat and probably these will be too small.

Again, provide us with more detail and we will very happy to try to help!

Kind regards,
JP

Dear @jplopezll ,

In my project, I will use 2 stepper motors STP-MTRH-34127, NEMA 34 to control the movement of the sail and the rudder. And I read somewhere that said the Pixhawk could not control the stepper motor directly, it has to be connected via some sources of microcontroller like Arduino. I don’t know if I understand it correctly. Also, I don’t know if I need to use anything else besides 2 stepper motors, such as stepper drive or gearbox.

Here is a short description of my project’s goal. The boat can only travel within the wind, and the Pixhawk has to determine the angle for the sail and the rudder so that the boat can obtain the maximum speed. The Pixhawk will get the information about the wind direction via an anemometer to figure out the possible angles for rudder and sail.
So, I have a difficult time to figure out how to connect the Pixhawk and Arduino, so that after calculating the angles, the Pixhawk will send that information to the Arduino and Arduino will control the stepper motor to rotate to the determine angles.

I am new to the Pixhawk, so I am not too sure if I use the right word to describe it. Please excuse me for that.

Thank you so much, Sir.

Duy

You’ve got quite a lot of reading and learning to do: I strongly suggest building a scale version before tackling a full size sailboat.
You don’t need mavlink to control the stepper motors. I’d suggest googling “pwm control of bipolar stepper motors” as a start point.

Dear @james_pattisone
Thank you so much for your advice. I wish I can build a small scale boat too. But unfortunately, this is the senior design project for mechanical engineering, and the previous year’s team already did the small scale version of the boat, and now our team’s mission is to upscale it. None of our team members have experience with the Pixhawk or coding, so this is a whole new world to us.
We are trying our best to understand how the pixhawk work as well as how to send the signal to the arduino so that it can control the stepper motor.
Once again, thank you so much, Sir.
Duy.

Hi, @duynp89:

As James suggests, you have a lot of reading to do…

Firstly, have a look at the specsheets of the mottors of your choice. I have found some docs in the download tab of this page. Pay attention to (1) the driver you will need to power and control the motors and (2) the braking resistors to avoid damage of the driver when there will be a force back.

What James suggests could be possible: using the pwm signal from the Pixhawk to control the steppers. For this, you will need some circuit to transform the pwm to pulses. It depends a lot on the driver you chose for the motors.

By the way, you can also go with Arduino to create the pulses needed by the driver of the motors. It is all a matter of design choices. As you mention there was a previous scale boat, what were the choices taken there?

If your questions are not precise, we cannot give you precise answers…

KR,
JP

Hi, @NiltonMadede:

Short answer: yes, there is.

Request the appropriate stream from the Arduino and then use the adequate library to display the received data on the display.

Hi @jplopezll, thank you very much for your information on MAVLink and Arduino. From my understanding, there is only for Pixhawk sending data to Arduino? Is it possible Arduino send sensor data to Pixhawk and display in Mission Planner?
Please help me. Thank you very much.

Hi, @Jamieyee16:

I have never tried it, but YES, you should be able to send messages from Arduino to your Ground Station. If you read again the post (in particular the last paragraphs about “Routing”) you will see that each message has a destination node address. Just use 255 as destination address for the Ground Station (Mission Planner in your setup).

In the code you can find that info were it says:

int sysid = 1;                   ///< ID 20 for this airplane. 1 PX, 255 ground station
int compid = 158;                ///< The component sending the message

If you have any futher doubts, just revert.
KR

Don’t forget that you have to send heartbeat as well to activate mavlink routing… otherwise packets will not passed to GCS.

ok, I will try on it.

Have u try to send a customise message? I understand MAVLink has its own unique packet format. Is it the same for the external sensor ( temperature sensor DHT22 ) while sending data to Pixhawk? If YES, what should I define for BYTE 3 ( System ID ), BYTE 4 ( Component ID ) and BYTE 5 ( Message ID )? Thank you for ur help! =)

Hi LeiroJuan,
Can you share your code with me? Currently I’m doing a project similar with you.

Hope for hearing from you soon. Thank you!

Hey, I’m pretty new to programming but need this for a school project. From your last section of code i should put that in a file called mav_request_data.h? Where do i put the earlier portions? Been having difficulty because of my weak background in programming.

Dear @LeesonL:

I do not want to look rude, but I think that, before going through all the code above, you should get a bit more familiarised with Arduino, the IDE and some C programming. Nothing will make sense without some previous background.

May I recommend you some reading?

You can later on ask at any time…

Cheers.

@jplopezll Hi Juan!

I am doing the same project, only the card (MKR arduino wifi 1010) and the LED matrix (MKR rgb arduino shield). I am a beginner with Arduino and need a completed file / code which I could just step is to add text and effects to show what the matrix. for example when pixhawk gets a 3d fixit gps then the matrix shows the text “gps ready”. Then it would be nice if the matrix could show different sensor data, for example the direction of flight to the south and the matrix shows “Mag 180” etc, it would also be nice if these animation and directly showing sensor values could be changed via rc channel. I tried to do as instructed but Arduino ide reports numerous errors in the code. i need your help here, do you want to help me?

Hi, @Fly_Ropo:

Sure I will be glad to assist. PM me to see if I can help you. I am not familiar with that board, please send me details on you wiring, layout, etc.

Before going to MAVLink, I assume that you have tested the board and the shield, and that you understand how they work and how to use the libraries.

KR.

@jplopezll thanks for your help! i understand how arduino ide software and how to connect pixhawkin to arduino, but
i just can’t write arduino code to make it work properly. It would be nice to just add to the finished code what mavlink value it listens to and what it writes to the matrix screen and in what color. For more information, see the links below. they have connection and information about the card and the led matrix display. I connect the pixhawkin card to the rx and tx pins as it should.

https://www.arduino.cc/en/guide/MKRRGBShield
https://store.arduino.cc/arduino-mkr-wifi-1010

Did finally this project? Can you help me?

can Anybody connect in ESP 32 + Lora like a Telemetry?

There is already a firmware for the ESP8266 to be used as a telemetry radio.
https://ardupilot.org/plane/docs/common-esp8266-telemetry.html