Hi
I am using the Arduino Ethernet Board with the Relay Shield V2 to control the servo payload release.
The servo open and close will be controlled by a software. So when I press the "Open" button, it should open and release the payload and when I press "Close", the servo shaft will close.
I have received some codes but want to put in the codes for the servo movement too
In addition, How do I make the connection from the servo to the Relay Shield
I have attached the code...As of now, the led on the relay will turn on and off. How do I make it control the servo.
#include "Define.h"
#include "Mavlink.h"
void loop() {
/*---------------------------------------------------------Read UDP MSG---------------------------------------------------------*/
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
int packetSize = Udp.parsePacket(); // if there's data available, read a packet
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
mavlink_status_t mavlink_status{};
mavlink_message_t mavlink_msg{};
for (int i = 0; i < packetSize; i++) {
if (mavlink_decode(packetBuffer[i], &mavlink_msg, &mavlink_status)) {
handle_message(&mavlink_msg);
}
}
delay(10);
loopcount++;
if (loopcount % 100 == 0) {
char ReplyBuffer[UDP_TX_PACKET_MAX_SIZE]; // a string to send back
int RAWReading = analogRead(0);
float ADCReading = RAWReading / 1024.000 * 5.000;
dtostrf (ADCReading, 5, 2, ReplyBuffer);
// send a reply to the IP address and port that sent us the packet we received
// Udp.beginPacket(Remote_ip, Remote_port);
// Udp.write(ReplyBuffer);
// Udp.endPacket();
}
if (loopcount % 10 == 0)
send_heartbeat();
}
void handle_message(mavlink_message_t *msg)
{
switch (msg->msgid) {
case MAVLINK_MSG_ID_COMMAND_LONG:
handle_message_command_long(msg);
break;
default:
break;
}
}
void handle_message_command_long(mavlink_message_t *msg)
{
/* command */
mavlink_command_long_t cmd_mavlink;
mavlink_msg_command_long_decode(msg, &cmd_mavlink);
switch (cmd_mavlink.command) {
case VEHICLE_CMD_PAYLOAD_CONTROL_DISCRETE:
if (cmd_mavlink.param1 + 0.5 < 1 )
{
/*do nothing here*/
}
else if (cmd_mavlink.param1 + 0.5 < 2)
{
send_long_command();
}
if (cmd_mavlink.param2 + 0.5 < 1 )
{
/*do nothing here*/
}
else if (cmd_mavlink.param2 + 0.5 < 2)
{
digitalWrite(RELAY, HIGH);
char text_msg[50] = "Relay Turn ON!";
send_text(MAV_SEVERITY_CRITICAL, text_msg);
}
else if (cmd_mavlink.param2 + 0.5 < 3)
{
digitalWrite(RELAY, LOW);
char text_msg[50] = "Relay Turn OFF!";
send_text(MAV_SEVERITY_CRITICAL, text_msg);
}
break;
case VEHICLE_CMD_NAV_LAND:
digitalWrite(RELAY, LOW);
break;
default:
break;
}
}
void send_heartbeat()
{
mavlink_heartbeat_t msg;
msg.base_mode = 0;
msg.custom_mode = 0;
msg.type = 0;
msg.autopilot = 12;
msg.mavlink_version = 3;
msg.rc_status = 0;
send_message(MAVLINK_MSG_ID_HEARTBEAT, &msg, LOCAL_SYS_ID, LOCAL_COMP_ID, GCS_ip, GCS_port);
}
void send_text(severity_t severity, const char *text_msg)
{
__mavlink_statustext_t msg;
strcpy(msg.text, text_msg);
send_message(MAVLINK_MSG_ID_STATUSTEXT, &msg, LOCAL_SYS_ID, LOCAL_COMP_ID, GCS_ip, GCS_port);
}
void send_long_command()
{
mavlink_command_long_t command_long;
command_long.command = VEHICLE_CMD_OVERRIDE_GOTO;
command_long.target_system = 1;
command_long.target_component = 50;
command_long.confirmation = 0;
send_message(MAVLINK_MSG_ID_COMMAND_LONG, &command_long, 255, 0, Remote_ip, Remote_port);
}
I believe the servo part need to be added here
digitalWrite(RELAY, HIGH);
char text_msg[50] = "Relay Turn ON!";
send_text(MAV_SEVERITY_CRITICAL, text_msg);
}
else if (cmd_mavlink.param2 + 0.5 < 3)
{
digitalWrite(RELAY, LOW);
char text_msg[50] = "Relay Turn OFF!";
send_text(MAV_SEVERITY_CRITICAL, text_msg);