Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Robobin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
plw1g21
Robobin
Commits
94f8a74f
Commit
94f8a74f
authored
6 months ago
by
jlKronos01
Browse files
Options
Downloads
Patches
Plain Diff
Revert "Updated d_origin"
This reverts commit
3e664451
.
parent
3c6933cf
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Wireless_Communication/UWB/Beacons_tag_position/beacons_D_origin.py
+33
-33
33 additions, 33 deletions
...ommunication/UWB/Beacons_tag_position/beacons_D_origin.py
with
33 additions
and
33 deletions
Wireless_Communication/UWB/Beacons_tag_position/beacons_D_origin.py
+
33
−
33
View file @
94f8a74f
...
...
@@ -2,54 +2,63 @@ from scipy.optimize import least_squares
import
numpy
as
np
# Measured distances arrive in order: A, E, D, B, F, C
measured_distances
=
[
1496.692877
,
1477.462413
,
677.287532
,
947.921123
,
1358.796385
,
922.593
19
6
]
measured_distances
=
[
550.00
,
514.78
,
700.00
,
269.26
,
890.22
,
1051.
19
]
# Introduce ±10 cm of noise
into the measurements
noise_level
=
0
# Set to zero for no noise
# Introduce ±10 cm of noise
noise_level
=
1
0
measured_distances_noisy
=
measured_distances
+
np
.
random
.
uniform
(
-
noise_level
,
noise_level
,
size
=
len
(
measured_distances
))
# Automatically generate a r
obust
initial guess
# Automatically generate a r
easonable
initial guess
def
generate_initial_guess
(
measured_distances
):
y_A
=
measured_distances
[
2
]
# Distance from A to D
# Guess for B based on distance A-B and B-D
x_B
=
measured_distances
[
0
]
/
2
y_B
=
measured_distances
[
4
]
/
2
+
2
00
# Start y_B above y_C
y_B
=
measured_distances
[
4
]
/
2
+
1
00
# Start y_B above y_C
# Guess for C
with symmetrical logic
x_C
=
-
measured_distances
[
5
]
/
2
# Allow for negative x_C
y_C
=
-
measured_distances
[
1
]
/
2
# Allow for negative y_C
# Guess for C
based on distance C-D and A-C
x_C
=
measured_distances
[
5
]
/
2
y_C
=
measured_distances
[
1
]
/
2
return
[
x_B
,
y_B
,
x_C
,
y_C
,
y_A
]
#
Generate bounds based on measured distance
s
#
Automatically generate reasonable bound
s
def
generate_bounds
(
measured_distances
):
min_dist
=
min
(
measured_distances
)
max_dist
=
max
(
measured_distances
)
# Define lower and upper bounds based on measured distances
lower_bound
=
[
-
max_dist
,
# x_B lower bound
-
max
_dist
,
# y_B lower bound
-
max_dist
,
# x_C lower bound
-
max
_dist
,
# y_C lower bound
-
max_dist
/
2
,
# x_B lower bound
min
_dist
/
2
,
# y_B lower bound
(above y_C)
-
max_dist
/
2
,
# x_C lower bound
min
_dist
/
4
,
# y_C lower bound
min_dist
/
2
# y_A lower bound
]
upper_bound
=
[
max_dist
*
1.5
,
# x_B upper bound
max_dist
*
1.5
,
# y_B upper bound
max_dist
*
1.5
,
# x_C upper bound
max_dist
*
1.5
,
# y_C upper bound
max_dist
*
1.
2
5
,
# y_C upper bound
max_dist
*
1.5
# y_A upper bound
]
return
lower_bound
,
upper_bound
# Define the error function
# Generate the initial guess and bounds
initial_guess
=
generate_initial_guess
(
measured_distances_noisy
)
lower_bounds
,
upper_bounds
=
generate_bounds
(
measured_distances_noisy
)
# Define the error function with constraint y_B > y_C
def
error_function
(
variables
,
measured
):
x_B
,
y_B
,
x_C
,
y_C
,
y_A
=
variables
# Map measured distances to a, e, d, b, f, c
a_measured
,
e_measured
,
d_measured
,
b_measured
,
f_measured
,
c_measured
=
measured
# Map measured distances to a,b,c,d,e,f
a_measured
=
measured
[
0
]
e_measured
=
measured
[
1
]
d_measured
=
measured
[
2
]
b_measured
=
measured
[
3
]
f_measured
=
measured
[
4
]
c_measured
=
measured
[
5
]
# Compute each distance
a_calc
=
np
.
sqrt
((
x_B
-
0
)
**
2
+
(
y_B
-
y_A
)
**
2
)
# A-B
...
...
@@ -67,19 +76,13 @@ def error_function(variables, measured):
r_e
=
e_calc
-
e_measured
r_f
=
f_calc
-
f_measured
# Add a smoother penalty if y_B <= y_C
penalty
=
1e3
*
max
(
0
,
y_C
-
y_B
+
10
)
# Soft penalty to enforce constraint
# Add a penalty if y_B <= y_C
penalty
=
0
if
y_B
<=
y_C
:
penalty
=
1e6
*
(
y_C
-
y_B
+
1
)
# Large penalty to enforce constraint
return
[
r_a
,
r_b
,
r_c
,
r_d
,
r_e
,
r_f
,
penalty
]
# Generate the initial guess and bounds
initial_guess
=
generate_initial_guess
(
measured_distances_noisy
)
lower_bounds
,
upper_bounds
=
generate_bounds
(
measured_distances_noisy
)
print
(
"
Lower bounds:
"
,
lower_bounds
)
print
(
"
Upper bounds:
"
,
upper_bounds
)
print
(
"
Initial guess:
"
,
initial_guess
)
# Run least squares optimization
result_noisy
=
least_squares
(
error_function
,
...
...
@@ -89,11 +92,8 @@ result_noisy = least_squares(
loss
=
'
soft_l1
'
)
# Extract optimized coordinates
optimized_coords_noisy
=
result_noisy
.
x
x_B
,
y_B
,
x_C
,
y_C
,
y_A
=
optimized_coords_noisy
print
(
"
Optimized coordinates with noise:
"
,
optimized_coords_noisy
)
# Calculate and print residuals
residuals_noisy
=
error_function
(
optimized_coords_noisy
,
measured_distances_noisy
)[:
-
1
]
# Ignore penalty
print
(
"
\n
Residuals with noisy measurements:
"
,
residuals_noisy
)
residuals_noisy
=
error_function
(
optimized_coords_noisy
,
measured_distances_noisy
)[:
-
1
]
# Ignore penalty in residuals
print
(
"
Residuals with noisy measurements:
"
,
residuals_noisy
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment