00001
00002
00003
00004
00005
00006
00007
00008 #include "automobile_network.h"
00009 #include <string.h>
00010
00011
00012 automobile_network :: automobile_network(dsim_model_setup *setup) : dsim_model(setup)
00013 {
00014 }
00015
00016 void automobile_network :: initialize_data()
00017 {
00018 dsim_model::initialize_data();
00019
00020 ml_matrix tempN(0,0), temp2(2,1);
00021
00022 int transmit = 0;
00023
00024
00025
00026 message_incoming_dsim = create_output("message_incoming", sd_type_matrix, &tempN, 0, 0, "", "Message incoming");
00027 position_dsim = create_output("position", sd_type_matrix, &temp2, 2, 1, "km", "Position of parent");
00028
00029 message_outgoing_dsim = create_input("message_outgoing", sd_type_matrix, &tempN, 0, 0, "", "Message outgoing");
00030 transmit_dsim = create_input("transmit", sd_type_int, &transmit, 1, 1, "", "Transmit Signal");
00031
00032 message_incoming_dsim.mark_telemetry();
00033 message_outgoing_dsim.mark_command();
00034 transmit_dsim.mark_telemetry_and_command();
00035 position_dsim.mark_telemetry();
00036
00037 configure_timestep(true, false, true);
00038
00039
00040 create_target("automobile_network_target","A target watched by this automobile_network sensor.",true);
00041
00042 }
00043
00044 void automobile_network :: initialization_complete()
00045 {
00046 dsim_model::initialization_complete();
00047
00048 std::vector<std::string> targets = target_destinations("automobile_network_target");
00049
00050 dsim_model *p = parent();
00051
00052 parent_position_dsim = p->request_local_variable("position");
00053 parent_yaw_matrix_dsim = p->request_local_variable("yaw_matrix");
00054
00055 }
00056
00057 void automobile_network :: initialize_timestep()
00058 {
00059 dsim_model::initialize_timestep();
00060
00061
00062 message_incoming = ml_matrix(0,0);
00063 }
00064
00065
00066 void automobile_network :: pre_calculate()
00067 {
00068 dsim_model::pre_calculate();
00069
00070 std::vector<std::string> targets = target_destinations("automobile_network_target");
00071
00072 if ( targets.size() == 0 ) return;
00073
00074
00075 ml_matrix r = parent_position_dsim.value_as_matrix();
00076
00077 position_dsim.set_value_as_matrix(r);
00078
00079
00080 ml_matrix dR;
00081
00082 std::vector<std::string>::iterator i = targets.begin();
00083
00084 for (int k=1;i!=targets.end();i++,k++)
00085 {
00086
00087 std::string varPath = (*i) + ":position";
00088 dsim_variable posVar = request_variable(varPath.c_str());
00089
00090
00091 if (!posVar.valid() )
00092 {
00093 warning("Target position not valid\n");
00094 }
00095 else
00096 {
00097
00098 ml_matrix m(1,1);
00099 ml_matrix pos = posVar.value_as_matrix();
00100
00101 dR = pos - r;
00102
00103 if( transmit_dsim.value_as_int() )
00104 {
00105 m(1,1) = dR.vmag();
00106 }
00107
00108 ml_matrix message = message_outgoing_dsim.value_as_matrix();
00109
00110 if( !message.is_empty() )
00111 m.stack(message);
00112
00113 std::string path = *i;
00114 const char *cpath = path.c_str();
00115 send_message(cpath,"automobile_network_message",m);
00116 }
00117 }
00118 }
00119
00120 void automobile_network :: post_calculate()
00121 {
00122 dsim_model::post_calculate();
00123 }
00124
00129 dsim_value automobile_network :: handle_message(const std::string &sender_path,const std::string &message_name,const dsim_value &argument)
00130 {
00131 if (message_name == "automobile_network_message")
00132 {
00133 ml_matrix temp = argument.matrix_value();
00134 message_incoming.stack(temp);
00135 message_incoming_dsim.set_value_as_matrix(message_incoming);
00136
00137 return dsim_value();
00138 }
00139
00140 else return dsim_model::handle_message(sender_path,message_name,argument);
00141
00142 }
00143
00144 extern "C"
00145 {
00146 dsim_model *AutomobileNetwork(void *setup);
00147
00148 dsim_model *AutomobileNetwork(void *setup)
00149 {
00150 return new automobile_network((dsim_model_setup *)setup);
00151 }
00152 }