|
| 1 | +#!/usr/bin/env python |
| 2 | +# Exercise the indexer at various elevations. |
| 3 | + |
| 4 | +import time |
| 5 | +import katpoint |
| 6 | + |
| 7 | +from katcorelib import (standard_script_options, |
| 8 | + verify_and_connect, |
| 9 | + user_logger) |
| 10 | + |
| 11 | + |
| 12 | +def wait_until_sensor_equals(timeout, sensorname, value, |
| 13 | + sensortype=str, places=5, pollperiod=0.5): |
| 14 | + |
| 15 | + stoptime = time.time() + timeout |
| 16 | + success = False |
| 17 | + |
| 18 | + if sensortype == float: |
| 19 | + cmpfun = lambda got, exp: abs(got - exp) < 10 ** -places |
| 20 | + else: |
| 21 | + cmpfun = lambda got, exp: got == exp |
| 22 | + |
| 23 | + lastval = None |
| 24 | + while time.time() < stoptime: |
| 25 | + lastval = kat.sensors.get(sensorname, None).get_value() |
| 26 | + |
| 27 | + if cmpfun(lastval, value): |
| 28 | + success = True |
| 29 | + break |
| 30 | + time.sleep(pollperiod) |
| 31 | + return (success, lastval) |
| 32 | + |
| 33 | + |
| 34 | +def track(ant, target, ridx_position='l', duration=10, dry_run=False): |
| 35 | + |
| 36 | + # TODO: make the indexer timeout configurable parameter |
| 37 | + indexer_timeout = 120 |
| 38 | + # send this target to the antenna. |
| 39 | + ant.req.target(target.description) |
| 40 | + user_logger.info("Target description: '%s' " % target.description) |
| 41 | + ant.req.mode("POINT") |
| 42 | + |
| 43 | + if not dry_run: |
| 44 | + # Added sleep to wait for AP brakes to disengage |
| 45 | + time.sleep(8) |
| 46 | + |
| 47 | + # TODO: more meaningful indication of the target |
| 48 | + # (where no name is specified) |
| 49 | + user_logger.info("Slewing %s to target: %s" % (ant.name, target.name)) |
| 50 | + # Wait for antenna to lock onto target |
| 51 | + if not dry_run: |
| 52 | + if ant.wait('lock', True, timeout=300): |
| 53 | + user_logger.info("%s on target... " |
| 54 | + "Wait for %d seconds " |
| 55 | + "before starting indexer cycle" % |
| 56 | + (ant.name, duration)) |
| 57 | + time.sleep(duration) |
| 58 | + else: |
| 59 | + user_logger.error("Antennas failed to reach target.") |
| 60 | + |
| 61 | + ant.req.mode('STOP') |
| 62 | + if not dry_run: |
| 63 | + # Added sleep to wait for AP brakes to engage |
| 64 | + time.sleep(2) |
| 65 | + result = wait_until_sensor_equals(5.0, ant.name + '_mode', 'STOP') |
| 66 | + if result[0] == False: |
| 67 | + user_logger.error("Failed to set AP to 'STOP' mode. " |
| 68 | + "Indexer commands will not be processed.") |
| 69 | + return |
| 70 | + |
| 71 | + user_logger.info("Setting initial RI on %s to position : %s " % |
| 72 | + (ant.name, ridx_position.upper())) |
| 73 | + |
| 74 | + ant.req.ap_set_indexer_position(ridx_position) |
| 75 | + if not dry_run: |
| 76 | + result = wait_until_sensor_equals(indexer_timeout, |
| 77 | + ant.name + '_ap_indexer_position', |
| 78 | + ridx_position) |
| 79 | + else: |
| 80 | + result = (True, ridx_position) |
| 81 | + |
| 82 | + if result[0] == False: |
| 83 | + ridx_position_raw = kat.sensors.get(ant.name + '_ap_indexer_position_raw', None).get_value() |
| 84 | + user_logger.error("Timed out while waiting %s seconds for " |
| 85 | + "indexer to reach '%s' position. " |
| 86 | + "Last position reading was %s degrees." % |
| 87 | + (indexer_timeout, |
| 88 | + ridx_position.upper(), |
| 89 | + ridx_position_raw)) |
| 90 | + |
| 91 | + # TODO: make this sequence easier to configure |
| 92 | + ridx_sequence = ['u', 'l', 'x', 's', 'l', 'u', 's', 'x', |
| 93 | + 'u', 'l', 's', 'x', 'l', 'u', 'x', 's', |
| 94 | + 'u', 'x', 'l', 's', 'l', 's'] |
| 95 | + |
| 96 | + # Cycling indexer positions |
| 97 | + if not dry_run: |
| 98 | + for pos in ridx_sequence: |
| 99 | + ridx_last_position = kat.sensors.get(ant.name + '_ap_indexer_position', None).get_value() |
| 100 | + ridx_movement_start_time = time.time() |
| 101 | + user_logger.info("--- Moving RI to position: '%s' ---" % |
| 102 | + pos.upper()) |
| 103 | + ant.req.ap_set_indexer_position(pos) |
| 104 | + |
| 105 | + result = wait_until_sensor_equals(indexer_timeout, |
| 106 | + ant.name + '_ap_indexer_position', |
| 107 | + pos) |
| 108 | + user_logger.debug("Request result: '%s', " |
| 109 | + "last sensor reading: '%s'" % |
| 110 | + (result[0], result[1])) |
| 111 | + |
| 112 | + if result[0] == False: |
| 113 | + ridx_position_raw = kat.sensors.get(ant.name + '_ap_indexer_position_raw', None).get_value() |
| 114 | + ridx_brakes_released = kat.sensors.get(ant.name + '_ap_ridx_brakes_released', None).get_value() |
| 115 | + |
| 116 | + user_logger.error("Timed out while waiting %s seconds " |
| 117 | + "for indexer to reach '%s' position. " |
| 118 | + "Last position reading was %s degrees. " |
| 119 | + "Brakes released: '%s'. " % |
| 120 | + (indexer_timeout, pos.upper(), |
| 121 | + ridx_position_raw, |
| 122 | + ridx_brakes_released)) |
| 123 | + |
| 124 | + ridx_current_position = kat.sensors.get(ant.name + '_ap_indexer_position', None).get_value() |
| 125 | + time_to_index = time.time() - ridx_movement_start_time |
| 126 | + if ridx_current_position in ['undefined']: |
| 127 | + user_logger.warning("Movement ended in undefined position") |
| 128 | + else: |
| 129 | + user_logger.info("RIDX from '%s' to '%s' position " |
| 130 | + "took '%s' seconds." % |
| 131 | + (ridx_last_position.upper(), |
| 132 | + ridx_current_position.upper(), |
| 133 | + time_to_index)) |
| 134 | + |
| 135 | + # 60 seconds comes from the antenna specification |
| 136 | + if (time_to_index > 60.0): |
| 137 | + user_logger.warning("Indexer took longer than 60 seconds!") |
| 138 | + |
| 139 | + user_logger.info("Dwell for %s seconds before " |
| 140 | + "going to next position." % duration) |
| 141 | + time.sleep(duration) |
| 142 | + |
| 143 | + user_logger.info("Pattern complete. Heading to next sky target.") |
| 144 | + |
| 145 | + |
| 146 | +# Set up standard script options |
| 147 | + |
| 148 | +parser = standard_script_options(usage="%prog [options] <'target/catalogue'> [<'target/catalogue'> ...]", |
| 149 | + description="Exercise the receiver indexer drive at different elevations.") |
| 150 | + |
| 151 | +parser.add_option('--dwell-time', type='float', |
| 152 | + default=5.0, |
| 153 | + help='Time between changing indexer positions in seconds (default=%default)') |
| 154 | +parser.add_option('--num-repeat', type='int', |
| 155 | + default=1, |
| 156 | + help='The number of times to repeat the sequence (once by by default)') |
| 157 | +parser.add_option('--ap', type='str', |
| 158 | + default="m036", |
| 159 | + help='Receptor under test (default is m036)') |
| 160 | + |
| 161 | +# Parse the command line |
| 162 | +opts, args = parser.parse_args() |
| 163 | + |
| 164 | +if opts.observer is None: |
| 165 | + raise RuntimeError("No observer provided script") |
| 166 | + |
| 167 | +if len(args) == 0: |
| 168 | + raise RuntimeError("No targets and indexer positions" |
| 169 | + "provided to the script") |
| 170 | + |
| 171 | +receptor = None |
| 172 | +targetlist = [] |
| 173 | + |
| 174 | +for argstr in args: |
| 175 | + temp, taz, tel, band = argstr.split(',') |
| 176 | + print "azimuth : %r elevation : %r indexer : %s" % (taz, tel, band) |
| 177 | + targetlist.append([taz, tel, band]) |
| 178 | + |
| 179 | +# Check options and build KAT configuration, connecting to proxies and devices |
| 180 | +with verify_and_connect(opts) as kat: |
| 181 | + |
| 182 | + for ant in kat.ants: |
| 183 | + if opts.ap in [ant.name]: |
| 184 | + receptor = ant |
| 185 | + |
| 186 | + if receptor is None: |
| 187 | + raise RuntimeError("Receptor under test is not in controlled array") |
| 188 | + |
| 189 | + # Set sensor strategies" |
| 190 | + kat.ants.set_sampling_strategy("lock", "event") |
| 191 | + kat.ants.set_sampling_strategy("ap.indexer-position", "event") |
| 192 | + |
| 193 | + if not kat.dry_run and receptor.req.mode('STOP'): |
| 194 | + user_logger.info("Setting Antenna Mode to 'STOP', " |
| 195 | + "Powering on Antenna Drives.") |
| 196 | + time.sleep(2) |
| 197 | + else: |
| 198 | + if not kat.dry_run: |
| 199 | + user_logger.error("Unable to set Antenna mode to 'STOP'.") |
| 200 | + |
| 201 | + for taz, tel, band in targetlist: |
| 202 | + for i in range(int(opts.num_repeat)): |
| 203 | + target = katpoint.Target('Name, azel, %s, %s' % (taz, tel)) |
| 204 | + track(receptor, target, ridx_position=band, |
| 205 | + duration=opts.dwell_time, dry_run=kat.dry_run) |
| 206 | + user_logger.info("Elevation: '%s' " |
| 207 | + "Patterns attempted: '%s' " |
| 208 | + "Contractual cycles: '%s'." % |
| 209 | + (tel, i+1, 11*(i+1))) |
0 commit comments