Skip to content

Commit

Permalink
Implement PWM output for aux ports
Browse files Browse the repository at this point in the history
Capable pins can now be configured as a PWM output.  After doing so,
either a "aux[12] pwm" diagnostic mode command or a register write can
be used to set the current PWM value.

Add support in the python and C++ libraries (and add gpio support in
the C++ library while we're at it).
  • Loading branch information
jpieper committed Sep 13, 2024
1 parent 9bafb87 commit f1fb3f7
Show file tree
Hide file tree
Showing 11 changed files with 711 additions and 69 deletions.
108 changes: 71 additions & 37 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,25 @@ The following table shows which pins can be used for the unique capabilities:

#### AUX1 / ENC ####

| moteus r4.5/8/11 | Con | Aux | SPI | ADC/Sin/Cos | I2C | HW Quad | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|---------|------|-----|
| 3.3V (3) | 1 | | | | | | | |
| C | 2 | 0 | X | | | | | X |
| GND (G) | 3 | | | | | | | |
| K | 4 | 1 | CLK | X | | | | |
| I | 5 | 2 | MISO | X | | | | |
| O | 6 | 3 | MOSI | X | | | | |

| moteus n1/c1 | Con | AUX | SPI | ADC/Sin/Cos | I2C | HW Quad | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|---------|------|-----|
| 5V (5) | 1 | | | | | | | |
| 3.3V (3) | 2 | | | | | | | |
| A | 3 | 0 | CLK | X | | | | |
| B * | 4 | 1 | MISO | | | 3.1 | RX | |
| C | 5 | 2 | MOSI | X | | 3.2 | | |
| D | 6 | 3 | | | SCL | 2.1 | RX | X |
| E | 7 | 4 | | | SDA | 2.2 | TX | X |
| GND (G) | 8 | | | | | | | |
| moteus r4.5/8/11 | Con | Aux | SPI | ADC/Sin/Cos | I2C | HW Quad/PWM | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|-------------|------|-----|
| 3.3V (3) | 1 | | | | | | | |
| C | 2 | 0 | X | | | | | X |
| GND (G) | 3 | | | | | | | |
| K | 4 | 1 | CLK | X | | | | |
| I | 5 | 2 | MISO | X | | | | |
| O | 6 | 3 | MOSI | X | | | | |

| moteus n1/c1 | Con | AUX | SPI | ADC/Sin/Cos | I2C | HW Quad/PWM | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|-------------|------|-----|
| 5V (5) | 1 | | | | | | | |
| 3.3V (3) | 2 | | | | | | | |
| A | 3 | 0 | CLK | X | | | | |
| B * | 4 | 1 | MISO | | | 3.1 | RX | |
| C | 5 | 2 | MOSI | X | | 3.2 | | |
| D | 6 | 3 | | | SCL | 2.1 | RX | X |
| E | 7 | 4 | | | SDA | 2.2 | TX | X |
| GND (G) | 8 | | | | | | | |

NOTE: For moteus n1, the B pin software configured pullup cannot be
used effectively. Thus the B pin is unsuitable for open-drain inputs
Expand All @@ -304,24 +304,24 @@ pullups are not available on moteus-c1 for aux1.

#### AUX2 / ABS ####

| moteus r4.5/8/11 | Con | Aux | SPI | ADC/Sin/Cos | I2C | HW Quad | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|---------|------|-----|
| 3.3V (3) | 1 | | | | | | | |
| | 2 | 0 | | | SCL | | RX | X |
| | 3 | 1 | | | SDA | | TX | X |
| GND (G) | 4 | | | | | | | |
| DBG 1 | | 2 | | | | | | X |
| DBG 2 | | 3 | | | | | | X |

| moteus n1/c1 | Con | AUX | SPI | ADC/Sin/Cos | I2C | HW Quad | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|---------|------|-----|
| 5V (5) | 1 | | | | | | | |
| 3.3V (3) | 2 | | | | | | | |
| A | 3 | 0 | CLK | X | | | | X |
| B | 4 | 1 | MISO | X | SDA | | RX | X |
| C | 5 | 2 | MOSI | X | SCL | 4.1 | TX | X |
| D | 6 | 3 | | | | 4.2 | RX | X |
| GND (G) | 7 | | | | | | | |
| moteus r4.5/8/11 | Con | Aux | SPI | ADC/Sin/Cos | I2C | HW Quad/PWM | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|-------------|------|-----|
| 3.3V (3) | 1 | | | | | | | |
| | 2 | 0 | | | SCL | | RX | X |
| | 3 | 1 | | | SDA | | TX | X |
| GND (G) | 4 | | | | | | | |
| DBG 1 | | 2 | | | | | | X |
| DBG 2 | | 3 | | | | | | X |

| moteus n1/c1 | Con | AUX | SPI | ADC/Sin/Cos | I2C | HW Quad/PWM | UART | 5VT |
|------------------|-----|-----|------|-------------|-----|-------------|------|-----|
| 5V (5) | 1 | | | | | | | |
| 3.3V (3) | 2 | | | | | | | |
| A | 3 | 0 | CLK | X | | | | X |
| B | 4 | 1 | MISO | X | SDA | | RX | X |
| C | 5 | 2 | MOSI | X | SCL | 4.1 | TX | X |
| D | 6 | 3 | | | | 4.2 | RX | X |
| GND (G) | 7 | | | | | | | |

NOTE: For moteus r4.5/8/11, DBG 1/2 are not present on the ABS
connector, but are exposed pads on the circuit board.
Expand Down Expand Up @@ -1160,6 +1160,19 @@ WARNING: Changing the speed affects all processes driven by the
microcontroller, including CAN communication. Thus setting this to a
non-zero value may prevent future CAN communications.

#### 0x076/0x07a - Aux1 PWM Outputs ####

Mode: Read/write

The current output PWM value for the given pin on Aux1. PWM mapping
is used for integral types.

#### 0x07b/0x07f - Aux2 PWM Outputs ####

Mode: Read/write

The current output PWM value for the given pin on Aux1. PWM mapping
is used for integral types.

#### 0x100 - Model Number ####

Expand Down Expand Up @@ -1533,6 +1546,17 @@ aux1 out <data>
'data' is a single decimal integer. Only bits associated with pins
configured as digital outputs are used, the remainder are ignored.

### `aux1 pwm` - Set PWM Output Values ###

```
aux1 pwm <pin> <value>
```

'pin' is a number from 0 to 4 that gives the pin to set.

'value' is a floating point value between 0.0 and 1.0 that gives the
output duty cycle for the pin.

### `aux1 ic-cmd` - Initiate an iC-PZ command ###

```
Expand Down Expand Up @@ -2109,6 +2133,16 @@ done with 12 bits, so 2048 would be exactly 0.5 * 3.3V. However, it
is best to calibrate this with actual readings as observed over the
diagnostic protocol for optimal performance.

## `aux[12].i2c_startup_delay_ms` ##

A delay in milliseconds after power-on (or upon reconfiguring), before
I2C devices associated with this auxiliary port are first used.

## `aux[12].pwm_period_us` ##

The period in microseconds to be used for PWM outputs on this
auxiliary port.

## `motor_position.sources.X.aux_number` ##

1 for an aux1 device, or 2 for an aux2 device.
Expand Down
1 change: 1 addition & 0 deletions fw/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cc_library(
"simple_pi.h",
"torque_model.h",
"stm32_i2c_timing.h",
"strtof.h",
],
srcs = [
"foc.cc",
Expand Down
8 changes: 8 additions & 0 deletions fw/aux_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ struct Pin {
kDigitalInput,
kDigitalOutput,
kAnalogInput,
kPwmOut,

kLength,
};
Expand Down Expand Up @@ -358,6 +359,7 @@ struct AuxConfig {
aux::Index::Config index;
aux::SineCosine::Config sine_cosine;
int32_t i2c_startup_delay_ms = 30;
int32_t pwm_period_us = 1000;

static constexpr size_t kNumPins = 5;
std::array<Pin, kNumPins> pins = { {} };
Expand All @@ -372,6 +374,7 @@ struct AuxConfig {
a->Visit(MJ_NVP(index));
a->Visit(MJ_NVP(sine_cosine));
a->Visit(MJ_NVP(i2c_startup_delay_ms));
a->Visit(MJ_NVP(pwm_period_us));
a->Visit(MJ_NVP(pins));
}
};
Expand All @@ -388,6 +391,7 @@ enum class AuxError {
kAdcPinError,
kSineCosinePinError,
kUartPinError,
kPwmPinError,

kLength,
};
Expand All @@ -408,6 +412,7 @@ struct AuxStatus {

uint8_t analog_bit_active = 0;
std::array<float, 5> analog_inputs = { {} };
std::array<float, 5> pwm = { {} };

// Increases anytime the configuration changes.
uint8_t epoch = 0;
Expand All @@ -426,6 +431,7 @@ struct AuxStatus {
a->Visit(MJ_NVP(pins));
a->Visit(MJ_NVP(analog_bit_active));
a->Visit(MJ_NVP(analog_inputs));
a->Visit(MJ_NVP(pwm));
a->Visit(MJ_NVP(epoch));
}
};
Expand Down Expand Up @@ -524,6 +530,7 @@ struct IsEnum<moteus::aux::Pin::Mode> {
{ P::kDigitalInput, "digital_in" },
{ P::kDigitalOutput, "digital_out" },
{ P::kAnalogInput, "analog_in" },
{ P::kPwmOut, "pwm_out" },
}};
}
};
Expand Down Expand Up @@ -563,6 +570,7 @@ struct IsEnum<moteus::aux::AuxError> {
{ A::kAdcPinError, "adc_pin_error" },
{ A::kSineCosinePinError, "sine_cosine_pin_error" },
{ A::kUartPinError, "uart_pin_error" },
{ A::kPwmPinError, "pwm_pin_error" },
}};
}
};
Expand Down
Loading

0 comments on commit f1fb3f7

Please sign in to comment.