Skip to content

Commit 2a194c5

Browse files
authored
Fixed acceleration divide by zero error (#111)
1 parent 3295c77 commit 2a194c5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pymavswarm/handlers/message_receivers.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ def listener(message: Any, agents: dict[AgentID, Agent]) -> None:
149149

150150
# Calculate the acceleration if there has been more than one velocity
151151
# reading
152-
if prev_velocity.global_frame.timestamp != 0.0:
152+
if (
153+
prev_velocity.global_frame.timestamp != 0.0
154+
and (timestamp - prev_velocity.global_frame.timestamp) > 0.0
155+
):
153156
agents[agent_id].acceleration.global_frame.x = (
154157
agents[agent_id].velocity.global_frame.x
155158
- prev_velocity.global_frame.x
@@ -202,8 +205,11 @@ def listener(message: Any, agents: dict[AgentID, Agent]) -> None:
202205
timestamp = message.time_boot_ms - agents[agent_id].clock_offset.value
203206

204207
# Calculate the acceleration if there has been more than one velocity
205-
# reading
206-
if prev_velocity.global_relative_frame.timestamp != 0.0:
208+
# reading and the time difference is greater than zero
209+
if (
210+
prev_velocity.global_relative_frame.timestamp != 0.0
211+
and (timestamp - prev_velocity.global_relative_frame.timestamp) > 0.0
212+
):
207213
agents[agent_id].acceleration.global_relative_frame.x = (
208214
agents[agent_id].velocity.global_relative_frame.x
209215
- prev_velocity.global_relative_frame.x
@@ -259,7 +265,10 @@ def listener(message: Any, agents: dict[AgentID, Agent]) -> None:
259265

260266
# Calculate the acceleration if there has been more than one velocity
261267
# reading
262-
if prev_velocity.local_frame.timestamp != 0.0:
268+
if (
269+
prev_velocity.local_frame.timestamp != 0.0
270+
and (timestamp - prev_velocity.local_frame.timestamp) > 0.0
271+
):
263272
agents[agent_id].acceleration.local_frame.x = (
264273
agents[agent_id].velocity.local_frame.x
265274
- prev_velocity.local_frame.x

0 commit comments

Comments
 (0)