set_urg_ip.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2014 Unbounded Robotics Inc. 
00004 # All right reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #
00009 #   * Redistributions of source code must retain the above copyright
00010 #     notice, this list of conditions and the following disclaimer.
00011 #   * Redistributions in binary form must reproduce the above copyright
00012 #     notice, this list of conditions and the following disclaimer in the
00013 #     documentation and/or other materials provided with the distribution.
00014 #   * Neither the name of Unbounded Robotics Inc. nor the names of its 
00015 #     contributors may be used to endorse or promote products derived 
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00019 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 # DISCLAIMED. IN NO EVENT SHALL UNBOUNDED ROBOTICS INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00026 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00029 """
00030 Change the IP address of a Hokuyo URG Laser
00031 """
00032 
00033 import argparse
00034 import socket
00035 
00036 def parse_and_validate_ipv4(argument, name):
00037     """
00038     Each address must have 4 
00039     """
00040     if len(argument.split(".")) != 4:
00041         print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
00042         exit(-1)
00043     parsed = ""
00044     for x in argument.split("."):
00045         if len(x) > 3:
00046             print("Invalid %s, must be of the form xxx.yyy.zzz.www" % name)
00047             exit(-1)
00048         while len(x) < 3:
00049             x = "0" + x
00050         parsed += x
00051     return parsed
00052 
00053 if __name__ == "__main__":
00054     parser = argparse.ArgumentParser(description=__doc__)
00055     parser.add_argument('new_ip', help='The desired IP address for the laser')
00056     parser.add_argument('new_gw', help='The desired gateway for the laser')
00057     parser.add_argument('--nm', help='The desired netmask for the laser', default="255.255.255.0")
00058     parser.add_argument('--ip', help='The current IP address of the laser', default="192.168.0.10")
00059     args = parser.parse_args()
00060 
00061     # Packet starts with $IP, ends with \x0a, contains:
00062     #   12 character IP
00063     #   12 character netmask
00064     #   12 character gateway
00065     ip = parse_and_validate_ipv4(args.new_ip, "IP address")
00066     gw = parse_and_validate_ipv4(args.new_gw, "gateway address")
00067     nm = parse_and_validate_ipv4(args.nm, "netmask")
00068     msg = "$IP" + ip + nm + gw + "\x0a"
00069 
00070     print("Connecting to %s" % args.ip)
00071     sock = socket.socket()
00072     sock.connect((args.ip, 10940))
00073 
00074     print("Updating settings")
00075     sock.send(msg)
00076     try:
00077         sock.settimeout(5)
00078         returned = sock.recv(40)
00079     except socket.timeout:
00080         print("Laser did not return any packet, is probably not updated.")
00081         exit(-1)
00082     if msg != returned:
00083         print("Laser does not appear to have updated")
00084         exit(-1)
00085     print("Done updating, cycle power on laser")


urg_node
Author(s): Chad Rockey , Mike O'Driscoll
autogenerated on Sat Jun 8 2019 19:16:00