This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepper_motor.py
59 lines (48 loc) · 1.79 KB
/
stepper_motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from science_servers.stepper_motor_server import StepperMotorServer
from science_servers.stepper_motor_cancel import StepperMotorCancel
from rclpy.executors import MultiThreadedExecutor
from threading import Thread
import os
import subprocess
import rclpy
class StepperMotor(object):
def __init__(self):
self.stepper_motor_server = StepperMotorServer()
self.stepper_motor_cancel = StepperMotorCancel()
self.stepper_executor = MultiThreadedExecutor()
self.stepper_executor.add_node(self.stepper_motor_server)
self.stepper_executor.add_node(self.stepper_motor_cancel)
self.stepper_thread = Thread(target=self.stepper_executor.spin, daemon=True)
self.update_thread = Thread(target=self.check_for_cancel, daemon=True)
self.stepper_thread.start()
self.update_thread.start()
def check_for_cancel(self):
while True:
if self.stepper_motor_cancel.cancel:
self.stepper_motor_server.cancel_movement()
self.stepper_motor_cancel.cancel = False
def close(self):
self.stepper_executor.shutdown
self.stepper_motor_server.destroy_node()
self.stepper_motor_cancel.destroy_node()
self.stepper_thread.join()
self.update_thread.join()
def main():
rclpy.init()
stepper_motor_controller = StepperMotor()
state = True
try:
while rclpy.ok():
state = not state
except Exception as e:
stepper_motor_controller.stepper_motor_server.get_logger().info(f'{e}')
finally:
stepper_motor_controller.close()
rclpy.shutdown()
def sudo(args=None):
subprocess.call(['sudo', 'python3', '-E', 'stepper_motor_controller.py'])
if __name__ == '__main__':
if os.geteuid() != 0:
sudo()
else:
main()