From 7e71f19b6ccdef59a882f4bc66a2b5f02bbda29f Mon Sep 17 00:00:00 2001
From: abeinder <abeinder@iastate.edu>
Date: Mon, 17 Apr 2023 22:24:33 -0500
Subject: [PATCH] current working version

---
 pycrocart/ParameterWindow.py     | 13 +++++++++----
 pycrocart/SetpointHandler.py     |  8 ++++----
 pycrocart/SetpointMenu.py        |  1 +
 pycrocart/logging_variables.json |  9 ++-------
 pycrocart/mp4params.json         | 26 +++++++++++++-------------
 pycrocart/uCartCommander.py      |  6 +++---
 6 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/pycrocart/ParameterWindow.py b/pycrocart/ParameterWindow.py
index 30a1db407..36356fae9 100644
--- a/pycrocart/ParameterWindow.py
+++ b/pycrocart/ParameterWindow.py
@@ -215,12 +215,15 @@ class ParameterWindow(QWidget):
     def on_send(self):
 
         if not self.sending:
-            self.sending = True
-            self.complete_label.setEnabled(False)
 
             try:
                 with open('./mp4params.json', 'r') as f:
                     contents = json.load(f)
+
+                self.sending = True
+                self.complete_label.setEnabled(False)
+                self.complete_label.setText("Complete ✓")
+
                 for key in contents:
                     for sub_key in contents[key]:
                         self.num_to_send += 1
@@ -229,9 +232,11 @@ class ParameterWindow(QWidget):
                                                 'value': contents[key][sub_key]})
 
                 self.timer.timeout.connect(self.send_callback)
-                self.timer.start(100)
+                self.timer.start(300)
             except json.decoder.JSONDecodeError:
-                print("File open")
+                error_text = "Malformed Json"
+                self.complete_label.setText(
+                    "<span style='color: red;'>" + error_text + "</span>")
 
 
     def send_callback(self):
diff --git a/pycrocart/SetpointHandler.py b/pycrocart/SetpointHandler.py
index 711504189..a16c450fe 100644
--- a/pycrocart/SetpointHandler.py
+++ b/pycrocart/SetpointHandler.py
@@ -94,11 +94,11 @@ class SetpointHandler:
 
             thrust = self.setpoint.thrust * 65000 / 100
             print(f"Set attitude: {self.setpoint.yaw}, {self.setpoint.pitch}, "
-                  f"{self.setpoint.roll}, {self.setpoint.thrust}")
+                  f"{self.setpoint.roll}, {thrust}")
 
             self.commander.send_attitude_setpoint(
                 self.setpoint.yaw, self.setpoint.pitch, self.setpoint.roll, 
-                int(thrust))
+                thrust)
 
         elif self._flight_mode == FlightMode.ATTITUDE_RATE_TYPE:
 
@@ -109,7 +109,7 @@ class SetpointHandler:
 
             self.commander.send_attitude_rate_setpoint(
                 self.setpoint.yaw, self.setpoint.pitch, self.setpoint.roll,
-                int(thrust))
+                thrust)
 
         self.setpoint_semaphore.release()
 
@@ -134,4 +134,4 @@ class SetpointHandler:
 
             self.commander.send_attitude_rate_setpoint(
                 self.setpoint.yaw, self.setpoint.pitch, self.setpoint.roll,
-                int(thrust))
\ No newline at end of file
+                thrust)
diff --git a/pycrocart/SetpointMenu.py b/pycrocart/SetpointMenu.py
index 15a815746..1d36456da 100644
--- a/pycrocart/SetpointMenu.py
+++ b/pycrocart/SetpointMenu.py
@@ -209,5 +209,6 @@ class SetpointMenu(QWidget):
         self.roll_box.setText(str(round(self.setpoint.roll, 2)))
         self.pitch_box.setText(str(round(self.setpoint.pitch, 2)))
         self.thrust_slider.setValue(int(round(self.setpoint.thrust, 2)))
