From f965fd757e67f601962b773aac63ba321cec0560 Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:06:00 +0300 Subject: [PATCH 1/9] Control missile launcher from the terminal --- README.md | 6 +++ turret.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100755 turret.py diff --git a/README.md b/README.md index b3cbf65..5ece32a 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,9 @@ It also requires that you run it as root unless you want to spend an afternoon p * Use arrow keys aim. * Press return to fire. + +## Controlling Missile Launcher from the terminal: + + cd stormLauncher + chmod +x turret.py + sudo ./turret.py <"left" | "right" | "up" | "down"> diff --git a/turret.py b/turret.py new file mode 100755 index 0000000..66e4708 --- /dev/null +++ b/turret.py @@ -0,0 +1,110 @@ +#!/usr/bin/python + +import os +import sys +import time +import pygame +import usb.core + +wavFile = "warcry.wav" + +cmdargs = [] +currentTime = 0 + +class launchControl(): + def __init__(self): + self.dev = usb.core.find(idVendor=0x2123, idProduct=0x1010) + if self.dev is None: + raise ValueError('Launcher not found.') + if self.dev.is_kernel_driver_active(0) is True: + self.dev.detach_kernel_driver(0) + self.dev.set_configuration() + + def loopMovement(self, movement): + + while (True): + if (time.time() - currentTime) > movement: + self.turretStop() + break + + def turretUp(self, movement): + print("Turret Up.") + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + self.loopMovement(movement) + + def turretDown(self, movement = 1000): + print("Turret Down.") + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + self.loopMovement(movement) + + def turretLeft(self, movement): + print("Turret Left.") + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + self.loopMovement(movement) + + def turretRight(self, movement): + print("Turret Right.") + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + self.loopMovement(movement) + + def turretStop(self): + print("Turret Stop.") + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + def turretFire(self): + self.message1.set("FIRE!") + + if os.path.isfile(wavFile): + if self.hasSound == True: + pygame.init() + sound = pygame.mixer.Sound("warcry.wav") + sound.play() + time.sleep(3) + + self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) + + def setSound(self, soundOn): + self.hasSound = soundOn + +if __name__ == '__main__': + + if not os.geteuid() == 0: + sys.exit("Script must be run as root.") + + # Get the total number of args passed to the turret.py + total = len(sys.argv) + + # Get the arguments list + cmdargs = sys.argv + + if len(sys.argv) < 3: + print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") + sys.exit() + + command = str.lower(cmdargs[1]) + + if len(sys.argv) == 3: + movement = int(cmdargs[2]) / 1000.0 + + launchControl().setSound(True) + + currentTime = time.time(); + + if command == "left": + launchControl().turretLeft(movement) + + if command == "right": + launchControl().turretRight(movement) + + if command == "up": + launchControl().turretUp(movement) + + if command == "down": + launchControl().turretDown(movement) + + if command == "fire": + launchControl().turretFire() From 997d2b715ec1da487f154257b098f8dc3f232b46 Mon Sep 17 00:00:00 2001 From: kennyhei Date: Tue, 27 Aug 2013 15:07:45 +0300 Subject: [PATCH 2/9] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5ece32a..3a6fd86 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,6 @@ It also requires that you run it as root unless you want to spend an afternoon p ## Controlling Missile Launcher from the terminal: - cd stormLauncher - chmod +x turret.py - sudo ./turret.py <"left" | "right" | "up" | "down"> + cd stormLauncher + chmod +x turret.py + sudo ./turret.py <"left" | "right" | "up" | "down"> From 2d2066b4052c498177d98672cf90911131c44a6e Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:11:38 +0300 Subject: [PATCH 3/9] Refactor --- turret.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/turret.py b/turret.py index 66e4708..b8b5435 100755 --- a/turret.py +++ b/turret.py @@ -86,12 +86,8 @@ def setSound(self, soundOn): sys.exit() command = str.lower(cmdargs[1]) - - if len(sys.argv) == 3: - movement = int(cmdargs[2]) / 1000.0 - + movement = int(cmdargs[2]) / 1000.0 launchControl().setSound(True) - currentTime = time.time(); if command == "left": From 087f2b7b49309cce698e0412eeefcc7971ea10d6 Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:20:40 +0300 Subject: [PATCH 4/9] Fixes --- turret.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/turret.py b/turret.py index b8b5435..907c144 100755 --- a/turret.py +++ b/turret.py @@ -19,9 +19,10 @@ def __init__(self): if self.dev.is_kernel_driver_active(0) is True: self.dev.detach_kernel_driver(0) self.dev.set_configuration() + + self.hasSound = False def loopMovement(self, movement): - while (True): if (time.time() - currentTime) > movement: self.turretStop() @@ -56,7 +57,7 @@ def turretStop(self): self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) def turretFire(self): - self.message1.set("FIRE!") + print("FIRE!") if os.path.isfile(wavFile): if self.hasSound == True: @@ -81,7 +82,10 @@ def setSound(self, soundOn): # Get the arguments list cmdargs = sys.argv - if len(sys.argv) < 3: + if total == 2 and str.lower(cmdargs[1]) == "fire": + launchControl().turretFire() + + if total < 3: print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") sys.exit() @@ -101,6 +105,3 @@ def setSound(self, soundOn): if command == "down": launchControl().turretDown(movement) - - if command == "fire": - launchControl().turretFire() From 7c8db99fd3547942b600c92f5e9c0f914ccdac17 Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:30:02 +0300 Subject: [PATCH 5/9] Fix --- turret.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/turret.py b/turret.py index 907c144..8c4017d 100755 --- a/turret.py +++ b/turret.py @@ -84,24 +84,25 @@ def setSound(self, soundOn): if total == 2 and str.lower(cmdargs[1]) == "fire": launchControl().turretFire() + else: + + if total < 3: + print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") + sys.exit() + + command = str.lower(cmdargs[1]) + movement = int(cmdargs[2]) / 1000.0 + launchControl().setSound(True) + currentTime = time.time(); + + if command == "left": + launchControl().turretLeft(movement) + + if command == "right": + launchControl().turretRight(movement) + + if command == "up": + launchControl().turretUp(movement) - if total < 3: - print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") - sys.exit() - - command = str.lower(cmdargs[1]) - movement = int(cmdargs[2]) / 1000.0 - launchControl().setSound(True) - currentTime = time.time(); - - if command == "left": - launchControl().turretLeft(movement) - - if command == "right": - launchControl().turretRight(movement) - - if command == "up": - launchControl().turretUp(movement) - - if command == "down": - launchControl().turretDown(movement) + if command == "down": + launchControl().turretDown(movement) From 12209b135ad47a96d03a5435b3238b44541a2ad3 Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:46:26 +0300 Subject: [PATCH 6/9] Refactor --- turret.py | 61 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/turret.py b/turret.py index 8c4017d..a609029 100755 --- a/turret.py +++ b/turret.py @@ -9,7 +9,7 @@ wavFile = "warcry.wav" cmdargs = [] -currentTime = 0 +initialTime = 0 class launchControl(): def __init__(self): @@ -24,7 +24,7 @@ def __init__(self): def loopMovement(self, movement): while (True): - if (time.time() - currentTime) > movement: + if (time.time() - initialTime) > movement: self.turretStop() break @@ -71,38 +71,45 @@ def turretFire(self): def setSound(self, soundOn): self.hasSound = soundOn -if __name__ == '__main__': - if not os.geteuid() == 0: - sys.exit("Script must be run as root.") - +def commandControl(cmdargs): + # Get the total number of args passed to the turret.py - total = len(sys.argv) - - # Get the arguments list - cmdargs = sys.argv + total = len(cmdargs) if total == 2 and str.lower(cmdargs[1]) == "fire": launchControl().turretFire() - else: - - if total < 3: - print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") - sys.exit() + return + + if total < 3: + print("sudo ./turret.py <\"left\" | \"right\" | \"up\" | \"down\"> ") + return - command = str.lower(cmdargs[1]) - movement = int(cmdargs[2]) / 1000.0 - launchControl().setSound(True) - currentTime = time.time(); + command = str.lower(cmdargs[1]) + movement = int(cmdargs[2]) / 1000.0 + launchControl().setSound(True) + + global initialTime + initialTime = time.time(); - if command == "left": - launchControl().turretLeft(movement) + if command == "left": + launchControl().turretLeft(movement) - if command == "right": - launchControl().turretRight(movement) + if command == "right": + launchControl().turretRight(movement) - if command == "up": - launchControl().turretUp(movement) + if command == "up": + launchControl().turretUp(movement) + + if command == "down": + launchControl().turretDown(movement) + +if __name__ == '__main__': + + if not os.geteuid() == 0: + sys.exit("Script must be run as root.") + + # Get the arguments list + cmdargs = sys.argv - if command == "down": - launchControl().turretDown(movement) + commandControl(cmdargs) From 62985e2ce31278652ad4919ca600c654684263cf Mon Sep 17 00:00:00 2001 From: Kenny Heinonen Date: Tue, 27 Aug 2013 15:58:30 +0300 Subject: [PATCH 7/9] Better if-statement --- turret.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/turret.py b/turret.py index a609029..81b0046 100755 --- a/turret.py +++ b/turret.py @@ -77,7 +77,7 @@ def commandControl(cmdargs): # Get the total number of args passed to the turret.py total = len(cmdargs) - if total == 2 and str.lower(cmdargs[1]) == "fire": + if total >= 2 and str.lower(cmdargs[1]) == "fire": launchControl().turretFire() return @@ -86,6 +86,7 @@ def commandControl(cmdargs): return command = str.lower(cmdargs[1]) + movement = int(cmdargs[2]) / 1000.0 launchControl().setSound(True) From f6737b9958b0a68c2fca8d39b8a0f691b7470dea Mon Sep 17 00:00:00 2001 From: kennyhei Date: Tue, 27 Aug 2013 16:00:03 +0300 Subject: [PATCH 8/9] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3a6fd86..7ae8ca5 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,10 @@ It also requires that you run it as root unless you want to spend an afternoon p * Use arrow keys aim. * Press return to fire. -## Controlling Missile Launcher from the terminal: +## Controlling from the terminal: cd stormLauncher chmod +x turret.py - sudo ./turret.py <"left" | "right" | "up" | "down"> + sudo ./turret.py <"left" | "right" | "up" | "down" | "fire"> + +* If the first argument equals "fire", second argument is completely optional. From b4e9c45121d604ab9ecfb76a712adb8a9b950006 Mon Sep 17 00:00:00 2001 From: kennyhei Date: Sun, 8 Sep 2013 19:05:41 +0300 Subject: [PATCH 9/9] remove sound configs --- turret.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/turret.py b/turret.py index 81b0046..492e691 100755 --- a/turret.py +++ b/turret.py @@ -6,8 +6,6 @@ import pygame import usb.core -wavFile = "warcry.wav" - cmdargs = [] initialTime = 0 @@ -19,8 +17,6 @@ def __init__(self): if self.dev.is_kernel_driver_active(0) is True: self.dev.detach_kernel_driver(0) self.dev.set_configuration() - - self.hasSound = False def loopMovement(self, movement): while (True): @@ -59,18 +55,8 @@ def turretStop(self): def turretFire(self): print("FIRE!") - if os.path.isfile(wavFile): - if self.hasSound == True: - pygame.init() - sound = pygame.mixer.Sound("warcry.wav") - sound.play() - time.sleep(3) - self.dev.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) - def setSound(self, soundOn): - self.hasSound = soundOn - def commandControl(cmdargs): @@ -88,7 +74,6 @@ def commandControl(cmdargs): command = str.lower(cmdargs[1]) movement = int(cmdargs[2]) / 1000.0 - launchControl().setSound(True) global initialTime initialTime = time.time();