You head a team developing a small satellite in competition for a
NASA contract. Your design calls for a central satellite that will deploy
sensor nodes. These nodes must remain within 30 km of the satellite to
allow for data transmission. If they pass out of range, they will use an
impulse of thrust propulsion to move back towards the satellite. Make a
Satellite class with the following properties:
location: An [X Y Z] vector of coordinates, with the satellite as
the origin.
magnetData: A vector storing magnetic readings.
nodeAlerts: An empty string to begin with, stores alerts when
nodes go out of range.
Satellite also has the following methods:
Satellite: The constructor, which sets location to [0 0 0] and
magnetData to 0.
retrieveData: Takes data from a node, extends the magnetData
vector.
Then, make the sensorNode class as a subclass of Satellite. It will have
the following properties:
distance: The magnitude of the distance from the satellite.
Presume that a node’s location comes from on-board, real-time
updating GPS (i.e., do not worry about updating node.location).
fuel: Sensor nodes begin with 100 kg of fuel.
sensorNode also has the following methods:
sensorNode: The constructor.
useThrust: Assume this propels node towards satellite. Each
usage consumes 2 kg of fuel. If the fuel is below 5 kg, send an
alert message to the satellite.
checkDistance: Check the magnitude of the distance between
useMagnetometer: Write this as a stub. Have the “magnetometer
reading” be a randomized number in the range 0 to 100.
sendAlert: set the “nodeAlerts” Satellite property to the string
‘Low fuel’.
First, treat both classes as value classes. Then, adjust your code so
that both are handle classes. Which code is simpler?
Solution 1: Value class
classdef Satellite
properties
location;
magnetData;
nodeAlerts;
end
methods
%Constructor
function obj = Satellite(varargin)
if nargin == 2
location = varargin{1};
magnetData = varargin{2};
else
location = [0 0 0];
%the satellite is the origin point, by default
magnetData = 0; %initialize to 0
end
end
%takes data from node, stores it aboard the satellite
function obj = retrieveData(sat,node)
node = node.useMagnetometer(node);
obj.magnetData = sat.magnetData + node.magnetData;
end
end
end
classdef sensorNode < Satellite
properties
distance = 0;
fuel = 100; %Nodes start with 100kg of fuel.
end
methods
function obj = sensorNode(varargin)
%Constructor
if nargin == 2
distance = varagin{1};
fuel = varagin{2};
elseif nargin == 4
obj@Satellite(varargin{3},varagin{4});
distance = varagin{1};
fuel = varagin{2};
end
end
function [obj1, obj2] = useThrust(sat, node)
%Use a brief impulse of thrust to move node back towards
% satellite. Assume thruster
% controls correctly determine how to move closer.
%Every time thrust is used, 2 kg of fuel is consumed.
node.fuel = node.fuel - 2;
if node.fuel <= 10
sat = node.sendAlert(sat);
end
obj1 = node;
obj2 = sat;
end
function obj = checkDistance(sat,node)
%Check distnace between nodes and satellite.
distXYZ = sat.location - node.location;
node.distance = sqrt(distXYZ(1)^2 + distXYZ(2)^2 + ...
distXYZ(3)^2);
if node.distance > 30
[node,sat] = node.useThrust(sat,node);
end
obj = node;
end
function obj = useMagnetometer(node)
%Gather data from sensor hardware
% the +10 is a stubbed value, substituting what the
% sensor will read from its magnetometer.
obj.magnetData = node.magnetData + 10;
end
function obj = sendAlert(sat)
%Alerts the satellite that the node is out of range.
sat.nodeAlerts = sprintf(‘Node out of fuel.’);
obj = sat;
end
end
Solution 2: Handle class
classdef SatelliteHx < handle
properties
location;
magnetData;
nodeAlerts;
end
methods
%Constructor
function obj = SatelliteHx(varargin)
if nargin == 2
location = varargin{1};
magnetData = varargin{2};
else
location = [0 0 0];
%the satellite is the origin point, by default
magnetData = 0;
end
end
function retrieveData(sat,node)
%takes data from node, stores it aboard the satellite
node.useMagnetometer(node);
sat.magnetData = sat.magnetData + node.magnetData;
end
end
end
classdef sensorNode < Satellite
properties
distance = 0;
fuel = 100; %Nodes start with 100kg of fuel.
end
methods
function obj = sensorNode(varargin)
%Constructor
if nargin == 2
distance = varagin{1};
fuel = varagin{2};
elseif nargin == 4
obj@Satellite(varargin{3},varagin{4});
distance = varagin{1};
fuel = varagin{2};
end
end
function useThrust(sat, node)
node.fuel = node.fuel - 2;
%Every time thrust is used, 2 kg of fuel is consumed.
if node.fuel <= 10
sat = node.sendAlert(sat);
end
end
function checkDistance(sat,node)
%Check distnace between nodes and satellite.
distXYZ = sat.location - node.location;
node.distance = sqrt(distXYZ(1)^2 + distXYZ(2)^2 + ...
distXYZ(3)^2);
if node.distance > 30
node.useThrust(sat,node);
end
end
function useMagnetometer(node)
%Gather data from sensor hardware
%the +10 is a stubbed value, substituting what the
%sensor will read from its magnetometer.
node.magnetData = node.magnetData + 10;
end
%Gather
function sendAlert(sat)
sat.nodeAlerts = sprintf(‘Node out of fuel.’);
end
end
need an explanation for this answer? contact us directly to get an explanation for this answer