summaryrefslogtreecommitdiffstats
path: root/source/Concept/Framework/modules/executor/navigator.c
blob: 928a5ff7bbce16f7f776ffe8ca5ca4b6ab390b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "navigator.h"

//-----------------------------------------------------------------------------
void Navigator::Stop()
{	
	this->direction = EMPTY_FLOAT;
	this->targetAngle = EMPTY_FLOAT;
	this->targetX = EMPTY_FLOAT;
	this->targetY = EMPTY_FLOAT;
	this->robotSpeed = 0;
	this->rotationSpeed = 0;

	for(uint8 i = IO_ENGINE_START; i < IO_ENGINE_END; i++)
	{
		(parent->GetModule<Engine>(i))->SetSpeed(0);
		(parent->GetModule<Engine>(i))->SetEnabled(true);
	}
}

//-----------------------------------------------------------------------------
void Navigator::Rotate(float rotationSpeed)
{
	this->rotationSpeed = min(fabs(rotationSpeed), 255.0f) * sign(rotationSpeed);
	this->direction = EMPTY_FLOAT;
	this->targetAngle = EMPTY_FLOAT;
	this->targetX = EMPTY_FLOAT;
	this->targetY = EMPTY_FLOAT;
	this->robotSpeed = 0;

	for(uint8 i = IO_ENGINE_START; i < IO_ENGINE_END; i++)
	{
		(parent->GetModule<Engine>(i))->SetSpeed(rotationSpeed);
		(parent->GetModule<Engine>(i))->SetEnabled(true);
	}
}

//-----------------------------------------------------------------------------
void Navigator::Drive(float newDirection, float newAngle, float newSpeed, float rotationSpeed)
{
	this->rotationSpeed = min(rotationSpeed, 255.0f);
	this->direction = newDirection * PI / 180.0f;
	this->targetAngle = newAngle * PI / 180.0f;
	this->targetX = EMPTY_FLOAT;
	this->targetY = EMPTY_FLOAT;
	this->robotSpeed = newSpeed;

	if(targetAngle == EMPTY_FLOAT)
	{
		rotationSpeed = 0;
	}
	else
	{
		rotationSpeed = fabs(rotationSpeed) * sign(PI - (targetAngle - (parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN))->GetOrientation()));
	}

	CalculateEngines();
}

//-----------------------------------------------------------------------------
void Navigator::DriveTo(float newX, float newY, float newAngle, float newSpeed, float rotationSpeed)
{
	if(newX < 0 || newY < 0) return;

	Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);

	this->rotationSpeed = min(rotationSpeed, 255.0f);
	this->targetX = newX;
	this->targetY = newY;
	this->direction = direction2d(locationeer->GetPositionX(), locationeer->GetPositionY(), targetX, targetY);;
	this->targetAngle = newAngle * PI / 180.0f;
	this->robotSpeed = newSpeed;

	if(targetAngle - locationeer->GetOrientation() > PI)
	{
		if(rotationSpeed > 0)
		{
			rotationSpeed = -rotationSpeed;
		}
	}
	else
	{
		if(rotationSpeed < 0)
		{
			rotationSpeed = -rotationSpeed;
		}
	}

	CalculateDirection();
}

//-----------------------------------------------------------------------------
void Navigator::Update()
{
	Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);

	bool targetReached = false;
	bool targetAngleReached = false;

	if(HasTarget() && distance2d(targetX, targetY, locationeer->GetPositionX(), locationeer->GetPositionY()) < 1.0f)
	{
		targetX = EMPTY_FLOAT;
		targetY = EMPTY_FLOAT;
		direction = EMPTY_FLOAT;
		robotSpeed = 0;

		targetReached = true;
	}

	if(targetAngle != EMPTY_FLOAT && fabs(targetAngle - locationeer->GetOrientation()) < 0.1f)
	{
		targetAngle = EMPTY_FLOAT;
		rotationSpeed = 0;

		targetAngleReached = true;
	}

	if(targetReached && targetAngleReached)
	{
		Stop();
	}
	else if(targetReached || targetAngleReached)
	{
		CalculateDirection();
	}

	if(!(correctionCountdown--))
	{
		CalculateDirection();
	}
}

//-----------------------------------------------------------------------------
void Navigator::CalculateDirection()
{
	correctionCountdown = CYCLES_PER_CORRECTION;

	if(targetAngle == EMPTY_FLOAT && !HasTarget()) return;

	Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
	
	if(HasTarget())
	{
		direction = direction2d(locationeer->GetPositionX(), locationeer->GetPositionY(), targetX, targetY);
	}

	if(targetAngle != EMPTY_FLOAT)
	{
		rotationSpeed = fabs(rotationSpeed) * sign(PI - (targetAngle - locationeer->GetOrientation()));
	}

	CalculateEngines();
}

//-----------------------------------------------------------------------------
void Navigator::CalculateEngines()
{
	// Use the position_tracker
	Position_Tracker* locationeer = parent->GetModule<Position_Tracker>(IO_POSITION_TRACKER_MAIN);
	// get the relative position of the robot
	float relativeDirection = this->direction - locationeer->GetOrientation();

	// calculate x and y-koordinates
	float xDrive = cos(relativeDirection + PI / 6.0f);
	float yDrive = sin(relativeDirection + PI / 6.0f);

	// calculate relative speed of engines
	float vLeft = xDrive;
	float vBack = (-xDrive + sqrt(3) * yDrive) / 2.0f;
	float vRight = (-xDrive - sqrt(3) * yDrive) / 2.0f;

	// Get the maximal value
	float speedCorrection = (float)max(max(fabs(vLeft),fabs(vBack)),fabs(vRight));

	// calculate the correct speeds of the engines
	vLeft = ((float)vLeft * (float)((float)this->robotSpeed / (float)speedCorrection)) + this->rotationSpeed;
	vBack = ((float)vBack * (float)((float)this->robotSpeed / (float)speedCorrection)) + this->rotationSpeed;
	vRight = ((float)vRight * (float)((float)this->robotSpeed / (float)speedCorrection)) + this->rotationSpeed;

	//(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print(vLeft,10,2);
	//(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print(vBack,10,3);
	//(parent->GetModule<Display>(IO_DISPLAY_MAIN))->Print(vRight,10,4);



	// Transfer the values to the engines
	Engine* curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_LEFT);
	curEngine->SetSpeed(vLeft);
	curEngine->SetEnabled(true);
	curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_BACK);
	curEngine->SetSpeed(vBack);
	curEngine->SetEnabled(true);
	curEngine = parent->GetModule<Engine>(IO_ENGINE_DRIVE_RIGHT);
	curEngine->SetSpeed(vRight);
	curEngine->SetEnabled(true);
	curEngine = NULL;
}