| 1 | #! /usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | ''' |
|---|
| 4 | /**************************************************************************** |
|---|
| 5 | |
|---|
| 6 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 7 | |
|---|
| 8 | This file is part of the OSSIE USRP_Commander_GUI. |
|---|
| 9 | |
|---|
| 10 | OSSIE USRP_Commander_GUI is free software; you can redistribute it and/or modify |
|---|
| 11 | it under the terms of the GNU General Public License as published by |
|---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | (at your option) any later version. |
|---|
| 14 | |
|---|
| 15 | OSSIE USRP_Commander_GUI is distributed in the hope that it will be useful, |
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | GNU General Public License for more details. |
|---|
| 19 | |
|---|
| 20 | You should have received a copy of the GNU General Public License |
|---|
| 21 | along with OSSIE USRP_Commander_GUI; if not, write to the Free Software |
|---|
| 22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | |
|---|
| 24 | ****************************************************************************/ |
|---|
| 25 | |
|---|
| 26 | ''' |
|---|
| 27 | |
|---|
| 28 | from omniORB import CORBA |
|---|
| 29 | from omniORB import URI |
|---|
| 30 | import CosNaming |
|---|
| 31 | import CF, CF__POA |
|---|
| 32 | import standardInterfaces__POA |
|---|
| 33 | import customInterfaces__POA |
|---|
| 34 | import sys |
|---|
| 35 | |
|---|
| 36 | import WorkModule # module found in the component directory. |
|---|
| 37 | # this module is where the main processing |
|---|
| 38 | # thread resides. |
|---|
| 39 | import threading |
|---|
| 40 | import time # primarily availble for time.sleep() statements |
|---|
| 41 | |
|---|
| 42 | #------------------------------------------------------------- |
|---|
| 43 | # USRP_Commander_GUI_i class definition (main component class) |
|---|
| 44 | #------------------------------------------------------------- |
|---|
| 45 | class USRP_Commander_GUI_i(CF__POA.Resource): |
|---|
| 46 | def __init__(self, uuid, label, poa): |
|---|
| 47 | CF._objref_Resource.__init__(self._this()) |
|---|
| 48 | print "USRP_Commander_GUI_i __init__: " + label |
|---|
| 49 | self.naming_service_name = label |
|---|
| 50 | self.poa = poa |
|---|
| 51 | |
|---|
| 52 | self.outPort0_servant = dataOut_Resource_i(self, "Data_Control") |
|---|
| 53 | self.outPort0_var = self.outPort0_servant._this() |
|---|
| 54 | self.outPort0_active = False |
|---|
| 55 | |
|---|
| 56 | self.outPort1_servant = dataOut_Radio_Control_i(self, "RX_Control") |
|---|
| 57 | self.outPort1_var = self.outPort1_servant._this() |
|---|
| 58 | self.outPort1_active = False |
|---|
| 59 | |
|---|
| 60 | self.outPort2_servant = dataOut_Radio_Control_i(self, "TX_Control") |
|---|
| 61 | self.outPort2_var = self.outPort2_servant._this() |
|---|
| 62 | self.outPort2_active = False |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | self.WorkModule_created = False |
|---|
| 66 | |
|---|
| 67 | self.propertySet = [] |
|---|
| 68 | |
|---|
| 69 | # properties initializations: |
|---|
| 70 | rx_freq = 0 |
|---|
| 71 | tx_freq = 0 |
|---|
| 72 | tx_interp_rate = 0 |
|---|
| 73 | rx_dec_fact = 0 |
|---|
| 74 | rx_pack_size = 0 |
|---|
| 75 | rx_gain = 0 |
|---|
| 76 | rx_gain_max = 0 |
|---|
| 77 | rx_start_flag = 0 |
|---|
| 78 | tx_start_flag = 0 |
|---|
| 79 | |
|---|
| 80 | self.work_mod = None |
|---|
| 81 | |
|---|
| 82 | def start(self): |
|---|
| 83 | print "USRP_Commander_GUI start called" |
|---|
| 84 | |
|---|
| 85 | # TODO: call start on remote resources |
|---|
| 86 | |
|---|
| 87 | #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
|---|
| 88 | # Set Transmitter Properties: |
|---|
| 89 | |
|---|
| 90 | # TODO: replace ##'s with python code |
|---|
| 91 | |
|---|
| 92 | # Initialize to default TX values |
|---|
| 93 | ##TXControl->set_number_of_channels(1); |
|---|
| 94 | ##TXControl->set_gain(DEFAULT_USRP_TX_CHANNEL, tx_gain); |
|---|
| 95 | ##TXControl->set_frequency(DEFAULT_USRP_TX_CHANNEL, tx_freq); |
|---|
| 96 | ##TXControl->set_interpolation_rate(DEFAULT_USRP_TX_CHANNEL, tx_interp); |
|---|
| 97 | |
|---|
| 98 | # Set transmit configurable properties not included in Radio_Control idl |
|---|
| 99 | ##CF::Properties tx_config; |
|---|
| 100 | ##tx_config.length(1); |
|---|
| 101 | |
|---|
| 102 | # Set automatic transmit/receive mode on |
|---|
| 103 | ##tx_config[0].id = CORBA::string_dup("SET_AUTO_TR_1"); |
|---|
| 104 | ##tx_config[0].value <<= (CORBA::ULong) 1; |
|---|
| 105 | |
|---|
| 106 | ##TXControl->set_values(tx_config); |
|---|
| 107 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv |
|---|
| 111 | # Set Receiver Properties: |
|---|
| 112 | |
|---|
| 113 | # Initialize to default RX values |
|---|
| 114 | ##RXControl->set_number_of_channels(1); |
|---|
| 115 | ##RXControl->set_gain(DEFAULT_USRP_RX_CHANNEL, rx_gain); |
|---|
| 116 | ##RXControl->set_frequency(DEFAULT_USRP_RX_CHANNEL, rx_freq); |
|---|
| 117 | ##RXControl->set_decimation_rate(DEFAULT_USRP_RX_CHANNEL, rx_decim); |
|---|
| 118 | ##RXControl->set_data_packet_size(DEFAULT_USRP_RX_CHANNEL, rx_size); |
|---|
| 119 | |
|---|
| 120 | # Set transmit configurable properties not included in Radio_Control idl |
|---|
| 121 | ##CF::Properties rx_config; |
|---|
| 122 | ##rx_config.length(1); |
|---|
| 123 | |
|---|
| 124 | # Set rx antenna |
|---|
| 125 | ##rx_config[0].id = CORBA::string_dup("SET_RX_ANT_1"); |
|---|
| 126 | ##rx_config[0].value <<= (CORBA::ULong) 0; |
|---|
| 127 | |
|---|
| 128 | ##RXControl->set_values(rx_config); |
|---|
| 129 | |
|---|
| 130 | ##if (tx_start) { |
|---|
| 131 | ## DEBUG(3, USRP_Commander, "starting USRP transmit process..."); |
|---|
| 132 | ## TXControl->start(DEFAULT_USRP_TX_CHANNEL); |
|---|
| 133 | ##} |
|---|
| 134 | |
|---|
| 135 | ##if (rx_start) { |
|---|
| 136 | ## RXControl->start(DEFAULT_USRP_RX_CHANNEL); |
|---|
| 137 | ## DEBUG(3, USRP_Commander, "starting USRP receive process..."); |
|---|
| 138 | ##} |
|---|
| 139 | #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | def stop(self): |
|---|
| 143 | print "USRP_Commander_GUI stop called" |
|---|
| 144 | |
|---|
| 145 | def getPort(self, id): |
|---|
| 146 | if str(id) == "Data_Control": |
|---|
| 147 | return self.outPort0_var |
|---|
| 148 | if str(id) == "RX_Control": |
|---|
| 149 | return self.outPort0_var |
|---|
| 150 | if str(id) == "TX_Control": |
|---|
| 151 | return self.outPort0_var |
|---|
| 152 | |
|---|
| 153 | return None #port not found in available ports list |
|---|
| 154 | |
|---|
| 155 | def initialize(self): |
|---|
| 156 | print "USRP_Commander_GUI initialize called" |
|---|
| 157 | |
|---|
| 158 | def configure(self, props): |
|---|
| 159 | ''' The configure method is called twice by the framework: |
|---|
| 160 | once to read the default properties in the component.prf |
|---|
| 161 | file, and once to read the component instance properties |
|---|
| 162 | storred in the waveform.sad file. This method should be |
|---|
| 163 | called before the start method. This method is where |
|---|
| 164 | the properties are read in by the component. |
|---|
| 165 | ''' |
|---|
| 166 | |
|---|
| 167 | print "USRP_Commander_GUI configure called" |
|---|
| 168 | buffer_size = 0 |
|---|
| 169 | |
|---|
| 170 | # TODO: set number of channels in RXControl? |
|---|
| 171 | |
|---|
| 172 | for property in props: |
|---|
| 173 | if property not in self.propertySet: |
|---|
| 174 | self.propertySet.append(property) |
|---|
| 175 | |
|---|
| 176 | if property.id = "DCE:6a2d6952-ca11-4787-afce-87a89b882b7b": |
|---|
| 177 | # receiver frequency |
|---|
| 178 | rx_freq = int(property.value.value()) |
|---|
| 179 | # TODO: do something with Rx freq here |
|---|
| 180 | |
|---|
| 181 | elif property.id = "DCE:9ca12c0e-ba65-40cf-9ef3-6e7ac671ab5d": |
|---|
| 182 | # transmitter frequency |
|---|
| 183 | tx_freq = int(property.value.value()) |
|---|
| 184 | # TODO: do something with tx frequency here |
|---|
| 185 | |
|---|
| 186 | elif property.id = "DCE:92ec2b80-8040-47c7-a1d8-4c9caa4a4ed2": |
|---|
| 187 | # transmitter interpolation rate |
|---|
| 188 | tx_interp_rate = int(property.value.value()) |
|---|
| 189 | # TODO: do something with tx interpolation rate here |
|---|
| 190 | |
|---|
| 191 | elif property.id = "DCE:3efc3930-2739-40b4-8c02-ecfb1b0da9ee": |
|---|
| 192 | # receiver decimation factor |
|---|
| 193 | rx_dec_fact = int(property.value.value()) |
|---|
| 194 | # TODO: do somethign with the rx decimation factor |
|---|
| 195 | |
|---|
| 196 | elif property.id = "DCE:93324adf-14f6-4406-ba92-a3650089857f": |
|---|
| 197 | # receiver data packet size |
|---|
| 198 | rx_pack_size = int(property.value.value()) |
|---|
| 199 | # TODO: do something with rx data packet size |
|---|
| 200 | |
|---|
| 201 | elif property.id = "DCE:99d586b6-7764-4dc7-83fa-72270d0f1e1b": |
|---|
| 202 | # receiver gain |
|---|
| 203 | rx_gain = int(property.value.value()) |
|---|
| 204 | # TODO: do something with rx gain |
|---|
| 205 | |
|---|
| 206 | elif property.id = "DCE:2d9c5ee4-a6f3-4ab9-834b-2b5c95818e53": |
|---|
| 207 | # receiver gain max |
|---|
| 208 | rx_gain_max = int(property.value.value()) |
|---|
| 209 | # TODO: do something with rx gain max |
|---|
| 210 | |
|---|
| 211 | elif property.id = "DCE:fd42344f-4d87-465b-9e6f-e1d7ae48afd6": |
|---|
| 212 | # receiver start flag |
|---|
| 213 | rx_start_flag = int(property.value.value()) |
|---|
| 214 | # TODO: do something with rx start flag |
|---|
| 215 | |
|---|
| 216 | elif property.id = "DCE:0a9b8c8c-f130-4a8f-9ef8-bba023128a4b": |
|---|
| 217 | # transmitter start flag |
|---|
| 218 | tx_start_flag = int(property.value.value()) |
|---|
| 219 | # TODO: do something with tx start flag |
|---|
| 220 | |
|---|
| 221 | else: |
|---|
| 222 | print "WARNING: the property with id :" + property.id + " is not supported in the configure method of USRP_Commander_GUI.py" |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | # make sure that only one WorkModule thread is started, |
|---|
| 226 | # even if configure method is called more than once |
|---|
| 227 | if not self.WorkModule_created: |
|---|
| 228 | self.work_mod = WorkModule.WorkClass(self, buffer_size) |
|---|
| 229 | self.WorkModule_created = True |
|---|
| 230 | |
|---|
| 231 | def query(self, props): |
|---|
| 232 | return self.propertySet |
|---|
| 233 | |
|---|
| 234 | def releaseObject(self): |
|---|
| 235 | # release the main work module |
|---|
| 236 | self.work_mod.Release() |
|---|
| 237 | |
|---|
| 238 | # release the main process threads for the ports |
|---|
| 239 | self.outPort0_servant.releasePort() |
|---|
| 240 | self.outPort1_servant.releasePort() |
|---|
| 241 | self.outPort2_servant.releasePort() |
|---|
| 242 | |
|---|
| 243 | # deactivate the ports |
|---|
| 244 | oid0 = self.poa.reference_to_id(self.outPort0_var) |
|---|
| 245 | oid0 = self.poa.reference_to_id(self.outPort0_var) |
|---|
| 246 | oid0 = self.poa.reference_to_id(self.outPort0_var) |
|---|
| 247 | |
|---|
| 248 | self.poa.deactivate_object(oid0) |
|---|
| 249 | self.poa.deactivate_object(oid1) |
|---|
| 250 | self.poa.deactivate_object(oid2) |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | #------------------------------------------------------------------ |
|---|
| 255 | # dataOut_complexShort_i class definition |
|---|
| 256 | #------------------------------------------------------------------ |
|---|
| 257 | class dataOut_Resource_i(CF__POA.Port): |
|---|
| 258 | def __init__(self, parent, name): |
|---|
| 259 | self.parent = parent |
|---|
| 260 | self.outPorts = {} |
|---|
| 261 | self.name = name |
|---|
| 262 | |
|---|
| 263 | self.data_buffer = [] |
|---|
| 264 | self.data_event = threading.Event() |
|---|
| 265 | self.data_buffer_lock = threading.Lock() |
|---|
| 266 | |
|---|
| 267 | self.is_running = True |
|---|
| 268 | self.process_thread = threading.Thread(target = self.Process) |
|---|
| 269 | self.process_thread.start() |
|---|
| 270 | |
|---|
| 271 | def connectPort(self, connection, connectionId): |
|---|
| 272 | port = connection._narrow(CF__POA.Resource) |
|---|
| 273 | self.outPorts[str(connectionId)] = port |
|---|
| 274 | self.parent.outPort0_active = True |
|---|
| 275 | |
|---|
| 276 | def disconnectPort(self, connectionId): |
|---|
| 277 | self.outPorts.pop(str(connectionId)) |
|---|
| 278 | if len(self.outPorts)==0: |
|---|
| 279 | self.parent.outPort0_active = False |
|---|
| 280 | |
|---|
| 281 | def releasePort(self): |
|---|
| 282 | # shut down the Process thread |
|---|
| 283 | self.is_running = False |
|---|
| 284 | self.data_event.set() |
|---|
| 285 | |
|---|
| 286 | # WARNING: I and Q may have to be changed depending on what data you are receiving (e.g., bytesIn for realChar) |
|---|
| 287 | def send_data(self, I, Q): |
|---|
| 288 | self.data_buffer_lock.acquire() |
|---|
| 289 | self.data_buffer.insert(0, (I,Q)) |
|---|
| 290 | self.data_buffer_lock.release() |
|---|
| 291 | self.data_event.set() |
|---|
| 292 | |
|---|
| 293 | def Process(self): |
|---|
| 294 | while self.is_running: |
|---|
| 295 | self.data_event.wait() |
|---|
| 296 | while len(self.data_buffer) > 0: |
|---|
| 297 | self.data_buffer_lock.acquire() |
|---|
| 298 | new_data = self.data_buffer.pop() |
|---|
| 299 | self.data_buffer_lock.release() |
|---|
| 300 | |
|---|
| 301 | for port in self.outPorts.values(): |
|---|
| 302 | port.pushPacket(new_data[0], new_data[1]) |
|---|
| 303 | |
|---|
| 304 | self.data_event.clear() |
|---|
| 305 | |
|---|
| 306 | #------------------------------------------------------------------ |
|---|
| 307 | # dataOut_complexShort_i class definition |
|---|
| 308 | #------------------------------------------------------------------ |
|---|
| 309 | class dataOut_Radio_Control_i(CF__POA.Port): |
|---|
| 310 | def __init__(self, parent, name): |
|---|
| 311 | self.parent = parent |
|---|
| 312 | self.outPorts = {} |
|---|
| 313 | self.name = name |
|---|
| 314 | |
|---|
| 315 | self.data_buffer = [] |
|---|
| 316 | self.data_event = threading.Event() |
|---|
| 317 | self.data_buffer_lock = threading.Lock() |
|---|
| 318 | |
|---|
| 319 | self.is_running = True |
|---|
| 320 | self.process_thread = threading.Thread(target = self.Process) |
|---|
| 321 | self.process_thread.start() |
|---|
| 322 | |
|---|
| 323 | def connectPort(self, connection, connectionId): |
|---|
| 324 | port = connection._narrow(standardInterfaces__POA.Radio_Control) |
|---|
| 325 | self.outPorts[str(connectionId)] = port |
|---|
| 326 | self.parent.outPort1_active = True |
|---|
| 327 | |
|---|
| 328 | def disconnectPort(self, connectionId): |
|---|
| 329 | self.outPorts.pop(str(connectionId)) |
|---|
| 330 | if len(self.outPorts)==0: |
|---|
| 331 | self.parent.outPort0_active = False |
|---|
| 332 | |
|---|
| 333 | def releasePort(self): |
|---|
| 334 | # shut down the Process thread |
|---|
| 335 | self.is_running = False |
|---|
| 336 | self.data_event.set() |
|---|
| 337 | |
|---|
| 338 | # WARNING: I and Q may have to be changed depending on what data you are receiving (e.g., bytesIn for realChar) |
|---|
| 339 | def send_data(self, I, Q): |
|---|
| 340 | self.data_buffer_lock.acquire() |
|---|
| 341 | self.data_buffer.insert(0, (I,Q)) |
|---|
| 342 | self.data_buffer_lock.release() |
|---|
| 343 | self.data_event.set() |
|---|
| 344 | |
|---|
| 345 | def Process(self): |
|---|
| 346 | while self.is_running: |
|---|
| 347 | self.data_event.wait() |
|---|
| 348 | while len(self.data_buffer) > 0: |
|---|
| 349 | self.data_buffer_lock.acquire() |
|---|
| 350 | new_data = self.data_buffer.pop() |
|---|
| 351 | self.data_buffer_lock.release() |
|---|
| 352 | |
|---|
| 353 | for port in self.outPorts.values(): |
|---|
| 354 | port.pushPacket(new_data[0], new_data[1]) |
|---|
| 355 | |
|---|
| 356 | self.data_event.clear() |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | #------------------------------------------------------------------- |
|---|
| 361 | # ORB_Init class definition |
|---|
| 362 | #------------------------------------------------------------------- |
|---|
| 363 | class ORB_Init: |
|---|
| 364 | """Takes care of initializing the ORB and bind the object""" |
|---|
| 365 | |
|---|
| 366 | def __init__(self, uuid, label): |
|---|
| 367 | # initialize the orb |
|---|
| 368 | self.orb = CORBA.ORB_init() |
|---|
| 369 | |
|---|
| 370 | # get the POA |
|---|
| 371 | obj_poa = self.orb.resolve_initial_references("RootPOA") |
|---|
| 372 | poaManager = obj_poa._get_the_POAManager() |
|---|
| 373 | poaManager.activate() |
|---|
| 374 | |
|---|
| 375 | ns_obj = self.orb.resolve_initial_references("NameService") |
|---|
| 376 | rootContext = ns_obj._narrow(CosNaming.NamingContext) |
|---|
| 377 | |
|---|
| 378 | # create the main component object |
|---|
| 379 | self.USRP_Commander_GUI_Obj = USRP_Commander_GUI_i(uuid, label, obj_poa) |
|---|
| 380 | USRP_Commander_GUI_var = self.USRP_Commander_GUI_Obj._this() |
|---|
| 381 | |
|---|
| 382 | name = URI.stringToName(label) |
|---|
| 383 | rootContext.rebind(name, USRP_Commander_GUI_var) |
|---|
| 384 | |
|---|
| 385 | self.orb.run() |
|---|
| 386 | |
|---|
| 387 | #------------------------------------------------------------------- |
|---|
| 388 | # Code run when this file is executed |
|---|
| 389 | #------------------------------------------------------------------- |
|---|
| 390 | if __name__ == "__main__": |
|---|
| 391 | if len(sys.argv) != 3: |
|---|
| 392 | print sys.argv[0] + " <id> <usage name> " |
|---|
| 393 | |
|---|
| 394 | uuid = str(sys.argv[1]) |
|---|
| 395 | label = str(sys.argv[2]) |
|---|
| 396 | |
|---|
| 397 | print "Identifier - " + uuid + " Label - " + label |
|---|
| 398 | |
|---|
| 399 | orb = ORB_Init(uuid, label) |
|---|
| 400 | |
|---|
| 401 | |
|---|