|
| 1 | +"""BSD 2-Clause License |
| 2 | +
|
| 3 | +Copyright (c) 2019, Allied Vision Technologies GmbH |
| 4 | +All rights reserved. |
| 5 | +
|
| 6 | +Redistribution and use in source and binary forms, with or without |
| 7 | +modification, are permitted provided that the following conditions are met: |
| 8 | +
|
| 9 | +1. Redistributions of source code must retain the above copyright notice, this |
| 10 | + list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +
|
| 27 | +THE SOFTWARE IS PRELIMINARY AND STILL IN TESTING AND VERIFICATION PHASE AND |
| 28 | +IS PROVIDED ON AN “AS IS” AND “AS AVAILABLE” BASIS AND IS BELIEVED TO CONTAIN DEFECTS. |
| 29 | +A PRIMARY PURPOSE OF THIS EARLY ACCESS IS TO OBTAIN FEEDBACK ON PERFORMANCE AND |
| 30 | +THE IDENTIFICATION OF DEFECT SOFTWARE, HARDWARE AND DOCUMENTATION. |
| 31 | +""" |
| 32 | + |
| 33 | +import sys |
| 34 | +from typing import Tuple |
| 35 | +from vimba import * |
| 36 | + |
| 37 | + |
| 38 | +def print_preamble(): |
| 39 | + print('/////////////////////////////////////////') |
| 40 | + print('/// Vimba API Action Commands Example ///') |
| 41 | + print('/////////////////////////////////////////\n') |
| 42 | + |
| 43 | + |
| 44 | +def print_usage(): |
| 45 | + print('Usage:') |
| 46 | + print(' python action_commands.py <camera_id> <interface_id>') |
| 47 | + print(' python action_commands.py [/h] [-h]') |
| 48 | + print() |
| 49 | + print('Parameters:') |
| 50 | + print(' camera_id ID of the camera to be used') |
| 51 | + print(' interface_id ID of network interface to send out Action Command') |
| 52 | + print(' \'ALL\' enables broadcast on all interfaces') |
| 53 | + print() |
| 54 | + |
| 55 | + |
| 56 | +def abort(reason: str, return_code: int = 1, usage: bool = False): |
| 57 | + print(reason + '\n') |
| 58 | + |
| 59 | + if usage: |
| 60 | + print_usage() |
| 61 | + |
| 62 | + sys.exit(return_code) |
| 63 | + |
| 64 | + |
| 65 | +def parse_args() -> Tuple[str, str]: |
| 66 | + args = sys.argv[1:] |
| 67 | + |
| 68 | + for arg in args: |
| 69 | + if arg in ('/h', '-h'): |
| 70 | + print_usage() |
| 71 | + sys.exit(0) |
| 72 | + |
| 73 | + if len(args) != 2: |
| 74 | + abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True) |
| 75 | + |
| 76 | + return (args[0], args[1]) |
| 77 | + |
| 78 | + |
| 79 | +def get_input() -> str: |
| 80 | + prompt = 'Press \'a\' to send action command. Press \'q\' to stop example. Enter:' |
| 81 | + print(prompt, flush=True) |
| 82 | + return input() |
| 83 | + |
| 84 | + |
| 85 | +def get_camera(camera_id: str) -> Camera: |
| 86 | + with Vimba.get_instance() as vimba: |
| 87 | + try: |
| 88 | + return vimba.get_camera_by_id(camera_id) |
| 89 | + |
| 90 | + except VimbaCameraError: |
| 91 | + abort('Failed to access Camera {}. Abort.'.format(camera_id)) |
| 92 | + |
| 93 | + |
| 94 | +def get_command_sender(interface_id): |
| 95 | + # If given interface_id is ALL, ActionCommand shall be sent from all Ethernet Interfaces. |
| 96 | + # This is achieved by run ActionCommand on the Vimba instance. |
| 97 | + if interface_id == 'ALL': |
| 98 | + return Vimba.get_instance() |
| 99 | + |
| 100 | + with Vimba.get_instance() as vimba: |
| 101 | + # A specific Interface was given. Lookup via given Interface id and verify that |
| 102 | + # it is an Ethernet Interface. Running ActionCommand will be only send from this Interface. |
| 103 | + try: |
| 104 | + inter = vimba.get_interface_by_id(interface_id) |
| 105 | + |
| 106 | + except VimbaInterfaceError: |
| 107 | + abort('Failed to access Interface {}. Abort.'.format(interface_id)) |
| 108 | + |
| 109 | + if inter.get_type() != InterfaceType.Ethernet: |
| 110 | + abort('Given Interface {} is no Ethernet Interface. Abort.'.format(interface_id)) |
| 111 | + |
| 112 | + return inter |
| 113 | + |
| 114 | + |
| 115 | +def set_feature(entity, feature_name: str, feature_value): |
| 116 | + try: |
| 117 | + entity.get_feature_by_name(feature_name).set(feature_value) |
| 118 | + |
| 119 | + except VimbaFeatureError: |
| 120 | + abort('Could not set Feature \'{}\'. Abort.'.format(feature_name)) |
| 121 | + |
| 122 | + |
| 123 | +def command_run(entity, feature_name: str): |
| 124 | + try: |
| 125 | + entity.get_feature_by_name(feature_name).run() |
| 126 | + |
| 127 | + except VimbaFeatureError: |
| 128 | + abort('Failed to run Feature \'{}\'. Abort.'.format(feature_name)) |
| 129 | + |
| 130 | + |
| 131 | +def command_run_and_wait(entity, feature_name: str): |
| 132 | + try: |
| 133 | + cmd_feat = entity.get_feature_by_name(feature_name) |
| 134 | + cmd_feat.run() |
| 135 | + |
| 136 | + while not cmd_feat.is_done(): |
| 137 | + pass |
| 138 | + |
| 139 | + except VimbaFeatureError: |
| 140 | + abort('Failed to run Feature \'{}\'. Abort.'.format(feature_name)) |
| 141 | + |
| 142 | + |
| 143 | +def frame_handler(cam: Camera, frame: Frame): |
| 144 | + if frame.get_status() == FrameStatus.Complete: |
| 145 | + print('Frame(ID: {}) has been received.'.format(frame.get_id()), flush=True) |
| 146 | + |
| 147 | + cam.queue_frame(frame) |
| 148 | + |
| 149 | + |
| 150 | +def main(): |
| 151 | + print_preamble() |
| 152 | + camera_id, interface_id = parse_args() |
| 153 | + |
| 154 | + with Vimba.get_instance(): |
| 155 | + cam = get_camera(camera_id) |
| 156 | + sender = get_command_sender(interface_id) |
| 157 | + |
| 158 | + with cam, sender: |
| 159 | + # Prepare Camera for ActionCommand - Trigger |
| 160 | + device_key = 1 |
| 161 | + group_key = 1 |
| 162 | + group_mask = 1 |
| 163 | + |
| 164 | + command_run_and_wait(cam, 'GVSPAdjustPacketSize') |
| 165 | + set_feature(cam, 'TriggerSelector', 'FrameStart') |
| 166 | + set_feature(cam, 'TriggerSource', 'Action0') |
| 167 | + set_feature(cam, 'TriggerMode', 'On') |
| 168 | + set_feature(cam, 'ActionDeviceKey', device_key) |
| 169 | + set_feature(cam, 'ActionGroupKey', group_key) |
| 170 | + set_feature(cam, 'ActionGroupMask', group_mask) |
| 171 | + |
| 172 | + # Enter Streaming mode and wait for user input. |
| 173 | + try: |
| 174 | + cam.start_streaming(frame_handler) |
| 175 | + |
| 176 | + while True: |
| 177 | + ch = get_input() |
| 178 | + |
| 179 | + if ch == 'q': |
| 180 | + break |
| 181 | + |
| 182 | + elif ch == 'a': |
| 183 | + set_feature(sender, 'ActionDeviceKey', device_key) |
| 184 | + set_feature(sender, 'ActionGroupKey', group_key) |
| 185 | + set_feature(sender, 'ActionGroupMask', group_mask) |
| 186 | + command_run(sender, 'ActionCommand') |
| 187 | + |
| 188 | + finally: |
| 189 | + cam.stop_streaming() |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + main() |