rt-thread移植并使用mavlink教程

一、移植

1、下载mavlink源码

打开mavlink官网
找到如下:
在这里插入图片描述
点击图中框下载。

2、将源文件添加到工程中

在这里插入图片描述

二、使用

1、发送包数据

void send_winch_command(winchCommage command) 
{
    
    
	mavlink_message_t msg;
	mavlink_msg_command_long_pack(/*SENDER_SYS_ID=*/1,
								  /*SENDER_COMP_ID=*/1,
								  /*MESSAGE=*/&msg,
								  /*TARGET_SYS_ID=*/0,
								  /*TARGET_COMP_ID=*/0,
								  /*CMD_ID=*/7001,
								  /*CONFIRMATION#=*/0,
								  /*P1_A2Z_COMMAND_TYPE=*/command,
								  /*P2=*/0,
								  /*P3=*/0,
								  /*P4=*/0,
								  /*P5=*/0,
								  /*P6=*/0,
								  /*P7=*/0);
	uint8_t buf[MAVLINK_MAX_PACKET_LEN];
	uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);

	// Here we presume the C file write operation but you can send the data in buf however you like.

	//write(target_filestream, buf, len);
	winch_send_data(buf, len);

	// For inspecting encoded messages:
	log_v("send data:");
	for (int i = 0; i < len; ++i) {
    
    
		elog_raw("%02X ", buf[i]);
	}
	elog_raw("\r\n");
	log_v("send over!");
	
}

2、接收处理数据

void mavlink_process() 
{
    
    
	rt_completion_wait(&winchg_received_completion,RT_WAITING_FOREVER);
	
	
	uint8_t buf[100];
	//从设备读取数据
	uint8_t count =winch_read_data(buf,sizeof(buf)); //read(target_filestream, buf, sizeof(buf));

	for (uint8_t i = 0; i < count; i++)
	{
    
    
		// This function takes a byte at a time, returning true only when the last byte of a completed message is received:
		if (mavlink_parse_char(1, buf[i], &mavlink_msg_in, &mavlink_status_in)) 
		{
    
    
		// You may also wish to check system ID, component ID, or other fields.
			if (mavlink_msg_in.msgid == 9005) 
			{
    
    
				mavlink_winch_status_t winch_status;
				mavlink_msg_winch_status_decode(&mavlink_msg_in, &winch_status);
		
				// The "ride-along" A2Z state encoded in the last 8 bytes WINCH_STATUS field:
				winchState state = winch_status.status >> 24;
		
				// Print the info for debugging
				log_d("Got a winch_status message:\n State %d\nTension %f m\nLength %f\n", state, winch_status.tension, winch_status.line_length);
				
				// Update your app/ui state (for you to impelement)
				/*
				SendStateToApp(state);
				SendMotorCurrentToApp(winch_status.tension);
				SendLineLengthToApp(winch_status.line_length);
				etc
				*/
			} 
			else if (mavlink_msg_in.msgid == 132) 
			{
    
    
				// The lidar value is encoded in the distance_sensor measurement 
				// so that a ground control station and autopilot can also make use of it
		
				mavlink_distance_sensor_t distance_sensor;
				mavlink_msg_distance_sensor_decode(&mavlink_msg_in, &distance_sensor);
		
				log_i("Got a distnace_sensor message:\n Distance %i\n", distance_sensor.current_distance);
				//SendLidarToApp(distance_sensor.current_distance);
		
			} 
			else 
			{
    
    
				log_w("Invalid message type %i\n", mavlink_msg_in.msgid);
			}
		}
	}

}

三、完整工程源码

完整工程下载点击我

猜你喜欢

转载自blog.csdn.net/qq_15181569/article/details/128312912