launch_core.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 ###
4 # Eventually this should do something smarter, like launch sonars
5 # 3DTOF, etc, if they are installed.
6 ###
7 
8 import sys, os, subprocess, time
9 import roslaunch
10 import yaml
11 import smbus # used for the hw rev stuff
12 
13 conf_path = "/etc/ubiquity/robot.yaml"
14 
15 default_conf = \
16 {
17  'raspicam' : {'position' : 'forward'},
18  'sonars' : 'None',
19  'motor_controller' : {
20  'board_version' : None,
21  'serial_port': "/dev/ttyAMA0",
22  'serial_baud': 38400,
23  'pid_proportional': 5000,
24  'pid_integral': 7,
25  'pid_derivative': -110,
26  'pid_denominator': 1000,
27  'pid_moving_buffer_size': 70,
28  'pid_velocity': 1500
29  },
30  'force_time_sync' : 'True',
31  'oled_display': {
32  'controller': None
33  }
34 }
35 
36 def get_conf():
37  try:
38  with open(conf_path) as conf_file:
39  try:
40  conf = yaml.safe_load(conf_file)
41  except Exception as e:
42  print('Error reading yaml file, using default configuration')
43  print(e)
44  return default_conf
45 
46  if (conf is None):
47  print('WARN /etc/ubiquity/robot.yaml is empty, using default configuration')
48  return default_conf
49 
50  for key, value in default_conf.items():
51  if key not in conf:
52  conf[key] = value
53 
54  return conf
55  except IOError:
56  print("WARN /etc/ubiquity/robot.yaml doesn't exist, using default configuration")
57  return default_conf
58  except yaml.parser.ParserError:
59  print("WARN failed to parse /etc/ubiquity/robot.yaml, using default configuration")
60  return default_conf
61 
62 if __name__ == "__main__":
63  conf = get_conf()
64 
65  # We only support 1 version of the Sonars right now
66  if conf['sonars'] == 'pi_sonar_v1':
67  conf['sonars'] = 'true'
68  else:
69  conf['sonars'] = 'false'
70 
71  # We only support 1 display type right now
72  oled_display_installed = 'false'
73  if conf['oled_display']['controller'] == 'SH1106':
74  oled_display_installed = 'true'
75 
76  print conf
77 
78  if conf['force_time_sync']:
79  time.sleep(5) # pifi doesn't like being called early in boot
80  try:
81  timeout = time.time() + 40 # up to 40 seconds
82  while (1):
83  if (time.time() > timeout):
84  print "Timed out"
85  raise RuntimeError # go to error handling
86  output = subprocess.check_output(["pifi", "status"])
87  if "not activated" in output:
88  time.sleep(5)
89  if "acting as an Access Point" in output:
90  print "we are in AP mode, don't wait for time"
91  break # dont bother with chrony in AP mode
92  if "is connected to" in output:
93  print "we are connected to a network, wait for time"
94  subprocess.call(["chronyc", "waitsync", "20"]) # Wait for chrony sync
95  break
96  except (RuntimeError, OSError, subprocess.CalledProcessError) as e:
97  print "Error calling pifi"
98  subprocess.call(["chronyc", "waitsync", "6"]) # Wait up to 60 seconds for chrony
99  else:
100  print "Skipping time sync steps due to configuration"
101 
102  boardRev = 0
103 
104  if conf['motor_controller']['board_version'] == None:
105  # Code to read board version from I2C
106  # The I2C chip is only present on 5.0 and newer boards
107  try:
108  i2cbus = smbus.SMBus(1)
109  i2cbus.write_byte(0x20, 0xFF)
110  time.sleep(0.2)
111  inputPortBits = i2cbus.read_byte(0x20)
112  boardRev = 49 + (15 - (inputPortBits & 0x0F))
113  print "Got board rev: %d" % boardRev
114  except:
115  print "Error reading motor controller board version from i2c"
116 
117  extrinsics_file = '~/.ros/camera_info/extrinsics_%s.yaml' % conf['raspicam']['position']
118  extrinsics_file = os.path.expanduser(extrinsics_file)
119  if not os.path.isfile(extrinsics_file):
120  extrinsics_file = '-'
121 
122  controller = conf['motor_controller'] # make it easier to access elements
123  # Ugly, but works
124  # just passing argv doesn't work with launch arguments, so we assign sys.argv
125  sys.argv = ["roslaunch", "magni_bringup", "core.launch",
126  "raspicam_mount:=%s" % conf['raspicam']['position'],
127  "sonars_installed:=%s" % conf['sonars'],
128  "controller_board_version:=%d" % boardRev,
129  "camera_extrinsics_file:=%s" % extrinsics_file,
130  "controller_serial_port:=%s" % controller['serial_port'],
131  "controller_serial_baud:=%s" % controller['serial_baud'],
132  "controller_pid_proportional:=%s" % controller['pid_proportional'],
133  "controller_pid_integral:=%s" % controller['pid_integral'],
134  "controller_pid_derivative:=%s" % controller['pid_derivative'],
135  "controller_pid_denominator:=%s" % controller['pid_denominator'],
136  "controller_pid_moving_buffer_size:=%s" % controller['pid_moving_buffer_size'],
137  "controller_pid_velocity:=%s" % controller['pid_velocity'],
138  "oled_display:=%s" % oled_display_installed
139  ]
140 
141  roslaunch.main(sys.argv)
142 
def get_conf()
Definition: launch_core.py:36


magni_bringup
Author(s):
autogenerated on Tue Jun 1 2021 02:33:35