change CLAMP macro to an inline function

This commit is contained in:
RowanC 2026-05-25 22:11:42 +01:00
parent 6725e53d45
commit b268cda633

View file

@ -18,14 +18,16 @@
#include "nrai_CAN_API.h"
#define CLAMP(_value,_min,_max) \
do { \
if (_value < _min){ \
_value = _min; \
} else if (_value > _max){ \
_value =_max; \
} \
} while(0)
static inline int32_t
CLAMP(int32_t value, int32_t min, int32_t max)
{
if (value < min){
return min;
} else if (value > max){
return max;
}
return value;
}
NRAI_FT int32_t
nrai_can_unpack_VCU2AI_Status(struct nrai_can_ai_read * s, struct can_frame * f)
@ -522,7 +524,7 @@ nrai_can_mkframe_AI2VCU_Steer( struct nrai_can_ai_write * s, struct can_frame *
f->len = 2;
tmp = s->STEER_REQUEST;
CLAMP(tmp,-210,210);
tmp = CLAMP(tmp,-210,210);
f->data[0] = (tmp & 0x00FF);
f->data[1] = (((uint16_t)tmp >> 8) & 0x00FF);
@ -540,11 +542,11 @@ nrai_can_mkframe_AI2VCU_Brake(struct nrai_can_ai_write * s, struct can_frame * f
f->len = 2;
tmp = s->HYD_PRESS_F_REQ_pct;
CLAMP(tmp,0,100);
tmp = CLAMP(tmp,0,100);
f->data[0] = tmp;
tmp = s->HYD_PRESS_R_REQ_pct;
CLAMP(tmp,0,100);
tmp = CLAMP(tmp,0,100);
f->data[1] = tmp;
return 0;
}
@ -560,12 +562,12 @@ nrai_can_mkframe_AI2VCU_Drive_F(struct nrai_can_ai_write * s, struct can_frame *
f->len = 4;
tmp = s->FRONT_AXLE_TRQ_REQUEST;
CLAMP(tmp,0,1950);
tmp = CLAMP(tmp,0,1950);
f->data[0] = (tmp & 0xFF);
f->data[1] = ((tmp >> 8) & 0xFF);
tmp = s->FRONT_MOTOR_SPEED_MAX;
CLAMP(tmp,0,4000);
tmp = CLAMP(tmp,0,4000);
f->data[2] = (tmp & 0xFF);
f->data[3] = ((tmp >> 8) & 0xFF);
return 0;
@ -582,12 +584,12 @@ nrai_can_mkframe_AI2VCU_Drive_R(struct nrai_can_ai_write * s, struct can_frame *
f->len = 4;
tmp = s->REAR_AXLE_TRQ_REQUEST;
CLAMP(tmp,0,1950);
tmp = CLAMP(tmp,0,1950);
f->data[0] = (tmp & 0xFF);
f->data[1] = ((tmp >> 8) & 0xFF);
tmp = s->REAR_MOTOR_SPEED_MAX;
CLAMP(tmp,0,4000);
tmp = CLAMP(tmp,0,4000);
f->data[2] = (tmp & 0xFF);
f->data[3] = ((tmp >> 8) & 0xFF);
return 0;