Hello,
I'm working on a project to control rovers via webapp. I was working with esp8266 until recently I decided to switch to esp32. I used the arduino ide to flash some code, it does get uploaded. But when I plug it into a pixhawk 2.4.8, nothing seems to work. So far I flashed on a set_actuator code that worked fine on the esp8266. I did make changes to make it work on the 32 but doesnt seem to work. Find the code below. What am I missing here?
(ps: my tx and rx pins are inserted in pin 36,37 respectively)
Edit: please ignore all serial.println() operations. I had used a separate serial earlier but got no response, so I tried to make it into a single serial.
#include <MAVLink.h>
void setup() {
Serial.begin(921600);
Serial1.begin(921600, SERIAL_8N1, 36, 37); // Initialize Serial2 with RX on pin 36 and TX on pin 37
}
void loop() {
setOffboardMode();
delay(3000);
disableRCChecks();
delay(3000);
disablePreflightChecks();
delay(3000);
disableCommanderChecks();
delay(3000);
armPixhawk();
delay(5000);
setActuator();
delay(5000);
Serial1.println("Actuator set");
stopActuators();
delay(3000);
Serial1.println("Actuators stopped");
disarmPixhawk();
delay(3000);
}
void disableRCChecks() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_param_set_pack(
255, 190, &msg,
1, 0, "COM_RC_IN_MODE", 2, MAV_PARAM_TYPE_UINT8
);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
Serial1.println("RC checks disabled");
}
void sendMavlinkMessage(const mavlink_message_t &msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
Serial1.println("Sent MAVLink command.");
}
void disablePreflightChecks() {
mavlink_message_t msg;
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "ARM_IMU_GYR", 0, MAV_PARAM_TYPE_REAL32);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "ARM_MAG", 0, MAV_PARAM_TYPE_REAL32);
sendMavlinkMessage(msg);
Serial1.println("Preflight checks disabled.");
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "RC_IN_MODE", 4, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
}
void disableCommanderChecks() {
mavlink_message_t msg;
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_SUM", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_LOC_POS", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_GLB_POS", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_MISS", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_OFFB", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_ARM_CHK_HOME", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
mavlink_msg_param_set_pack(255, 190, &msg, 1, 1, "COM_HEALTH_SUM", 0, MAV_PARAM_TYPE_UINT8);
sendMavlinkMessage(msg);
Serial1.println("Commander checks disabled.");
}
void setActuator() {
mavlink_message_t msg;
mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, 1, 1, 0, 0, 0, 0, 0);
sendMavlinkMessage(msg);
delay(3000);
mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, -1, 1, 0, 0, 0, 0, 0);
sendMavlinkMessage(msg);
delay(3000);
mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, 1, -1, 0, 0, 0, 0, 0);
sendMavlinkMessage(msg);
delay(3000);
mavlink_msg_command_long_pack(255, 190, &msg, 1, 1, MAV_CMD_DO_SET_ACTUATOR, 0, -1, -1, 0, 0, 0, 0, 0);
sendMavlinkMessage(msg);
delay(3000);
}
void armPixhawk() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_command_long_pack(
255, 190, &msg,
1, 0,
MAV_CMD_COMPONENT_ARM_DISARM,
0,
1, 0, 0, 0, 0, 0, 0
);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
Serial1.println("Pixhawk armed");
}
void disarmPixhawk() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_command_long_pack(
255, 190, &msg,
1, 0,
MAV_CMD_COMPONENT_ARM_DISARM,
0,
0, 0, 0, 0, 0, 0, 0
);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
Serial1.println("Pixhawk disarmed");
}
void setManualMode() {
mavlink_message_t msg;
mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_MANUAL_ARMED, MAV_MODE_FLAG_MANUAL_INPUT_ENABLED);
sendMavlinkMessage(msg);
Serial1.println("Set to Manual mode.");
}
void setOffboardMode() {
mavlink_message_t msg;
mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_GUIDED_ARMED, MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);
sendMavlinkMessage(msg);
Serial1.println("Set to Offboard mode.");
}
void setMissionMode() {
mavlink_message_t msg;
mavlink_msg_set_mode_pack(255, 190, &msg, 1, MAV_MODE_AUTO_ARMED, MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);
sendMavlinkMessage(msg);
Serial1.println("Set to Mission mode.");
}
void stopActuators() {
mavlink_message_t msg;
// Assuming we're setting control values to stop actuators.
mavlink_actuator_control_target_t act_control;
act_control.time_usec = 0;
act_control.group_mlx = 0; // actuator group
act_control.controls[0] = 0;
// ... set other controls as needed
mavlink_msg_actuator_control_target_encode(255, 190, &msg, &act_control);
sendMavlinkMessage(msg);
}