Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """Manually send commands to the RC car."""
  3. import argparse
  4. import socket
  5. import sys
  6.  
  7. from common import dead_frequency
  8. from common import format_command
  9. from common import server_up
  10.  
  11. # pylint: disable=superfluous-parens
  12.  
  13.  
  14. def input_function(type_cast):
  15. """Returns the input function for the running version of Python for reading
  16. data from stdin.
  17. """
  18. # pylint: disable=bad-builtin
  19. if sys.version_info.major == 2:
  20. return lambda message: type_cast(raw_input(message))
  21. else:
  22. return lambda message: type_cast(input(message))
  23.  
  24.  
  25. def get_command_array(parser):
  26. """Returns an array of command information that can be used in the
  27. format_command function.
  28. """
  29. args = parser.parse_args()
  30.  
  31. read_float = input_function(float)
  32. read_int = input_function(int)
  33.  
  34. option_to_prompt_and_function = {
  35. 'frequency': ('Command frequency? ', read_float),
  36. 'microseconds': ('Microseconds? ', read_int),
  37. 'sync_multiplier': ('Synchronization multiplier? ', read_int),
  38. 'sync_repeats': ('Synchronization repeats? ', read_int),
  39. }
  40.  
  41. for option, prompt_and_function in option_to_prompt_and_function.items():
  42. if getattr(args, option) is None:
  43. prompt, function = prompt_and_function
  44. setattr(args, option, function(prompt))
  45.  
  46. return [
  47. float(args.frequency),
  48. int(args.microseconds),
  49. int(args.sync_multiplier),
  50. int(args.sync_repeats),
  51. 0, # Signal repeats, to be read in and configured later
  52. ]
  53.  
  54.  
  55. def make_parser():
  56. """Builds and returns an argument parser."""
  57. parser = argparse.ArgumentParser(
  58. description='Sends burst commands to Raspberry Pi RC.'
  59. )
  60.  
  61. parser.add_argument(
  62. '-p',
  63. '--port',
  64. dest='port',
  65. help='The port to send control commands to.',
  66. default=12345,
  67. type=int
  68. )
  69. parser.add_argument(
  70. '-s',
  71. '--server',
  72. dest='server',
  73. help='The server to send control commands to.',
  74. default='127.1'
  75. )
  76.  
  77. parser.add_argument(
  78. '-f',
  79. '--frequency',
  80. dest='frequency',
  81. help='The frequency to broadcast commands on.'
  82. )
  83. parser.add_argument(
  84. '-u',
  85. '--microseconds',
  86. dest='microseconds',
  87. help='The interval in microseconds for the commands.'
  88. )
  89. parser.add_argument(
  90. '--sync-multiplier',
  91. dest='sync_multiplier',
  92. help='The multiplier of the interval microseconds for the'
  93. ' synchronization burst. For example, if the microseconds is 100'
  94. ' and the multiplier is 3, each synchronization burst will have'
  95. ' 300 us of signal and 100 us of dead signal.'
  96. )
  97. parser.add_argument(
  98. '--sync-repeats',
  99. dest='sync_repeats',
  100. help='The number of times to repeat the synchronization bursts.'
  101. )
  102.  
  103. return parser
  104.  
  105.  
  106. def send_signal_repeats(host, port, command_array):
  107. """Reads signal repeat bursts and sends commands to the Raspberry Pi."""
  108. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  109.  
  110. read_int = input_function(int)
  111.  
  112. while True:
  113. try:
  114. command_array[4] = read_int('Signal repeats? ')
  115. except ValueError:
  116. pass
  117. # pylint: disable=star-args
  118. command = format_command(*command_array)
  119. if sys.version_info.major == 3:
  120. command = bytes(command, 'utf-8')
  121. sock.sendto(command, (host, port))
  122.  
  123.  
  124. def main():
  125. """Parses command line arguments and runs the simple controller."""
  126. parser = make_parser()
  127. args = parser.parse_args()
  128.  
  129. if args.frequency is not None:
  130. frequency = dead_frequency(args.frequency)
  131. else:
  132. frequency = 49.830
  133.  
  134. if not server_up(args.server, args.port, frequency):
  135. print('Server does not appear to be listening for messages, aborting')
  136. return
  137.  
  138. command_array = get_command_array(parser)
  139.  
  140. print('Sending commands to ' + args.server + ':' + str(args.port))
  141. send_signal_repeats(args.server, args.port, command_array)
  142.  
  143.  
  144. if __name__ == '__main__':
  145. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement