Created
November 21, 2020 15:57
-
-
Save dave-malone/b723f3e75a656c61e66b81a7e297b10c to your computer and use it in GitHub Desktop.
cardboard_cruiser.py
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
import time | |
import logging | |
import threading | |
import atexit | |
from adafruit_crickit import crickit | |
format = "%(asctime)s: %(message)s" | |
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") | |
# make two variables for the motors to make code shorter to type | |
motor_1 = crickit.dc_motor_1 | |
motor_2 = crickit.dc_motor_2 | |
motor_1_throttle_speed = 0.75 | |
motor_2_throttle_speed = motor_1_throttle_speed | |
def motor_1_demo(): | |
while True: | |
motor_1.throttle = motor_1_throttle_speed | |
time.sleep(0.5) | |
def motor_2_demo(): | |
while True: | |
motor_2.throttle = motor_2_throttle_speed | |
time.sleep(0.5) | |
def shutdown_motors(): | |
logging.info("Shutting down motor_1") | |
motor_1.throttle = 0 | |
logging.info("Shutting down motor_2") | |
motor_2.throttle = 0 | |
atexit.register(shutdown_motors) | |
if __name__ == "__main__": | |
threads = list() | |
logging.info("Main : create and start motor_1_demo thread ") | |
thread_1 = threading.Thread(target=motor_1_demo) | |
threads.append(0) | |
thread_1.start() | |
logging.info("Main : create and start motor_2_demo thread ") | |
thread_2 = threading.Thread(target=motor_2_demo) | |
threads.append(1) | |
thread_2.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment