View | Details | Raw Unified | Return to bug 145
Collapse All | Expand All

(-)d64b1561b1c2 (+180 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2008 INESC Porto
4
 * All rights reserved.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License version 2 as
8
 * published by the Free Software Foundation;
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * Author: Gustavo Carneiro  <gjc@inescporto.pt>
20
 */
21
#include <cmath>
22
#include "ns3/simulator.h"
23
#include "ns3/random-variable.h"
24
#include "ns3/random-variable-default-value.h"
25
#include "ns3/type-id-default-value.h"
26
#include "waypoint-list-mobility-model.h"
27
#include "random-position.h"
28
29
namespace ns3 {
30
31
NS_OBJECT_ENSURE_REGISTERED (WaypointListMobilityModel);
32
33
static RandomVariableDefaultValue
34
g_speed ("WaypointListSpeed",
35
	 "A random variable used to pick the speed of a random waypoint model.",
36
	 "Uniform:10:20");
37
38
39
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters ()
40
  : m_speed (g_speed.Get ())
41
{
42
}
43
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters (const RandomVariable &speed,
44
                                                                          const std::vector<Vector> &waypoints)
45
  : m_speed (speed),
46
    m_waypoints (waypoints)
47
{
48
}
49
void 
50
WaypointListMobilityModelParameters::SetWaypoints (const std::vector<Vector> &waypoints)
51
{
52
  m_waypoints = waypoints;
53
}
54
void 
55
WaypointListMobilityModelParameters::SetSpeed (const RandomVariable &speed)
56
{
57
  m_speed = speed;
58
}
59
60
Ptr<WaypointListMobilityModelParameters>
61
WaypointListMobilityModelParameters::GetCurrent (void)
62
{
63
  static Ptr<WaypointListMobilityModelParameters> parameters = 0;
64
  if (parameters == 0 ||
65
      g_speed.IsDirty ())
66
    {
67
      parameters = CreateObject<WaypointListMobilityModelParameters> ();
68
    }
69
  return parameters;
70
}
71
72
TypeId
73
WaypointListMobilityModel::GetTypeId (void)
74
{
75
  static TypeId tid = TypeId ("WaypointListMobilityModel")
76
    .SetParent<MobilityModel> ()
77
    .AddConstructor<WaypointListMobilityModel> ()
78
    .AddConstructor<WaypointListMobilityModel,Ptr<WaypointListMobilityModelParameters> > ();
79
  return tid;
80
}
81
82
WaypointListMobilityModel::WaypointListMobilityModel ()
83
  : m_parameters (WaypointListMobilityModelParameters::GetCurrent ())
84
{
85
  Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this);
86
}
87
88
WaypointListMobilityModel::WaypointListMobilityModel (Ptr<WaypointListMobilityModelParameters> parameters)
89
  : m_parameters (parameters)
90
{
91
  Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this);
92
  NotifyCourseChange ();
93
}
94
95
void
96
WaypointListMobilityModel::BeginWalk (void)
97
{
98
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
99
    {
100
      // list of waypoints is empty; nothing to do
101
      return;
102
    }
103
  Vector destination = *m_nextWaypoint;
104
  NextWaypoint ();
105
  Vector current = GetPosition ();
106
  m_walkStartPoint = current;
107
  m_walkStartTime = Simulator::Now ();
108
  double speed = m_parameters->m_speed.GetValue ();
109
  double dx = (destination.x - current.x);
110
  double dy = (destination.y - current.y);
111
  double dz = (destination.z - current.z);
112
  double sq = dx*dx + dy*dy + dz*dz;
113
  if (sq == 0)
114
    {
115
      m_velocity = Vector (0, 0, 0);
116
    }
117
  else
118
    {
119
      double k = speed / std::sqrt (sq);
120
      m_velocity = Vector (k*dx, k*dy, k*dz);
121
    }
122
  Time travelDelay = Seconds (CalculateDistance (destination, current) / speed);
123
  m_walkEndEvent = Simulator::Schedule
124
    (travelDelay, &WaypointListMobilityModel::BeginWalk, this);
125
  NotifyCourseChange ();
126
}
127
128
129
/// prepare next waypoint
130
void
131
WaypointListMobilityModel::NextWaypoint (void)
132
{
133
  m_nextWaypoint++;
134
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
135
    {
136
      // cycle back to the first waypoint
137
      m_nextWaypoint = m_parameters->m_waypoints.begin ();
138
    }  
139
}
140
141
void
142
WaypointListMobilityModel::Start (void)
143
{
144
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
145
    {
146
      NS_FATAL_ERROR("list of waypoints is empty; nothing to do");
147
      return;
148
    }
149
  m_nextWaypoint = m_parameters->m_waypoints.begin ();
150
  m_velocity = Vector (0, 0, 0);
151
  m_walkStartTime = Simulator::Now ();
152
  m_walkStartPoint = *m_nextWaypoint;
153
  BeginWalk ();
154
}
155
156
Vector
157
WaypointListMobilityModel::DoGetPosition (void) const
158
{
159
  double dt = (Simulator::Now () - m_walkStartTime).GetSeconds ();
160
  return Vector (m_walkStartPoint.x + dt*m_velocity.x,
161
                 m_walkStartPoint.y + dt*m_velocity.y,
162
                 m_walkStartPoint.z + dt*m_velocity.z);
163
}
164
void 
165
WaypointListMobilityModel::DoSetPosition (const Vector &position)
166
{
167
  NS_FATAL_ERROR ("Cannot change the position WaypointListMobilityModel models, only the waypoints");
168
}
169
Vector
170
WaypointListMobilityModel::DoGetVelocity (void) const
171
{
172
  return m_velocity;
173
}
174
175
WaypointListMobilityModel::~WaypointListMobilityModel ()
176
{
177
  m_walkEndEvent.Cancel ();
178
}
179
180
} // namespace ns3
(-)d64b1561b1c2 (+112 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2008 INESC Porto
4
 * All rights reserved.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License version 2 as
8
 * published by the Free Software Foundation;
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * Author: Gustavo Carneiro  <gjc@inescporto.pt>
20
 */
21
#ifndef WAYPOINT_LIST_MOBILITY_MODEL_H
22
#define WAYPOINT_LIST_MOBILITY_MODEL_H
23
24
#include "static-speed-helper.h"
25
#include "mobility-model.h"
26
#include "random-position.h"
27
#include "ns3/ptr.h"
28
#include "ns3/random-variable.h"
29
30
namespace ns3 {
31
32
/**
33
 * \brief the parameters which control the behavior of a random waypoint
34
 *        mobility model.
35
 */
36
class WaypointListMobilityModelParameters : public Object
37
{
38
public:
39
  /**
40
   * Default parameters from \valueref{WaypointListPause},
41
   * and, \valueref{WaypointListPosition}.
42
   */
43
  WaypointListMobilityModelParameters ();
44
  /**
45
   * \param randomPosition a random position model to choose the position of waypoints.
46
   * \param speed a random variable to choose the speed
47
   * \param pause a random variable to choose the pause delay
48
   */
49
  WaypointListMobilityModelParameters (const RandomVariable &speed,
50
                                       const std::vector<Vector> &waypoints);
51
  /**
52
   * \param waypoints a list of waypoints
53
   */
54
  void SetWaypoints (const std::vector<Vector> &waypoints);
55
  /**
56
   * \param speed a random variable to choose the speed
57
   */
58
  void SetSpeed (const RandomVariable &speed);
59
private:
60
  friend class WaypointListMobilityModel;
61
  static Ptr<WaypointListMobilityModelParameters> GetCurrent (void);
62
  RandomVariable m_speed;
63
  std::vector<Vector> m_waypoints;
64
};
65
66
/**
67
 * \brief a waypoint list mobility model
68
 *
69
 * Each object chooses a random speed, and a random pause time: it
70
 * then pauses for the specified pause time, and starts moving towards
71
 * the specified destination with the specified speed. Once the
72
 * destination is reached it picks the next waypoint and the process
73
 * starts again.
74
 *
75
 * The implementation of this model is not 2d-specific. i.e. if you provide
76
 * a 3d random waypoint position model to this mobility model, the model 
77
 * will still work.
78
 */
79
class WaypointListMobilityModel : public MobilityModel
80
{
81
public:
82
  static TypeId GetTypeId (void);
83
  /**
84
   * Default parameters from \valueref{WaypointListPause},
85
   * and, \valueref{WaypointListPosition}.
86
   */
87
  WaypointListMobilityModel ();
88
  /**
89
   * \param parameters the parameters which control the behavior of this model.
90
   */
91
  WaypointListMobilityModel (Ptr<WaypointListMobilityModelParameters> parameters);
92
  
93
  ~WaypointListMobilityModel ();
94
private:
95
  void Start (void);
96
  virtual Vector DoGetPosition (void) const;
97
  virtual void DoSetPosition (const Vector &position);
98
  virtual Vector DoGetVelocity (void) const;
99
  void NextWaypoint (void);
100
  void BeginWalk (void);
101
102
  Ptr<WaypointListMobilityModelParameters> m_parameters;
103
  std::vector<Vector>::const_iterator m_nextWaypoint;
104
  EventId m_walkEndEvent;
105
  Time m_walkStartTime;
106
  Vector m_walkStartPoint;
107
  Vector m_velocity;
108
};
109
110
} // namespace ns3
111
112
#endif /* WAYPOINT_LIST_MOBILITY_MODEL_H */
(-)a/src/mobility/wscript (+2 lines)
 Lines 19-24   def build(bld): Link Here 
19
        'random-walk-2d-mobility-model.cc',
19
        'random-walk-2d-mobility-model.cc',
20
        'random-direction-2d-mobility-model.cc',
20
        'random-direction-2d-mobility-model.cc',
21
        'ns2-mobility-file-topology.cc',
21
        'ns2-mobility-file-topology.cc',
22
        'waypoint-list-mobility-model.cc',
22
        ]
