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

(-)d64b1561b1c2 (+179 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2007 INRIA
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
7
 * published by the Free Software Foundation;
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
 */
20
#include <cmath>
21
#include "ns3/simulator.h"
22
#include "ns3/random-variable.h"
23
#include "ns3/random-variable-default-value.h"
24
#include "ns3/type-id-default-value.h"
25
#include "waypoint-list-mobility-model.h"
26
#include "random-position.h"
27
28
namespace ns3 {
29
30
NS_OBJECT_ENSURE_REGISTERED (WaypointListMobilityModel);
31
32
static RandomVariableDefaultValue
33
g_speed ("WaypointListSpeed",
34
	 "A random variable used to pick the speed of a random waypoint model.",
35
	 "Uniform:10:20");
36
37
38
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters ()
39
  : m_speed (g_speed.Get ())
40
{
41
}
42
WaypointListMobilityModelParameters::WaypointListMobilityModelParameters (const RandomVariable &speed,
43
                                                                          const std::vector<Vector> &waypoints)
44
  : m_speed (speed),
45
    m_waypoints (waypoints)
46
{
47
}
48
void 
49
WaypointListMobilityModelParameters::SetWaypoints (const std::vector<Vector> &waypoints)
50
{
51
  m_waypoints = waypoints;
52
}
53
void 
54
WaypointListMobilityModelParameters::SetSpeed (const RandomVariable &speed)
55
{
56
  m_speed = speed;
57
}
58
59
Ptr<WaypointListMobilityModelParameters>
60
WaypointListMobilityModelParameters::GetCurrent (void)
61
{
62
  static Ptr<WaypointListMobilityModelParameters> parameters = 0;
63
  if (parameters == 0 ||
64
      g_speed.IsDirty ())
65
    {
66
      parameters = CreateObject<WaypointListMobilityModelParameters> ();
67
    }
68
  return parameters;
69
}
70
71
TypeId
72
WaypointListMobilityModel::GetTypeId (void)
73
{
74
  static TypeId tid = TypeId ("WaypointListMobilityModel")
75
    .SetParent<MobilityModel> ()
76
    .AddConstructor<WaypointListMobilityModel> ()
77
    .AddConstructor<WaypointListMobilityModel,Ptr<WaypointListMobilityModelParameters> > ();
78
  return tid;
79
}
80
81
WaypointListMobilityModel::WaypointListMobilityModel ()
82
  : m_parameters (WaypointListMobilityModelParameters::GetCurrent ())
83
{
84
  Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this);
85
}
86
87
WaypointListMobilityModel::WaypointListMobilityModel (Ptr<WaypointListMobilityModelParameters> parameters)
88
  : m_parameters (parameters)
89
{
90
  Simulator::ScheduleNow (&WaypointListMobilityModel::Start, this);
91
  NotifyCourseChange ();
92
}
93
94
void
95
WaypointListMobilityModel::BeginWalk (void)
96
{
97
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
98
    {
99
      // list of waypoints is empty; nothing to do
100
      return;
101
    }
102
  Vector destination = *m_nextWaypoint;
103
  NextWaypoint ();
104
  Vector current = GetPosition ();
105
  m_walkStartPoint = current;
106
  m_walkStartTime = Simulator::Now ();
107
  double speed = m_parameters->m_speed.GetValue ();
108
  double dx = (destination.x - current.x);
109
  double dy = (destination.y - current.y);
110
  double dz = (destination.z - current.z);
111
  double sq = dx*dx + dy*dy + dz*dz;
112
  if (sq == 0)
113
    {
114
      m_velocity = Vector (0, 0, 0);
115
    }
116
  else
117
    {
118
      double k = speed / std::sqrt (sq);
119
      m_velocity = Vector (k*dx, k*dy, k*dz);
120
    }
121
  Time travelDelay = Seconds (CalculateDistance (destination, current) / speed);
122
  m_walkEndEvent = Simulator::Schedule
123
    (travelDelay, &WaypointListMobilityModel::BeginWalk, this);
124
  NotifyCourseChange ();
125
}
126
127
128
/// prepare next waypoint
129
void
130
WaypointListMobilityModel::NextWaypoint (void)
131
{
132
  m_nextWaypoint++;
133
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
134
    {
135
      // cycle back to the first waypoint
136
      m_nextWaypoint = m_parameters->m_waypoints.begin ();
137
    }  
138
}
139
140
void
141
WaypointListMobilityModel::Start (void)
142
{
143
  if (m_nextWaypoint == m_parameters->m_waypoints.end ())
144
    {
145
      NS_FATAL_ERROR("list of waypoints is empty; nothing to do");
146
      return;
147
    }
148
  m_nextWaypoint = m_parameters->m_waypoints.begin ();
149
  m_velocity = Vector (0, 0, 0);
150
  m_walkStartTime = Simulator::Now ();
151
  m_walkStartPoint = *m_nextWaypoint;
152
  BeginWalk ();
153
}
154
155
Vector
156
WaypointListMobilityModel::DoGetPosition (void) const
157
{
158
  double dt = (Simulator::Now () - m_walkStartTime).GetSeconds ();
159
  return Vector (m_walkStartPoint.x + dt*m_velocity.x,
160
                 m_walkStartPoint.y + dt*m_velocity.y,
161
                 m_walkStartPoint.z + dt*m_velocity.z);
162
}
163
void 
164
WaypointListMobilityModel::DoSetPosition (const Vector &position)
165
{
166
  NS_FATAL_ERROR ("Cannot change the position WaypointListMobilityModel models, only the waypoints");
167
}
168
Vector
169
WaypointListMobilityModel::DoGetVelocity (void) const
170
{
171
  return m_velocity;
172
}
173
174
WaypointListMobilityModel::~WaypointListMobilityModel ()
175
{
176
  m_walkEndEvent.Cancel ();
177
}
178
179
} // namespace ns3
(-)d64b1561b1c2 (+112 lines)
Added Link Here 
1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
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