Skip to content

Commit

Permalink
Record videos
Browse files Browse the repository at this point in the history
  • Loading branch information
bskari committed Sep 17, 2016
1 parent 7bd0c28 commit d588155
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
39 changes: 39 additions & 0 deletions control/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Class to control the RC car."""

import datetime
import math
import queue
import random
Expand All @@ -15,6 +16,19 @@
from messaging.async_producers import CommandForwardProducer


try:
import picamera
except ImportError:
class Dummy(object): # pylint: disable=missing-docstring,too-few-public-methods
def __getattr__(self, attr):
time.sleep(0.01)
return Dummy()
def __call__(self, *args, **kwargs):
time.sleep(0.01)
return Dummy()
picamera = Dummy()


class Command(threading.Thread): # pylint: disable=too-many-instance-attributes
"""Processes telemetry data and controls the RC car."""
VALID_COMMANDS = {'start', 'stop', 'reset', 'calibrate-compass'}
Expand Down Expand Up @@ -51,6 +65,8 @@ def __init__(
self._wake_time = None
self._start_time = None

self._camera = picamera.PiCamera()

# If the car is on the starting line (not started yet)
self._on_starting_line = True

Expand Down Expand Up @@ -93,12 +109,29 @@ def _handle_message(self, command):
return

if command == 'start':
try:
now = datetime.datetime.now()
file_name = '/data/{}-{}-{}_{}:{}:{}.h264'.format(
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second
)
self._camera.start_recording(file_name)
except Exception as exc: # pylint: disable=broad-except
self._logger.warning('Unable to save video: {}'.format(exc))
self.run_course()

elif command == 'stop':
threading.Thread(target=self._stop_recording).start()
self.stop()

elif command == 'reset':
self._on_starting_line = True
self.reset()

elif command == 'calibrate-compass':
self.calibrate_compass(10)

Expand All @@ -118,6 +151,7 @@ def run(self):
"""Run in a thread, controls the RC car."""
error_count = 0
if self._waypoint_generator.done():
threading.Thread(target=self._stop_recording).start()
self._logger.info('All waypoints reached')
return

Expand Down Expand Up @@ -470,3 +504,8 @@ def _unstuck_yourself_iterator(self, seconds):
yield True

yield False

def _stop_recording(self):
"""Stops recording after a little while."""
time.sleep(5)
self._camera.stop_recording()
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ def main():
parser = make_parser()
args = parser.parse_args()

try:
global POPEN
POPEN = subprocess.Popen((
'raspivid', '-o', args.video, '-w', '1024', '-h', '576', '-b', '6000000', '-t', '300000'
))
except Exception:
logging.warning('Unable to save video')
#try:
# global POPEN
# POPEN = subprocess.Popen((
# 'raspivid', '-o', args.video, '-w', '1024', '-h', '576', '-b', '6000000', '-t', '300000'
# ))
#except Exception:
# logging.warning('Unable to save video')

concrete_logger = logging.Logger('sparkfun')
concrete_logger.setLevel(logging.DEBUG)
Expand Down

0 comments on commit d588155

Please sign in to comment.