CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Airspeed/models/NMEA_Sensor.m
Views: 1799
1
clc
2
clear
3
4
% This is a example of using MATLAB to test a sensor drive using TCP
5
% we send a NMEA messages at 1hz, note that this assumes that SITL is
6
% running at real time.
7
8
% NMEA is a basic one way protocol but more complex protocols can be
9
% implemented in the same way
10
11
% Use the same TCP/UDP libbary that is used for MALTAB SITL
12
addpath(genpath('../../SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6'))
13
14
% if this dosn't work try the MALTAB SITL example first
15
pnet('closeall')
16
17
% Init the TCP port, 5763 is serial 2
18
u = pnet('tcpconnect','127.0.0.1',5763);
19
20
flipflop = true;
21
while(true)
22
23
if flipflop
24
% send MTW temp message
25
water_temp = 10 + randn();
26
NMEA_string = sprintf('$YXMTW,%0.1f,C',water_temp);
27
else
28
% send VHW speed message
29
water_speed_knots = 5 + randn()*2;
30
water_speed_kph = water_speed_knots * 1.852;
31
NMEA_string = sprintf('$VWVHW,,T,,M,%0.1f,N,%0.1f,F',water_speed_knots,water_speed_kph);
32
end
33
flipflop = ~flipflop;
34
35
% Calculate the correct checksum
36
NMEA_string = add_checksum(NMEA_string);
37
38
% send to ap
39
pnet(u,'printf',sprintf('%s\r\n',NMEA_string));
40
pnet(u,'writepacket');
41
42
% also print to MATLAB console
43
fprintf("%s\n",NMEA_string);
44
45
% 1hz (ish)
46
pause(1);
47
end
48
49
function NMEA_string_out = add_checksum(NMEA_string_in)
50
checksum = uint8(0);
51
for i = 2:numel(NMEA_string_in)
52
checksum = bitxor(checksum,uint8(NMEA_string_in(i)),'uint8');
53
end
54
NMEA_string_out = sprintf('%s*%s',NMEA_string_in,dec2hex(checksum));
55
end
56
57