23
        ]
23
24
24
    headers = bld.create_obj('ns3header')
25
    headers = bld.create_obj('ns3header')
 Lines 39-42   def build(bld): Link Here 
39
        'random-walk-2d-mobility-model.h',
40
        'random-walk-2d-mobility-model.h',
40
        'random-direction-2d-mobility-model.h',
41
        'random-direction-2d-mobility-model.h',
41
        'ns2-mobility-file-topology.h',
42
        'ns2-mobility-file-topology.h',
43
        'waypoint-list-mobility-model.h',
42
        ]
44
        ]
(-)a/utils/mobility-visualizer-model.cc (+15 lines)
 Lines 15-20    Link Here 
15
#include "ns3/node-list.h"
15
#include "ns3/node-list.h"
16
#include "ns3/rectangle-default-value.h"
16
#include "ns3/rectangle-default-value.h"
17
#include "ns3/type-id-default-value.h"
17
#include "ns3/type-id-default-value.h"
18
#include "ns3/waypoint-list-mobility-model.h"
19
#include "ns3/log.h"
18
20
19
#include "mobility-visualizer.h"
21
#include "mobility-visualizer.h"
20
22
 Lines 76-81   int model_init (int argc, char *argv[], Link Here 
76
  DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300");
78
  DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300");
77
  DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300");
79
  DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300");
78
  DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30");
80
  DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30");
81
  DefaultValue::Bind ("WaypointListSpeed", "Uniform:100:150");
79
82
80
//   DefaultValue::Bind ("RandomDiscPositionX", "100");
83
//   DefaultValue::Bind ("RandomDiscPositionX", "100");
81
//   DefaultValue::Bind ("RandomDiscPositionY", "50");
84
//   DefaultValue::Bind ("RandomDiscPositionY", "50");
 Lines 98-103   int model_init (int argc, char *argv[], Link Here 
98
    }
101
    }
99
102
100
  topology.Layout (NodeList::Begin (), NodeList::End ());
103
  topology.Layout (NodeList::Begin (), NodeList::End ());
104
  g_numNodes++;
105
  Ptr<Node> node = CreateObject<Node> ();
106
  std::vector<Vector> waypoints;
107
  for (double theta = 0; theta < 2*M_PI; theta += 2*M_PI/20)
108
    {
109
      double x = cos(theta)*100;
110
      double y = sin(theta)*100;
111
      waypoints.push_back (Vector (200+x, 150+y, 0));
112
    }
113
  Ptr<WaypointListMobilityModelParameters> params = Create<WaypointListMobilityModelParameters> ();
114
  params->SetWaypoints (waypoints);
115
  node->AggregateObject (CreateObject<WaypointListMobilityModel> (params));
101
116
102
  Simulator::Schedule (g_sampleInterval, Sample);
117
  Simulator::Schedule (g_sampleInterval, Sample);
103
118

Return to bug 145