Wednesday, April 10, 2024

Re: How to get UHD 'rx_time' / 'rx_freq' after 'tune'? (Python)

Hi All,

I managed to partially answer my own question:

> ... Question:
>
> Although I know when the command was sent, I don't know when it was processed. I want to either:
>
> 1. get 'rx_time' and 'rx_freq' messages after the 'tune' command is executed, or
> 2. access <my_uhd_block>.get_time_now() / <my_uhd_block>.get_center_freq() from within a python block. I'm unclear on how to get the <my_uhd_block> reference.
>

I'm still not sure how to get messages (1.), the answer for (2.) is as follows:

ASSUMPTIONS/PRECONDITIONS:
- An existing 'UHD: USRP Source' Block named 'B210A' ...
- An Embedded 'Python Block' (epyblock) named 'MyEPyBlock' that needs info from B210A

(1) Create a variable in the flowgraph named 'REF_B210A' and initialize it to 0

(2) Create a 'Python Snippet' Block 'pysnip_main_after_start_setB210A' : the 'Section of Flowgraph' must be set to 'Main After Start'.

(3) The 'Code Snippet' of the 'pysnip_main_after_start_setB210A' block must contain this line:

```python
# this line must be in the body of 'pysnip_main_after_start_setB210A':
self.set_REF_B210A(self.B210A) # note: self.set_REF_B210A, **NOT** self.REF_B210A
```

(4.A) Add a parameter `ref_usrp` to the `__init__(...)` method of 'MyEPyBlock' [Code], and
(4.B) Initialize property `self.ref_usrp = ref_usrp` (so GRC will create a callback):

```python
class MyEPyBlock(gr.basic_block):
# ... other code ...
def __init__(self,
# other params ...
ref_usrp = 0,
# ... more params
):
self.ref_usrp = ref_usrp # creates callbacks in flowgraph
# ... more initializion
...
```

(5) After editing the code, the Python Block will have a parameter 'Ref_Usrp' ...
Set the value of 'Ref_Usrp' to REF_USRP (the variable name)

(6) 'MyEPyBlock' code can now access the referenced usrp:

```python
# a function called by `work(...)` or a message handler in 'MyEPyBlock':
def my_usrp_info() -> str:
uhd_msg_str: str = 'NOTHING YET'
if self.ref_usrp:
usrp_tm: float = (_t_ := self.ref_usrp.get_time_now()).get_real_secs()
usrp_last_pps: float = self.ref_usrp.get_time_last_pps().get_real_secs()
usrp_cr: float = self.ref_usrp.get_clock_rate()
usrp_ticks: int = _t_.to_ticks(usrp_tm)
uhd_msg_str = (f"snippetMStart_get_USRP_info:\n"
f"usrp_tm={usrp_tm} (ticks={usrp_ticks})\nusrp_last_pps={usrp_last_pps}\nusrp_cr={usrp_cr}")
return uhd_msg_str
```

Cheers,

- W

No comments:

Post a Comment