代码
clc;clear all;
theta_2pi = [0:0.01:2*pi];
earth_moon_l = 10; % 月亮到地球的平均距离
moon_changE_l = 3; % 嫦娥一号到月亮的平均距离
w_moon = 1; %设置月亮公转角速度
w_changE = 12; % 设置嫦娥一号绕月亮公转角速度
t=0; % 初始时刻为0
pauseTime = 0.002; % 设置暂停时间
theta_1 = 0; % 设置开始它们都在水平线上
theta_2 = 0; % 设置开始它们都在水平线上
figure('name','嫦娥一号与月亮、地球关系'); % 设置标题名字
hold on;
axis equal; % 建立坐标系
axis off; % 除掉Axes
set(gcf,'doublebuffer','on') % 消除抖动
plot(-20,18,'color','b','marker','.','markersize',40);
text(-17,18,'地球'); % 对地球进行标识
earth_plot = plot(-20,16,'color','m','marker','.','markersize',20);
text(-17,16,'月亮'); % 对月亮进行标识
moon_plot = plot(-20,14,'color','r','marker','.','markersize',13);
text(-17,14,'嫦娥一号'); % 对嫦娥一号进行标识
plot(0,0,'color','b','marker','.','markersize',60); % 画地球
plot(earth_moon_l*cos(theta_2pi),earth_moon_l*sin(theta_2pi)); % 画月亮公转轨道
set(gca,'xlim',[-20 20],'ylim',[-20 20]);
moon_x = earth_moon_l*cos(theta_1);
moon_y = earth_moon_l*sin(theta_1);
moon_lx = moon_changE_l*cos(theta_2pi);
moon_ly = moon_changE_l*sin(theta_2pi);
moon_dx = moon_changE_l*cos(theta_2);
moon_dy = moon_changE_l*sin(theta_2);
moon_plot = plot(moon_x,moon_y,'color','m','marker','.','markersize',30); % 画月亮初始位置
changE_plot_l = plot(moon_x+moon_lx,moon_y+moon_ly); % 画嫦娥一号绕月亮公转轨道
changE_plot_x = moon_x+moon_dx; % 计算嫦娥一号的初始x位置
changE_plot_y = moon_y+moon_dy; % 计算嫦娥一号的初始y位置
changE_plot = plot(changE_plot_x,changE_plot_y,'r','marker','.','markersize',20); % 画嫦娥一号的初始位置
orbit = line('xdata',changE_plot_x,'ydata',changE_plot_y,'color','r'); % 画嫦娥一号的运动轨迹
t_server = tcpip('192.168.1.103',30000,'NetworkRole','server'); % 建立服务器
t_server.InputBuffersize = 100000;
% 使用fopen会导致成功连接后才回执行后续代码
fprintf('主机地址为为192.168.1.103,端口号为30000\n');
fprintf('等待客户端响应......\n');
fopen(t_server); % 打开服务器
fprintf('成功连接至客户端.....');
while 1
moon_x = earth_moon_l*cos(theta_1);
moon_y = earth_moon_l*sin(theta_1);
moon_lx = moon_changE_l*cos(theta_2pi);
moon_ly = moon_changE_l*sin(theta_2pi);
moon_dx = moon_changE_l*cos(theta_2);
moon_dy = moon_changE_l*sin(theta_2);
set(moon_plot,'xdata',moon_x,'ydata',moon_y); % 设置月亮的运动过程
set(changE_plot_l,'xdata',moon_x+moon_lx,'ydata',moon_y+moon_ly); % 设置嫦娥一号绕月亮的公转轨道的运动过程
changE_x = moon_x+moon_dx;
changE_y = moon_y+moon_dy;
set(changE_plot,'xdata',changE_x,'ydata',changE_y); % 设置嫦娥一号的位置
changE_plot_x = [changE_plot_x changE_x];
changE_plot_y = [changE_plot_y changE_y];
set(orbit,'xdata',changE_plot_x,'ydata',changE_plot_y); % 设置嫦娥一号运动轨迹的显示过程
theta_1 = theta_1+w_moon*pauseTime; % 月亮相对地球转过的角度
theta_2 = theta_2+w_changE*pauseTime; % 嫦娥一号相对月亮转过的角度
pause(pauseTime); % 暂停一会儿
% 向“地面站”发送自身坐标(向移动端发送自身坐标)
fprintf(t_server,changE_x);
fprintf(t_server,changE_y);
drawnow
end
fclose(t_server);