From c176bb40a2d5f047cde74e82569f096a04f6355c Mon Sep 17 00:00:00 2001 From: Michael Ehrenreich Date: Tue, 1 Sep 2020 23:35:25 +0200 Subject: [PATCH 1/2] Shrink structs, make fields unsigned where no sign needed This reduces sizeof(Command) from 40 to 32 and sizeof(Feedback) from 44 to 34 --- protocol.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/protocol.h b/protocol.h index 35cde1e..2ff94cf 100644 --- a/protocol.h +++ b/protocol.h @@ -22,11 +22,11 @@ struct MotorState { int16_t pwm = 0; ControlType ctrlTyp = ControlType::FieldOrientedControl; ControlMode ctrlMod = ControlMode::OpenMode; - int16_t iMotMax = 15; // [A] Maximum motor current limit - int16_t iDcMax = 17; // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) - int16_t nMotMax = 1000; // [rpm] Maximum motor speed limit - int16_t fieldWeakMax = 10; // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. - int16_t phaseAdvMax = 40; // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. + uint8_t iMotMax = 15; // [A] Maximum motor current limit + uint8_t iDcMax = 17; // [A] Maximum DC Link current limit (This is the final current protection. Above this value, current chopping is applied. To avoid this make sure that I_DC_MAX = I_MOT_MAX + 2A) + uint16_t nMotMax = 1000; // [rpm] Maximum motor speed limit + uint8_t fieldWeakMax = 10; // [A] Maximum Field Weakening D axis current (only for FOC). Higher current results in higher maximum speed. + uint8_t phaseAdvMax = 40; // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. }; uint16_t calculateChecksum(MotorState state) { @@ -81,7 +81,7 @@ struct MotorFeedback { int16_t speed = 0; uint8_t error = 0; int16_t current = 0; - uint32_t chops = 0; + uint16_t chops = 0; bool hallA = false, hallB = false, hallC = false; -- 2.50.1 From dfa9e2178e2f6000d9ca864448fc608f72c46f5d Mon Sep 17 00:00:00 2001 From: Michael Ehrenreich Date: Sun, 25 Oct 2020 01:55:16 +0200 Subject: [PATCH 2/2] Add struct layout assertions --- protocol.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/protocol.h b/protocol.h index 2ff94cf..bd918a0 100644 --- a/protocol.h +++ b/protocol.h @@ -119,4 +119,67 @@ uint16_t calculateChecksum(Feedback feedback) { feedback.timeoutCntSerial; } + +#define ASSERT_LAYOUT(st, memb, off) \ + static_assert(offsetof(st, memb) == off, "struct layout wrong"); + +ASSERT_LAYOUT(Feedback, start, 0); +ASSERT_LAYOUT(Feedback, left, 2); +ASSERT_LAYOUT(Feedback, left.angle, 2); +ASSERT_LAYOUT(Feedback, left.speed, 4); +ASSERT_LAYOUT(Feedback, left.error, 6); +ASSERT_LAYOUT(Feedback, left.current, 8); +ASSERT_LAYOUT(Feedback, left.chops, 10); +ASSERT_LAYOUT(Feedback, left.hallA, 12); +ASSERT_LAYOUT(Feedback, left.hallB, 13); +ASSERT_LAYOUT(Feedback, left.hallC, 14); + +ASSERT_LAYOUT(Feedback, right, 16); +ASSERT_LAYOUT(Feedback, right.angle, 16); +ASSERT_LAYOUT(Feedback, right.speed, 18); +ASSERT_LAYOUT(Feedback, right.error, 20); +ASSERT_LAYOUT(Feedback, right.current, 22); +ASSERT_LAYOUT(Feedback, right.chops, 24); +ASSERT_LAYOUT(Feedback, right.hallA, 26); +ASSERT_LAYOUT(Feedback, right.hallB, 27); +ASSERT_LAYOUT(Feedback, right.hallC, 28); + +ASSERT_LAYOUT(Feedback, batVoltage, 30); +ASSERT_LAYOUT(Feedback, boardTemp, 32); +ASSERT_LAYOUT(Feedback, timeoutCntSerial, 34); +ASSERT_LAYOUT(Feedback, checksum, 36); + +static_assert(sizeof(Command) == 32, "sizeof(Command) wrong"); + +ASSERT_LAYOUT(Command, start, 0); + +ASSERT_LAYOUT(Command, left, 2); +ASSERT_LAYOUT(Command, left.enable, 2); +ASSERT_LAYOUT(Command, left.pwm, 4); +ASSERT_LAYOUT(Command, left.ctrlTyp, 6); +ASSERT_LAYOUT(Command, left.ctrlMod, 7); +ASSERT_LAYOUT(Command, left.iMotMax, 8); +ASSERT_LAYOUT(Command, left.iDcMax, 9); +ASSERT_LAYOUT(Command, left.nMotMax, 10); +ASSERT_LAYOUT(Command, left.fieldWeakMax, 12); +ASSERT_LAYOUT(Command, left.phaseAdvMax, 13); + +ASSERT_LAYOUT(Command, right.enable, 14); +ASSERT_LAYOUT(Command, right.pwm, 16); +ASSERT_LAYOUT(Command, right.ctrlTyp, 18); +ASSERT_LAYOUT(Command, right.ctrlMod, 19); +ASSERT_LAYOUT(Command, right.iMotMax, 20); +ASSERT_LAYOUT(Command, right.iDcMax, 21); +ASSERT_LAYOUT(Command, right.nMotMax, 22); +ASSERT_LAYOUT(Command, right.fieldWeakMax, 24); +ASSERT_LAYOUT(Command, right.phaseAdvMax, 25); + +ASSERT_LAYOUT(Command, buzzer, 26); +ASSERT_LAYOUT(Command, buzzer.freq, 26); +ASSERT_LAYOUT(Command, buzzer.pattern, 27); + +ASSERT_LAYOUT(Command, poweroff, 28); +ASSERT_LAYOUT(Command, led, 29); +ASSERT_LAYOUT(Command, checksum, 30); + } -- 2.50.1