+        self.send_setpoint()
 
         self.setpoint_semaphore.release()
diff --git a/pycrocart/logging_variables.json b/pycrocart/logging_variables.json
index 2f108231a..031cfdfbd 100644
--- a/pycrocart/logging_variables.json
+++ b/pycrocart/logging_variables.json
@@ -1,12 +1,7 @@
 {
     "group1":
     {
-        "update_frequency_ms": 21,
-        "vars": ["stateEstimate.pitch", "stateEstimate.roll", "stateEstimate.yaw"]
-    },
-    "group2":
-    {
-        "update_frequency_ms": 100,
-        "vars": ["health.motorVarXM1"]
+        "update_frequency_ms": 50,
+        "vars": ["ctrlStdnt.r_roll", "ctrlStdnt.rollRate", "stateEstimate.roll", "ctrlStdnt.roll"]
     }
 }
\ No newline at end of file
diff --git a/pycrocart/mp4params.json b/pycrocart/mp4params.json
index 5a470795b..0baf5fd96 100644
--- a/pycrocart/mp4params.json
+++ b/pycrocart/mp4params.json
@@ -5,26 +5,26 @@
     },
     "s_pid_rate":
     {
-        "roll_kp" : 0,
-        "roll_ki" : 0,
+        "roll_kp" : 300,
+        "roll_ki" : 40,
         "roll_kd" : 0,
-        "pitch_kp": 0,
-        "pitch_ki": 0,
+        "pitch_kp": 200,
+        "pitch_ki": 40,
         "pitch_kd": 0,
-        "yaw_kp"  : 0,
+        "yaw_kp"  : 2000,
         "yaw_ki"  : 0,
         "yaw_kd"  : 0
     },
     "s_pid_attitude":
     {
-        "roll_kp" : 0,
-        "roll_ki" : 0,
-        "roll_kd" : 0,
-        "pitch_kp": 0,
-        "pitch_ki": 0,
-        "pitch_kd": 0,
-        "yaw_kp"  : 0,
+        "roll_kp" : 15,
+        "roll_ki" : 10,
+        "roll_kd" : 3,
+        "pitch_kp": 15,
+        "pitch_ki": 10,
+        "pitch_kd": 3,
+        "yaw_kp"  : 10,
         "yaw_ki"  : 0,
-        "yaw_kd"  : 0,
+        "yaw_kd"  : 0
     }
 }
\ No newline at end of file
diff --git a/pycrocart/uCartCommander.py b/pycrocart/uCartCommander.py
index a5b91ef56..a2f13a078 100644
--- a/pycrocart/uCartCommander.py
+++ b/pycrocart/uCartCommander.py
@@ -208,9 +208,9 @@ class Commander:
         pk = CRTPPacket()
         pk.port = CRTPPort.COMMANDER_GENERIC
         pk.channel = SET_SETPOINT_CHANNEL
-        pk.data = struct.pack('<Bffff', ATTITUDE_RATE_TYPE, yawRate,
+        pk.data = struct.pack('<Bffff', ATTITUDE_RATE_TYPE, rollRate,
                               pitchRate,
-                              rollRate, thrust)  # todo confirm struct is
+                              yawRate, thrust)  # todo confirm struct is
         # right, I tried to format it using H for the int of thrust and f for
         # floats otherwise. Kinda looks like what is above
         self._cf.send_packet(pk)
@@ -230,7 +230,7 @@ class Commander:
         pk = CRTPPacket()
         pk.port = CRTPPort.COMMANDER_GENERIC
         pk.channel = SET_SETPOINT_CHANNEL
-        pk.data = struct.pack('<Bffff', ATTITUDE_TYPE, yaw, pitch, roll,
+        pk.data = struct.pack('<Bffff', ATTITUDE_TYPE, roll, pitch, yaw,
          thrust)
         self._cf.send_packet(pk)  # todo confirm struct is right , I tried to
         # format it using H for the int of thrust and f for
-- 
GitLab