V-ART
noisydofmover.cpp
Go to the documentation of this file.
1 
5 #include "vart/noisydofmover.h"
7 #include "vart/dof.h"
8 #include <cstdlib>
9 
10 //#include <iostream>
11 using namespace std;
12 
13 float VART::NoisyDofMover::defaultNoiseAmplitude = 0.0f; // no noise
14 float VART::NoisyDofMover::defaultNoiseWaveLenght = 1.01f; // no subgoals
15 float VART::NoisyDofMover::defaultOvershoot = 0.0f; // no overshoot
16 float VART::NoisyDofMover::defaultOffset = 0.0f; // no offset
17 float VART::NoisyDofMover::defaultPeakTime = 0.9f; // overshoot at 90% movement
18 
20 // "active" is initialized at DofMover()
21 {
22 }
23 
24 void VART::NoisyDofMover::Initialize(float iniTime, float finTime, float finPos)
25 // virtual method
26 {
27  initialTime = iniTime;
28  finalTime = finTime;
29  targetPosition = finPos;
30  noiseAmplitude = defaultNoiseAmplitude;
31  noiseWaveLenght = defaultNoiseWaveLenght;
32  overshoot = defaultOvershoot;
33  offset = defaultOffset;
34  peakTime = defaultPeakTime;
35  hasOvershoot = (defaultOvershoot > 0.001);
36  hasNoise = (defaultNoiseAmplitude > 0.001);
37 }
38 
39 void VART::NoisyDofMover::SetNoise(float newNoiseAmplitude, float newNoiseWaveLenght)
40 {
41  noiseAmplitude = newNoiseAmplitude;
42  noiseWaveLenght = newNoiseWaveLenght;
43  previousSubGoalTime = initialTime;
44  previousSubGoalPosition = 0.0f;
45  nextSubGoalTime = initialTime + noiseWaveLenght;
46  nextSubGoalPosition = Random(); // probably not needed anymore, since activation computes noise.
47  hasNoise = true;
48 }
49 
50 //~ void VART::NoisyDofMover::SetPositionalError(float newOvershoot, float newOffset)
51 //~ {
52  //~ overshoot = newOvershoot;
53  //~ offset = newOffset;
54  //~ hasOvershoot = true;
55 //~ }
56 
57 //~ void VART::NoisyDofMover::SetPositionalError(float newOvershoot, float newOffset, float newPeakTime)
58 //~ {
59  //~ overshoot = newOvershoot;
60  //~ offset = newOffset;
61  //~ peakTime = newPeakTime;
62  //~ hasOvershoot = true;
63 //~ }
64 
65 void VART::NoisyDofMover::SetOvershoot(float amplitude, float peak)
66 {
67  overshoot = amplitude;
68  peakTime = peak;
69  hasOvershoot = (amplitude > 0.001);
70 }
71 
72 void VART::NoisyDofMover::SetOffset(float newOffset)
73 {
74  offset = newOffset;
75 }
76 
78 // virtual method
79 {
80  if ((goalTime > initialTime) && (goalTime < finalTime))
81  {
82  float interpolationIndex;
83  float goalPosition;
84 
85  if (active)
86  {
87  // Really move
88  interpolationIndex = (goalTime - activationTime) / timeRange;
89  //~ cout << interpolationIndex<<" "<<interpolationIndex-peakTime<<"\n";
90  if (hasOvershoot)
91  {
92  if (interpolationIndex < peakTime)
93  // moving from initialPosition to overshoot position
94  goalPosition = interpolatorPtr->GetValue(interpolationIndex / peakTime,
95  initialPosition, overshootRange);
96  else
97  // moving from overshoot position to targetPosition
98  goalPosition = interpolatorPtr->GetValue((interpolationIndex-peakTime)/(1-peakTime),
99  overshootPosition, finalRange);
100  }
101  else
102  // no overshoot
103  goalPosition = interpolatorPtr->GetValue(interpolationIndex,
104  initialPosition, positionRange);
105  if (hasNoise)
106  {
107  float myNoise = Noise();
108  //~ cout << goalTime<<" "<<myNoise<<"\n";
109  goalPosition += myNoise;
110  }
111  //~ cout << goalTime<<" "<<goalPosition<<"\n";
112  targetDofPtr->MoveTo(goalPosition, priority);
113  }
114  else
115  { // Activate
116  initialPosition = targetDofPtr->GetCurrent();
117  nextSubGoalPosition = Random();
118  active = true;
119  positionRange = targetPosition + offset - initialPosition;
120  timeRange = finalTime - goalTime;
121  // Validade against minimum duration to avoid movements that are too fast.
122  if (timeRange < minimumDuration)
123  // When time range is too short, the movement will not reach its goal, but it will
124  // be as fast as possible.
125  timeRange = minimumDuration;
126  activationTime = goalTime;
127  overshootRange = positionRange * overshoot;
128  finalRange = -overshootRange;
129  overshootPosition = targetPosition + offset + overshootRange;
130  overshootRange += positionRange;
131  // Move to current position, so that lower priority movers do not take effect
132  targetDofPtr->MoveTo(initialPosition, priority);
133  //~ cout << "NoisyDofMover "<<this<<" activating from: "<<initialPosition<<" to: "<<targetPosition<<"\n";
134  //~ cout << "overshootPosition: "<<overshootPosition<<endl;
135  }
136  }
137  else
138  // If outside its time range, the dof mover could be deactivating.
139  active = false;
140 }
141 
143 {
144  static VART::SineInterpolator interpolator;
145  float subPositionRange;
146  float linearIndex;
147 
148  if (goalTime > nextSubGoalTime)
149  {
150  previousSubGoalTime = nextSubGoalTime;
151  previousSubGoalPosition = nextSubGoalPosition;
152  nextSubGoalTime += noiseWaveLenght;
153  if (nextSubGoalTime > finalTime)
154  {
155  // into last part of path
156  nextSubGoalTime = finalTime;
157  if ((nextSubGoalTime - previousSubGoalTime) > 0.001)
158  // protection against removing large amounts of noise in a short time
159  nextSubGoalPosition = 0.0f;
160  }
161  else
162  {
163  // changing path parts
164  nextSubGoalPosition = Random();
165  //~ cout<<"noise: "<<nextSubGoalPosition<<" at "<<nextSubGoalTime<<"\n";
166  }
167  }
168  else
169  {
170  if (goalTime < previousSubGoalTime)
171  {
172  // wraping around
173  previousSubGoalTime = 0.0f;
174  previousSubGoalPosition = 0.0f;
175  nextSubGoalTime = noiseWaveLenght;
176  nextSubGoalPosition = Random();
177  }
178  else
179  {
180  // same path part as before
181  }
182  }
183  subPositionRange = nextSubGoalPosition - previousSubGoalPosition;
184  linearIndex = (goalTime - previousSubGoalTime)/(nextSubGoalTime-previousSubGoalTime);
185  return interpolator.GetValue(linearIndex, previousSubGoalPosition, subPositionRange);
186 }
187 
189 {
190  return ((static_cast<float>(rand())/RAND_MAX) - 0.5f) * noiseAmplitude;
191 }
192 
194 {
195  defaultNoiseAmplitude = value;
196 }
197 
199 {
200  defaultNoiseWaveLenght = value;
201 }
202 
204 {
205  defaultOvershoot = value;
206 }
207 
209 {
210  defaultOffset = value;
211 }
212 
214 {
215  defaultPeakTime = value;
216 }
Header file for V-ART class "Dof".
static float defaultNoiseWaveLenght
Definition: noisydofmover.h:66
virtual void Move()
Changes target DOF.
Smooth (sine function) interpolator.
static void SetDefaultNoiseAmplitude(float value)
NoisyDofMover()
Creates an unitialized noisy DOF mover.
Header file for V-ART class "SineInterpolator".
static void SetDefaultOvershoot(float value)
static float defaultPeakTime
Definition: noisydofmover.h:69
void SetNoise(float newNoiseAmplitude, float newNoiseWaveLenght)
Sets the noise attributes.
float Noise()
Generates and returns corehent noise.
static void SetDefaultOffset(float value)
void SetOvershoot(float amplitude, float peak)
Sets the overshoot attributes.
virtual void Initialize(float iniTime, float finTime, float finPos)
Initializes a noisy DOF mover.
static float defaultOvershoot
Definition: noisydofmover.h:67
float Random()
Generates and returns a pseudo-random number within internal noise amplitude.
void SetOffset(float newOffset)
Set the offset attribute.
Header file for V-ART class "NoisyDofMover".
static void SetDefaultNoiseWaveLenght(float value)
static void SetDefaultPeakTime(float value)
static float defaultOffset
Definition: noisydofmover.h:68
static float defaultNoiseAmplitude
Definition: noisydofmover.h:65
virtual float GetValue(float linearIndex, float initialPos, float range) const
Evaluates a goal position for DOF interpolation.