From e527eee5b14f84ba608a82cef94b2fbc9be44650 Mon Sep 17 00:00:00 2001
From: stedif <stefano.difrischia@inaf.it>
Date: Fri, 1 Apr 2022 12:50:13 +0200
Subject: [PATCH] L2SS-643: add calculate_bf_weights function

---
 .../devices/sdp/beamlet.py                    | 22 ++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/beamlet.py b/tangostationcontrol/tangostationcontrol/devices/sdp/beamlet.py
index dbbc64436..35d484139 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/beamlet.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/beamlet.py
@@ -92,14 +92,6 @@ class Beamlet(opcua_device):
     FPGA_beamlet_output_scale_R = attribute_wrapper(comms_annotation=["FPGA_beamlet_output_scale_R"], datatype=numpy.double, dims=(16,))
     FPGA_beamlet_output_scale_RW = attribute_wrapper(comms_annotation=["FPGA_beamlet_output_scale_RW"], datatype=numpy.double, dims=(16,), access=AttrWriteType.READ_WRITE)
 
-    # List of OPC-UA CP for BF beamlets
-    S_pn = SDP.S_pn
-    N_pn = SDP.N_pn
-    A_pn = 6
-    N_pol = 2
-    N_beamlets_ctrl = 488
-    N_pol_bf = 2
-
     FPGA_beamlet_output_nof_packets_R = attribute_wrapper(comms_annotation=["FPGA_beamlet_output_nof_packets_R"], datatype=numpy.int32, dims=(N_beamlets_ctrl, N_pn))
     FPGA_beamlet_output_nof_valid_R = attribute_wrapper(comms_annotation=["FPGA_beamlet_output_nof_valid_R"], datatype=numpy.int32, dims=(N_beamlets_ctrl, N_pn))
 
@@ -152,8 +144,18 @@ class Beamlet(opcua_device):
     # internal functions
     # --------
     def _calculate_bf_weights(self, phases: numpy.ndarray):
-        pass
-
+        """ Helper function that converts a difference in phase (in radiants) 
+        to a FPGA weight (in complex number) """
+        
+        # Convert array values in complex numbers
+        unit = numpy.power(2,14)
+        real = numpy.array(unit * numpy.sin(phases), dtype=numpy.short)
+        imag = numpy.array(unit * numpy.cos(phases), dtype=numpy.short)
+        # join 16 bits of imaginary part (MSB) with 16 bits of real part (LSB)
+        bf_weights = numpy.array( numpy.frombuffer( b''.join(imag,real), dtype=numpy.uint32 ) )
+        
+        return bf_weights
+        
     # --------
     # Commands
     # --------
-- 
GitLab