Skip to content

Commit a5be998

Browse files
committed
fix: sero trim works
1 parent 677da94 commit a5be998

3 files changed

Lines changed: 24 additions & 41 deletions

File tree

service.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ configuration:
3434
- name: servo-scaler
3535
type: number
3636
tunable: true
37-
value: 1
37+
value: 0.9
3838

3939
- name: servo-trim
4040
type: number
4141
tunable: true
42-
value: 0
42+
value: 0.0
4343

4444
- name: fan-cap
4545
type: number

src/drivers/pca9685/init.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ func NewPCA9685Controller(address uint8, bus uint) (*PCA9685Controller, error) {
6363
jumpTable := map[ActuationIndex]channelConfig{
6464
ActuationIndex(Steer): {
6565
InUse: true,
66-
MinPulseFrac: 225,
67-
MaxPulseFrac: 385,
68-
Trim: 1,
66+
MinPulseFrac: 205, // 1ms up
67+
MaxPulseFrac: 410, // 2ms up
68+
Trim: 0,
6969
},
7070
ActuationIndex(LeftThrottle): {
7171
InUse: true,
@@ -148,30 +148,6 @@ func abs(value float64) float64 {
148148
return value
149149
}
150150

151-
// Update the frequency at index @p channel to @p value using the trim value
152-
func (pc *PCA9685Controller) SetChannelWithTrim(channel ActuationIndex, value float64) error {
153-
154-
// Calculate duty cycles range
155-
trim := pc.jumpTable[channel].Trim
156-
maxDuty := pc.jumpTable[channel].MaxPulseFrac
157-
minDuty := pc.jumpTable[channel].MinPulseFrac
158-
159-
// log.Debug().float64("trimBefore", trim).float64("valueBefore", value).Msg("asdf")
160-
161-
if trim > 0 {
162-
maxDuty = maxDuty + (abs(trim) * travelExtender)
163-
} else if trim < 0 {
164-
minDuty = minDuty - (abs(trim) * travelExtender)
165-
}
166-
167-
dutyRange := maxDuty - minDuty
168-
dutyCycle := int(minDuty + (dutyRange * value))
169-
170-
// log.Debug().float64("trim", trim).float64("value", value).float64("dutyRange", dutyRange).float64("maxDuty", maxDuty).float64("minDuty", minDuty).Msg("SetChannelWithTrim")
171-
172-
return pc.pca.SetChannel(int(channel), 0, dutyCycle)
173-
}
174-
175151
// AllOff sets all channels to their midpoints)
176152
func (pc *PCA9685Controller) AllOff() {
177153
log.Info().Msg("Turning off all channels")

src/drivers/pca9685/interface.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ const servoLimiter = 0.8
88

99
// Set the servo duty. In range -1 (left) to 1(right).
1010
func (pc *PCA9685Controller) SetServo(value float64, servoScaler float64) {
11-
value = value * -servoLimiter
11+
// value = value * -servoLimiter
1212
value = clamp(value)
1313

14-
trim := pc.jumpTable[Steer].Trim
15-
if trim != 0 {
16-
value = value * abs(1.0-(trim*0.75)) * servoScaler
17-
} else {
18-
value = value * servoScaler
19-
}
14+
// Calculate the duty cycle range
15+
maxDuty := pc.jumpTable[Steer].MaxPulseFrac
16+
minDuty := pc.jumpTable[Steer].MinPulseFrac
17+
dutyRange := maxDuty - minDuty
2018

21-
value = ((value + 1) / 2)
19+
// This gets added or subtracted from the midpoint
20+
halfRange := dutyRange / 2.0
2221

23-
err := pc.SetChannelWithTrim(Steer, value)
24-
if err != nil {
25-
log.Error().Err(err).Msg("Error setting Steer value")
26-
}
22+
// We do not want to "oversteer" the servo, which might damage it
23+
// so we limit the range
24+
halfRange *= servoScaler
25+
26+
// Find the center of the range
27+
center := minDuty + (dutyRange / 2.0)
28+
servoTrim := pc.jumpTable[Steer].Trim
29+
30+
// Calculate the new duty cycle
31+
// (due to an annoying convention, we do - right and + left)
32+
dutyCycle := int(center - (halfRange * (value - servoTrim)))
33+
pc.pca.SetChannel(int(Steer), 0, dutyCycle)
2734
}
2835

2936
func (pc *PCA9685Controller) SetServoTrim(value float64) {

0 commit comments

Comments
 (0)