From 5b3136476510bde919ceac7a36d1ddb0a31e8b99 Mon Sep 17 00:00:00 2001
From: C-Glick <colton.glick@gmail.com>
Date: Wed, 16 Mar 2022 17:45:03 -0500
Subject: [PATCH] Added custom attitude setpoint packet, ID 9

---
 .../src/modules/src/crtp_commander_generic.c  | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/crtp_commander_generic.c b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/crtp_commander_generic.c
index 9d038cc5c..a4b1f55c4 100644
--- a/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/crtp_commander_generic.c
+++ b/crazyflie_software/crazyflie-firmware-2021.06/src/modules/src/crtp_commander_generic.c
@@ -72,6 +72,7 @@ enum packet_type {
   fullStateType     = 6,
   positionType      = 7,
   attitudeRateType  = 8,
+  attitudeType      = 9,
 };
 
 /* ---===== 2 - Decoding functions =====--- */
@@ -399,6 +400,37 @@ static void attitudeRateDecoder(setpoint_t *setpoint, uint8_t type, const void *
   setpoint->thrust = values->thrust;
 }
 
+/*
+ * Custom Attitude decoder, bypasses the normal attitude control path,
+ * packet contains the desired attitude angle in addition to the thrust value
+ */
+struct attitudePacket_s{
+  float rollAngle;   // deg
+  float pitchAngle;  // deg
+  float yawAngle;    // deg
+  float thrust;     // thrust percentage 0 - 60,000 
+} __attribute__((packed));
+static void attitudeDecoder(setpoint_t *setpoint, uint8_t type, const void *data, size_t datalen){
+  
+  const struct attitudePacket_s *values = data;
+
+  ASSERT(datalen == sizeof(struct attitudePacket_s));
+
+  setpoint->mode.x = modeDisable;
+  setpoint->mode.y = modeDisable;
+  setpoint->mode.z = modeDisable;
+
+  setpoint->mode.roll = modeAbs;
+  setpoint->mode.pitch = modeAbs;
+  setpoint->mode.yaw = modeAbs;
+  
+  setpoint->attitude.roll = values->rollAngle;
+  setpoint->attitude.pitch = values->pitchAngle;
+  setpoint->attitude.yaw = values->yawAngle;
+  
+  setpoint->thrust = values->thrust;
+}
+
  /* ---===== 3 - packetDecoders array =====--- */
 const static packetDecoder_t packetDecoders[] = {
   [stopType]          = stopDecoder,
@@ -410,6 +442,7 @@ const static packetDecoder_t packetDecoders[] = {
   [fullStateType]     = fullStateDecoder,
   [positionType]      = positionDecoder,
   [attitudeRateType]  = attitudeRateDecoder,
+  [attitudeType]      = attitudeDecoder,
 };
 
 /* Decoder switch */
-- 
GitLab