technocreatives / dbc-codegen

Generate Rust structs for messages from a dbc (CAN bus definition) file.
Apache License 2.0
43 stars 22 forks source link

Add methods to get signal min and max values #37

Closed andresv closed 3 years ago

andresv commented 3 years ago

For example here we have steering angle that goes from -500 to 500.

BO_ 37 STEER_ANGLE_SENSOR: 8 XXX
 SG_ STEER_ANGLE : 3|12@0- (1.5,0) [-500|500] "deg" XXX
 SG_ STEER_FRACTION : 39|4@0- (0.1,0) [-0.7|0.7] "deg" XXX
 SG_ STEER_RATE : 35|12@0- (1,0) [-2000|2000] "deg/s" XXX

It would be useful to access those values also from Rust. Currently they are actually hardcoded in range check.

    /// Set value of STEER_ANGLE
    #[inline(always)]
    pub fn set_steer_angle(&mut self, value: f32) -> Result<(), CanError> {
        #[cfg(feature = "range_checked")]
        if value < -500_f32 || 500_f32 < value {
            return Err(CanError::ParameterOutOfRange { message_id: 37 });
        }
        let factor = 1.5_f32;
        let offset = 0_f32;
        let value = ((value - offset) / factor) as i16;

        let value = u16::from_ne_bytes(value.to_ne_bytes());
        self.raw.view_bits_mut::<Msb0>()[4..16].store_be(value);
        Ok(())
    }
marcelbuesing commented 3 years ago

I think that's a great idea, probably add them as associated constants? Then also use those in the range check code.

pub const STEER_ANGLE_MIN: f32 = -500;
pub const STEER_ANGLE_MAX: f32 = 500;
andresv commented 3 years ago

Yes, that is better than adding separate methods.

andresv commented 3 years ago

https://github.com/technocreatives/dbc-codegen/pull/38