Created
September 2, 2023 20:39
-
-
Save gentijo/f751eb1b524de8744581fd2efaf7414b to your computer and use it in GitHub Desktop.
Micropython variable sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This version works, notice where ros_event_callbasck is declasred | |
# | |
from microros import readROSMsg, publishMsg, registerEventSubscription, init_event_queue | |
import machine | |
import utime | |
import _thread | |
import gc | |
print("\r\nInit Event Queue") | |
init_event_queue() | |
# | |
# Thread code | |
# | |
def ros_event_thread(): | |
# | |
# THis works | |
# | |
def ros_event_callback(data): | |
print("Ros did something") | |
#print(data) | |
print("\r\nRegistgering Event Subscription") | |
registerEventSubscription("CmdVel", "Twist", ros_event_callback) | |
while True: | |
print("In event Thread") | |
utime.sleep(2) | |
readROSMsg() | |
# Function that initializ | |
_thread.start_new_thread(ros_event_thread, ()) | |
while True: | |
print("In Main Thread") | |
utime.sleep(2) | |
# | |
# This does not work, notice that where the ros_event_callback is declared, it is | |
# crated on the MP main THread but we are trying to access in the event thread. | |
# | |
from microros import readROSMsg, publishMsg, registerEventSubscription, init_event_queue | |
import machine | |
import utime | |
import _thread | |
import gc | |
print("\r\nInit Event Queue") | |
init_event_queue() | |
#### | |
def ros_event_callback(data): | |
print("Ros did something") | |
#print(data) | |
#### | |
def ros_event_thread(): | |
print("\r\nRegistgering Event Subscription") | |
registerEventSubscription("CmdVel", "Twist", ros_event_callback) | |
while True: | |
print("In event Thread") | |
utime.sleep(2) | |
readROSMsg() | |
# Function that initializ | |
_thread.start_new_thread(ros_event_thread, ()) | |
while True: | |
print("In Main Thread") | |
utime.sleep(2) | |
# | |
# Shows how call back is registered in the C Module | |
# | |
mp_obj_t g_eventName = NULL; | |
mp_obj_t g_eventType = NULL; | |
mp_obj_t g_eventCallback = NULL; | |
mp_obj_t registerEventSubscription(mp_obj_t eventName, mp_obj_t eventType, mp_obj_t eventCallback) { | |
g_eventName = eventName; | |
g_eventType = eventType; | |
g_eventCallback = eventCallback; | |
return eventName; | |
} | |
# | |
# Poll the Queue for a new event. Note: the queue is configured to transfer an | |
# unsigned long, the ROS event payload informatuon is not being conveyed yet. | |
# A Timer task wiil load unsigned longs into the queue and when received will | |
# call a callback dispatch method, this method will run on the Event thread and | |
not the MP main thread. | |
mp_obj_t readROSMsg() { | |
const TickType_t xDelay = 100 / portTICK_PERIOD_MS; | |
unsigned long value; | |
if (xQueueReceive(mp_uros_queue, &value, 0 )== pdTRUE) { | |
if (g_eventCallback != NULL) { | |
mp_call_function_1(g_eventCallback, NULL); | |
} | |
} | |
return NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment