Skip to content

Commit

Permalink
Move polynomial_evaluator outside #16
Browse files Browse the repository at this point in the history
  • Loading branch information
RexBerry committed Oct 21, 2022
1 parent 8582f59 commit e7e6afa
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions flight/avoidance/obstacle_avoidance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
from velocity import Velocity


def polynomial_evaluator(coefficients: list[float]) -> Callable[[float], float]:
"""
Converts a list of polynomial coefficients into a function
Parameters
----------
coefficients : list[float]
A list of coefficients for a polynomial - highest degree first
Returns
-------
evaluate : Callable[[float], float]
A function that evaluates the polynomial at any given value
"""

def evaluate(val: float) -> float:
result: float = 0.0
coefficient: float
for coefficient in coefficients:
result *= val
result += coefficient
return result

return evaluate


async def calculate_avoidance_path(
drone: mavsdk.System,
obstacle_data: list[InputPoint],
Expand Down Expand Up @@ -106,17 +132,6 @@ async def calculate_avoidance_path(
)
)

def polynomial_evaluator(coefficients: list[float]) -> Callable[[float], float]:
def evaluate(val: float) -> float:
result: float = 0.0
coefficient: float
for coefficient in coefficients:
result *= val
result += coefficient
return result

return evaluate

predict_x: Callable[[float], float] = polynomial_evaluator(x_polynomial)
predict_y: Callable[[float], float] = polynomial_evaluator(y_polynomial)
predict_altitude: Callable[[float], float] = polynomial_evaluator(altitude_polynomial)
Expand Down

0 comments on commit e7e6afa

Please sign in to comment.