macformula / racecar

Monorepo for all firmware running on our custom ECUs.
https://macformula.github.io/racecar/
9 stars 2 forks source link

Split PWMOutput to PWMOutput and AnalogOutput #164

Closed BlakeFreer closed 1 week ago

BlakeFreer commented 2 months ago

PWM Output

Timeline: End of October

Background Reading

Description

PWM is a microcontroller peripheral for outputting an Analog voltage level, but it can also be used when digital signal of a specific frequency and duty cycle is required. These are fundamentally different purposes, but the PWMOutput peripheral in racecar/ is responsible for both roles. In this issue, you will separate the functionality into 2 classes.

Current State

See firmware/shared/periph/pwm.h

There are 4 methods on the PWM class:

class PWMOutput : public util::Peripheral {
public:
    virtual void Start() = 0;
    virtual void Stop() = 0;
    virtual void SetDutyCycle(float duty_cycle) = 0;
    virtual float GetDutyCycle() = 0;
};

Start and Stop control whether the output is active, while SetDutyCycle changes the duty cycle and GetDutyCycle returns it. This should be broken into 2 different classes

Class 1: PWMOutput

This should be identical to the current PWMOutput but with an added SetFrequency and GetFrequency method which operates on floating-point frequencies measured in Hertz.

Class 2: AnalogOutput

This class is much different from the existing PWMOutput class. It has only 1 method called Set which takes a floating point voltage argument.

Task

Stage 1: Prototype

  1. Create a new header file firmware/shared/periph/analog_output.h to define the AnalogOutput abstract class.
  2. Edit the existing shared/.../pwm.h class as described about
  3. Implement the classes for the CLI platform.
  4. Make a Demo project which uses your peripherals on the CLI platform.
  5. Push to a branch on GitHub

Stage 2: Implementation

After discussing your implementation, we will apply it to the other platforms and update our existing projects.

For the stm32f767 platform, both AnalogOutput and PWMOutput will use a PWM peripheral under-the-hood. This is fine: the App-Level doesn't care how the peripherals are implemented, only that they have a specific form.

floralandfaded commented 1 month ago

Do we want to use Clamper to limit the voltage between 0-5V in the Set method?

BlakeFreer commented 1 month ago

Yes there should be some clamping and it will happen at the platform level. You do not need to clamp on the cli but will need to clamp when settings the duty cycle in the stm32 peripheral. The stm runs at 3.3, not 5V

BlakeFreer commented 1 week ago

closed by #231