Andy_Y
May 24, 2021 - 14:19
I have a strange behaviour. Sometimes I get an error:
module 'motion' has no attribute 'start_updates'
And I can't see why... If I try the "Motion Plot.py" example and than my sources, it works.
I did an thread for the getting the motion movement:
# -*- coding: utf-8 -*-
from threading import Thread
import motion
from time import sleep
class SteeringThread(Thread):
def __init__(self, label = None):
# Init the thread
Thread.__init__(self)
# Set the name of this thread
self.setName('SteeringThread')
# Set the running flag of this thread
self.__running = True
# Motion updates
motion.start_updates()
sleep(0.5)
# Get the first gravity value
self.__gravity = motion.get_gravity()
def terminate(self):
"""
Terminate the thread itself.
"""
self.__running = False
motion.stop_updates()
@property
def gravity(self):
"""
Returns the actual gravity value.
"""
return self.__gravity
def run(self):
"""
Run this thread.
"""
# Run
while self.__running:
print('X: %f | Y: %f | Z: %f' % (self.__gravity[0], self.__gravity[1], self.__gravity[2]))
# Get the gravity values
self.__gravity = motion.get_gravity()
# Timing of the loop
sleep(.8)
And it will used in:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from steering import SteeringThread
import time
def main():
try:
# Create the steering task
steering = SteeringThread()
steering.start()
time.sleep(20.0)
steering.terminate()
steering.join()
except Exception as e:
print(e)
finally:
exit()
if __name__ == '__main__':
main()
That is all...