tact1m4n3 / crsf-rs

A work in progress crsf protocol packet parser
MIT License
10 stars 7 forks source link

Small question. #1

Closed muttering-oldman closed 7 months ago

muttering-oldman commented 7 months ago

Hi! Thanks for this library! After running through it with debug I have a better understanding of crsf. But I have a small question. In the map_chan function I see some "magic" numbers. Could you tell me a little about them? Or maybe you can give me links to articles about crsf parsing?

const fn map_chan(x: u16) -> u16 {
    (1000 + (((x as u32).saturating_sub(191)) * (2000 - 1000) * 2 / (1792 - 191) + 1) / 2) as u16
}
muttering-oldman commented 7 months ago

In Betaflight I found a function with some comments explaining the "magic" numbers. Maybe it will be useful for someone. https://github.com/betaflight/betaflight/blob/master/src/main/rx/crsf.c

STATIC_UNIT_TESTED float crsfReadRawRC(const rxRuntimeState_t *rxRuntimeState, uint8_t chan)
{
    UNUSED(rxRuntimeState);
    if (channelScale == CRSF_RC_CHANNEL_SCALE_LEGACY) {
        /* conversion from RC value to PWM
        * for 0x16 RC frame
        *       RC     PWM
        * min  172 ->  988us
        * mid  992 -> 1500us
        * max 1811 -> 2012us
        * scale factor = (2012-988) / (1811-172) = 0.62477120195241
        * offset = 988 - 172 * 0.62477120195241 = 880.53935326418548
        */
        return (channelScale * (float)crsfChannelData[chan]) + 881;
    } else {
        /* conversion from RC value to PWM
        * for 0x17 Subset RC frame
        */
        return (channelScale * (float)crsfChannelData[chan]) + 988;
    }
}
tact1m4n3 commented 7 months ago

Hi! Sorry for the confusion. I mapped those values this way because that's how betaflight does it. Now that I think about it, it might be better to return the raw values and leave the mapping to the user.

muttering-oldman commented 7 months ago

I think you can do two methods. One can return raw values, and the second can return the values as they are now.

tact1m4n3 commented 6 months ago

I just released a new version with a better mapping system :))