# Sending messages to pcc from sandbox

**URL:** <https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311>\
**Category:** AVR Drone Software\
**Created:** [October 31, 2022, 6:30pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311 "2022-10-31T18:30:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [October 31, 2022, 6:30pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/1 "2022-10-31T18:30:04Z")

</div>

Hey guys! We are progressing fast through the autonomous code and stumbled across another issue. Here’s the code:

def on\_autonomous\_enable(self, payload: AvrAutonomousEnablePayload):  
did\_message\_recieve = payload[“enabled”]  
logger.debug(f"visible tag: {self.visible\_tag}“)  
logger.debug(f"recieved auton enable: {did\_message\_recieve}”)  
start = time.time ()  
finish\_1 = start + 4  
finish\_2 = start + 5  
finish\_3 = start + 6  
# Check if there is a visible april tag, if the vehicle is within specified horizontal tolerance, and if the vehicle has not already dropped the water  
if self.visible\_tag == 0:  
timing\_message\_1 = True  
self.open\_servo(0) # Open servo on channel 0  
while time.time () \< finish\_1:  
pass  
logger.debug(f"timing\_message\_1: {timing\_message\_1}“)  
self.blink\_leds(0.5) # Blink LEDs 1 times at 0.5 second interval  
while time.time () \< finish\_2:  
pass  
self.blink\_leds(0.5) # Blink LEDs 1 times at 0.5 second interval  
while time.time () \< finish\_3:  
pass  
self.blink\_leds(0.5) # Blink LEDs 1 times at 0.5 second interval  
self.close\_servo(0)  
self.has\_dropped = True  
logger.debug(f"self.has\_dropped: {self.has\_dropped}”)

Our problem is that the PCC doesn’t receive messages until the function has run. For example, it will run through the code, spit out the logger messages, go through the delays, and finally spit out the self.has\_dropped result. Only then does it send the messages to open and close the servo, as well as the LEDs. This causes it to do it all at once, bypassing out timing and screwing everything up. We have them sitting in another function, could that be the problem? Or do we have to use the serial messages shown in [pypi.org](http://pypi.org)? Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [October 31, 2022, 6:30pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/2 "2022-10-31T18:30:43Z")

</div>

@nathan If you know anything about this that would be great!

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.theavr.org/nathan/32/4_2.png) [@nathan](https://forum.theavr.org/u/nathan)\
**Post date:** [October 31, 2022, 9:45pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/3 "2022-10-31T21:45:30Z")

</div>

Okay, so what I would recommend is to use MQTT messages to control the PCC, rather than a serial connection. I’m pretty certain the PCM module will block the connection to the serial device to some extent, which is likely causing your timing issues. The architecture of the software stack as a whole is such that each module is sort of a hardware-to-MQTT translator, so I highly recommend sticking with 100% MQTT communications.

Unrelated, your:

```python
while time.time () < finish_1:
    pass

```

loops, can easily be replaced with `time.sleep(number)` statements instead, where `number` is the number of seconds to wait.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [October 31, 2022, 11:01pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/4 "2022-10-31T23:01:16Z")

</div>

We tried time.sleep() and it stopped all code on the jetson, causing it to stop and it caused the GUI to crash. Also, we are using MQTT messages already, right? If so what is causing out problem?

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [October 31, 2022, 11:02pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/5 "2022-10-31T23:02:21Z")

</div>

Also here is link to all of our code. [AVR-2022/sandbox.py at main · BHS-drone-team/AVR-2022 · GitHub](https://github.com/BHS-drone-team/AVR-2022/blob/main/VMC/sandbox/sandbox.py)

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.theavr.org/nathan/32/4_2.png) [@nathan](https://forum.theavr.org/u/nathan)\
**Post date:** [November 1, 2022, 12:06am UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/6 "2022-11-01T00:06:44Z")

</div>

Thanks for the link that helps a ton. I am going to try and make time to sit down after work tomorrow and see what I can figure out.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [November 1, 2022, 12:34pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/7 "2022-11-01T12:34:47Z")

</div>

Thanks so much for the help!

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [November 1, 2022, 12:44pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/8 "2022-11-01T12:44:11Z")

</div>

Something that I just thought about trying would be to put the send message thingy to the PCC inside of the main function instead of separating it into different, external functions.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [November 1, 2022, 4:22pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/9 "2022-11-01T16:22:00Z")

</div>

I tried to combine the functions but it did not do anything different.

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.theavr.org/nathan/32/4_2.png) [@nathan](https://forum.theavr.org/u/nathan)\
**Post date:** [November 1, 2022, 10:49pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/10 "2022-11-01T22:49:30Z")

</div>

Some good news, I can replicate the problem, just running your Sandbox code on my desktop, without any PCC or Jetson, so I think this is a pure Python programming issue. I’ll reply back once I figure something out.

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.theavr.org/nathan/32/4_2.png) [@nathan](https://forum.theavr.org/u/nathan)\
**Post date:** [November 1, 2022, 11:50pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/11 "2022-11-01T23:50:08Z")

</div>

I found the problem and have a solution.

Short explanation: It’s a bug in the `bell-avr-libraries` package that prevented MQTT messages from being sent until after a callback function was complete

Long explanation: With how `paho-mqtt` works, it holds onto a mutex (mutual exclusion) during callbacks to prevent multiple MQTT events from happening at once and getting lost. What this meant, was the sending messages were getting queued until the function returned, and then they’d be sent all at once. We’ve never run into this before, since we never put `time.sleep` in our MQTT callbacks. Thankfully, they expose a way to pre-emptively send messages, if you’re using the `loop_forever()` function, which the Sandbox does.

This PR should fix the issue once it has been merged and published:

> <https://github.com/bellflight/AVR-Python-Libraries/pull/27>
>
> See code comments and forum post

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.theavr.org/nathan/32/4_2.png) [@nathan](https://forum.theavr.org/u/nathan)\
**Post date:** [November 2, 2022, 1:55am UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/12 "2022-11-02T01:55:26Z")

</div>

Version 0.1.10 has been released which addresses this: [bell-avr-libraries · PyPI](https://pypi.org/project/bell-avr-libraries/0.1.10/)

Update your requirements.txt file, rebuild, and let us know if that works.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [November 2, 2022, 12:13pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/13 "2022-11-02T12:13:19Z")

</div>

Thank you very much, I really appreciate it! I will try this soon.

---

<div class="post-metadata">

**Author:** ![Jackson](https://avatars.discourse-cdn.com/v4/letter/j/e68b1a/32.png) [@Jackson](https://forum.theavr.org/u/Jackson)\
**Post date:** [November 2, 2022, 1:38pm UTC](https://forum.theavr.org/t/sending-messages-to-pcc-from-sandbox/311/14 "2022-11-02T13:38:53Z")

</div>

THIS WORKED! Thanks for the help!
