dotnet / dotnet/iot

MotorHat/Pca9685 is throwing an exception when initial value from PCM registers are out of range

Open
#1,887 1 comment 0 reactions 1 assignee Claimed by @pgrawehr View on GitHub
area-device-bindings bug Priority:2 up-for-grabs
Dominant language
C#
Stars
2.4k
Forks
630
Avg merge
11d 3h
Merged PRs (30d)
2

Description

**Describe the bug**

See the report here: https://stackoverflow.com/questions/72848224/iot-device-motorhat-and-raspterry-pi4

When the initial values for the duty cycle present in the Registers are outside the range the library would set them itself, an `InvalidOperationException` is thrown in `GetDutyCycle`. Since by default, the PCA9685 constructor does not set initial values (leaving them at what they were), this is a problem. The MotorHat constructor, which internally creates a PCA9685, does not have a way to use the pca ctor overload that takes additional arguments.

**Steps to reproduce**

```csharp
public class DCMotorController : BaseClass
{
public void StartMotor()
{
using (var motorHat = new MotorHat(1000d, 0x40))
{
var dcMotorOne = motorHat.CreateDCMotor(1); // The error is thrown here
dcMotorOne.Speed = 1;
Wait(1500);
dcMotorOne.Speed = 0;
}
}
}
```

**Expected behavior**

The motor should run, no exception thrown.

**Actual behavior**

Depending on the state of the PCA9685, an exception is thrown.

**Workaround**

Initializing the PCA9685 with forced default values before initializing the MotorHat binding seems to solve the issue.

**Suggested fix**

In the MotorHat ctor, use the PCA9685 ctor that uses all arguments to make sure it is initialized correctly. Or add another ctor there.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.