Initial startup script to configure WiFi and MAVProxy
/etc/rc.local is almost the last script called at boot up of Ubuntu. We use this script to do our init.
NOTE: exit 0 is a mandatory statement added in the end.
#!/bin/bash /usr/bin/wireless_card_startup.sh /usr/bin/mav_wrapper.py /etc/mavproxy_config /usr/bin/drone_app_wrapper.py /etc/drone_app_cmd exit 0
There are several startup scripts, one for the wireless card and the other is for mavproxy. The scripts are explained as follows:
Wireless Card
The wireless card used is TL-WN7200ND and its plug and play with Ubuntu 12.04 LTS. If you are not using Ubuntu OS, it is recommended that you have the ath9k_htc driver for the wireless card to work.
The script /usr/bin/wireless_card_startup.sh configures the wireless card as follows:
#!/bin/bash ifconfig wlan0 down iwconfig wlan0 mode Ad-Hoc freq 2.412G ap 22:22:22:22:22:22 iwconfig wlan0 essid centMeshAir ifconfig wlan0 152.14.189.16/24
MAVProxy
mavwrapper.py is a wrapper python routine to start MAVProxy with appropriate arguments and also enables MAVProxy to store logs instead of overwriting them. It takes a config file /etc/mavproxy_config as its input.
Config file (/etc/mavproxy_config)
mav_log: 81
Script (/usr/bin/mav_wrapper.py)
#!/usr/bin/python import os import re import sys from tempfile import mkstemp from shutil import move """ This script acts as a wrapper for invoking mavproxy. It performs two key functions: 1) Read the value stored in a configuration file, increment the value by 1 and use that for logging. We need this as the BBBK has no concept of date. 2) Start the mavproxy with a list of IP addresses (read from another file) as --out. """ # This variable is used if any of the file handling functionality fails. So we skip that value when actually using it. error_handle_value = 1357 value_for_logging = 0 aircraft_name = "garuda" """ This function performs the process of incrementing the given value by 1 and returning it. Before that, it performs a check to see if that value is already used. If it is, then the change is made accordingly. """ def val_to_be_used(int_value_from_file): global error_handle_value # TODO: Perform additional checking int_value_from_file += 1 if int_value_from_file == error_handle_value: int_value_from_file += 1 return int_value_from_file """main() function""" def main(): global error_handle_value global value_for_logging global aircraft_name if len(sys.argv) != 2: print len(sys.argv) else: print sys.argv[1] file_handle = "" # Open the file name for reading try: file_handle = open (sys.argv[1],'r') # Read file contents file_contents = file_handle.read() # Get the value corresponding to mav_log search_log_value_in_file = re.search(r'mav_log:\s+(.+)',file_contents) if not search_log_value_in_file: print "No value found, using a predefined value of %d" % error_handle_value if file_handle: file_handle.close() output_file_for_writing = open('/etc/mavproxy_config','w') output_file_for_writing.write ('mav_log: '+str(error_handle_value)) output_file_for_writing.close() else: value_from_file_as_string = search_log_value_in_file.group(1) int_value_from_file = int(value_from_file_as_string) print "Value found: %s" % int_value_from_file # This is the value from the file, calculate the value to be used # after necessary checks value_for_logging = val_to_be_used(int_value_from_file) print "Value after modification is %d " % value_for_logging #Since we have already performed a 'read()' once , rewind file_handle.seek(0) # Write the updated value to the file fh, abs_path = mkstemp() output_file_for_writing = open(abs_path,"w") for line in file_handle: new_line = re.sub(r'mav_log:\s+(.+)','mav_log: '+str(value_for_logging),line) output_file_for_writing.write(new_line) if file_handle: file_handle.close() if output_file_for_writing: output_file_for_writing.close() move(abs_path, sys.argv[1]) except: print "File open exception, using a predefined value of %d" % error_handle_value value_for_logging = error_handle_value if file_handle: file_handle.close() output_file_for_writing = open('/etc/mavproxy_config','w') output_file_for_writing.write ('mav_log: '+str(value_for_logging)) output_file_for_writing.close() # TODO: This is hardcoded for now, change this to read from a file with the list of # IP addresses and ports which will be used as --out=<IP>:<port> aircraft_name_for_logging = aircraft_name + "_" + str(value_for_logging) command_to_be_used_for_mavproxy = 'screen -S garuda -s /bin/bash -d -m /usr/local/bin/mavproxy.py --aircraft="'+aircraft_name_for_logging+'" --master=/dev/ttyACM0 --out=152.14.189.18:14550 --out=152.14.189.15:14550 --out=152.14.189.20:14550 --out=localhost:12345' print command_to_be_used_for_mavproxy # Start mavproxy os.system (command_to_be_used_for_mavproxy) if __name__ == '__main__': main()
DroneAppWrapper
This script is located in /usr/bin/drone_app_wrapper.py
#!/usr/bin/python import os import re import sys """ This script acts as a wrapper for invoking the drone application. It performs two key functions: 1) Read the command of the sample application that needs to be executed 2) Invoke the drone application in a screen session """ # Default location where the command for the drone application is stored. # This is used if no argument is provided to this script. default_location = "/etc/drone_app_cmd" # Default command that will be run in case of any error reading the drone # app command default_cmd = "/usr/bin/application_code/sample_prog_mavutil/program_ss2/mcp_ss2.py 152.14.189.255 7889 7890" """main() function""" def main(): global default_location global default_cmd file_location = None command_to_be_used_for_drone_app = None if len(sys.argv) != 2: print "File name not specified, using default location" file_location = default_location else: print sys.argv[1] file_location = sys.argv[1] file_handle = None # Open the file name for reading try: file_handle = open (file_location,'r') # Read file contents file_contents = file_handle.read() # Get the value corresponding to command search_command_in_file = re.search(r'command:\s+(.+)',file_contents) if not search_command_in_file: print "No command found, using the default command" if file_handle: file_handle.close() command_to_be_used_for_drone_app = default_cmd else: command_to_be_used_for_drone_app = search_command_in_file.group(1) print "Value found: %s" % command_to_be_used_for_drone_app except: print "File open exception, using %s as command" % default_location command_to_be_used_for_drone_app = default_cmd if file_handle: file_handle.close() command_to_run = 'screen -S drone_app -s /bin/bash -d -m '+command_to_be_used_for_drone_app+'' print command_to_run # Start drone_app os.system (command_to_run) if __name__ == '__main__': main()
Drone Application Command
This file is located in /etc/drone_app_cmd
command: /usr/bin/application_code/sample_prog_mavutil/program_ss3/mcp_ss3.py 152.14.189.255 7889 7890