Skip to content

Commit

Permalink
Add support for PWM servo mode and PWM capture mode.
Browse files Browse the repository at this point in the history
For ports S1, S2, S3, if they are in servo mode, it is necessary to write to captureFile 0.

For capture mode, you need to write to captureFile 1.

captureFile is not needed for ports S4, S5, S6.
  • Loading branch information
danielsheh02 committed Dec 20, 2024
1 parent 14a119d commit 144bf3c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 14 deletions.
20 changes: 13 additions & 7 deletions tests/kernel-3.6/test-system-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,21 @@ equal to its class name.
deviceFile="/sys/class/pwm/ecap.2/duty_ns"
periodFile="/sys/class/pwm/ecap.2/period_ns"
runFile="/sys/class/pwm/ecap.2/run"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<servoMotor
port="S2"
deviceFile="/sys/class/pwm/ecap.1/duty_ns"
periodFile="/sys/class/pwm/ecap.1/period_ns"
runFile="/sys/class/pwm/ecap.1/run"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<servoMotor
port="S3"
deviceFile="/sys/class/pwm/ecap.0/duty_ns"
periodFile="/sys/class/pwm/ecap.0/period_ns"
runFile="/sys/class/pwm/ecap.0/run"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<servoMotor
port="S4"
Expand All @@ -113,18 +116,21 @@ equal to its class name.
/>
<pwmCapture
port="S1"
frequencyFile="/sys/class/pwm/ecap_cap.2/freq"
dutyFile="/sys/class/pwm/ecap_cap.2/duty_percentage"
frequencyFile="/sys/class/pwm/ecap.2/period_freq"
dutyFile="/sys/class/pwm/ecap.2/duty_ns"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<pwmCapture
port="S2"
frequencyFile="/sys/class/pwm/ecap_cap.1/freq"
dutyFile="/sys/class/pwm/ecap_cap.1/duty_percentage"
frequencyFile="/sys/class/pwm/ecap.1/period_freq"
dutyFile="/sys/class/pwm/ecap.1/duty_ns"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<pwmCapture
port="S3"
frequencyFile="/sys/class/pwm/ecap_cap.0/freq"
dutyFile="/sys/class/pwm/ecap_cap.0/duty_percentage"
port="S3"
frequencyFile="/sys/class/pwm/ecap.0/period_freq"
dutyFile="/sys/class/pwm/ecap.0/duty_ns"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<powerMotor port="M1" i2cCommandNumber="0x14" invert="true" />
<powerMotor port="M2" i2cCommandNumber="0x15" />
Expand Down
6 changes: 6 additions & 0 deletions tests/kernel-4.14/test-system-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ equal to its class name.
deviceFile="/sys/class/pwm/ecap.2/duty_ns"
periodFile="/sys/class/pwm/ecap.2/period_ns"
runFile="/sys/class/pwm/ecap.2/run"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<servoMotor
port="S2"
deviceFile="/sys/class/pwm/ecap.1/duty_ns"
periodFile="/sys/class/pwm/ecap.1/period_ns"
runFile="/sys/class/pwm/ecap.1/run"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<servoMotor
port="S3"
deviceFile="/sys/class/pwm/ecap.0/duty_ns"
periodFile="/sys/class/pwm/ecap.0/period_ns"
runFile="/sys/class/pwm/ecap.0/run"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<servoMotor
port="S4"
Expand All @@ -133,16 +136,19 @@ equal to its class name.
port="S1"
frequencyFile="/sys/class/pwm/ecap_cap.2/freq"
dutyFile="/sys/class/pwm/ecap_cap.2/duty_percentage"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<pwmCapture
port="S2"
frequencyFile="/sys/class/pwm/ecap_cap.1/freq"
dutyFile="/sys/class/pwm/ecap_cap.1/duty_percentage"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<pwmCapture
port="S3"
frequencyFile="/sys/class/pwm/ecap_cap.0/freq"
dutyFile="/sys/class/pwm/ecap_cap.0/duty_percentage"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<powerMotor port="M1" i2cCommandNumber="0x14" invert="true" />
<powerMotor port="M2" i2cCommandNumber="0x15" />
Expand Down
20 changes: 13 additions & 7 deletions trikControl/configs/kernel-3.6/system-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@ equal to its class name.
deviceFile="/sys/class/pwm/ecap.2/duty_ns"
periodFile="/sys/class/pwm/ecap.2/period_ns"
runFile="/sys/class/pwm/ecap.2/run"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<servoMotor
port="S2"
deviceFile="/sys/class/pwm/ecap.1/duty_ns"
periodFile="/sys/class/pwm/ecap.1/period_ns"
runFile="/sys/class/pwm/ecap.1/run"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<servoMotor
port="S3"
deviceFile="/sys/class/pwm/ecap.0/duty_ns"
periodFile="/sys/class/pwm/ecap.0/period_ns"
runFile="/sys/class/pwm/ecap.0/run"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<servoMotor
port="S4"
Expand All @@ -115,18 +118,21 @@ equal to its class name.
/>
<pwmCapture
port="S1"
frequencyFile="/sys/class/pwm/ecap_cap.2/period_freq"
dutyFile="/sys/class/pwm/ecap_cap.2/duty_ns"
frequencyFile="/sys/class/pwm/ecap.2/period_freq"
dutyFile="/sys/class/pwm/ecap.2/duty_ns"
captureFile="/sys/class/pwm/ecap.2/capture"
/>
<pwmCapture
port="S2"
frequencyFile="/sys/class/pwm/ecap_cap.1/period_freq"
dutyFile="/sys/class/pwm/ecap_cap.1/duty_ns"
frequencyFile="/sys/class/pwm/ecap.1/period_freq"
dutyFile="/sys/class/pwm/ecap.1/duty_ns"
captureFile="/sys/class/pwm/ecap.1/capture"
/>
<pwmCapture
port="S3"
frequencyFile="/sys/class/pwm/ecap_cap.0/period_freq"
dutyFile="/sys/class/pwm/ecap_cap.0/duty_ns"
port="S3"
frequencyFile="/sys/class/pwm/ecap.0/period_freq"
dutyFile="/sys/class/pwm/ecap.0/duty_ns"
captureFile="/sys/class/pwm/ecap.0/capture"
/>
<powerMotor port="M1" i2cCommandNumber="0x14" invert="true" />
<powerMotor port="M2" i2cCommandNumber="0x15" />
Expand Down
6 changes: 6 additions & 0 deletions trikControl/configs/kernel-4.14/system-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ equal to its class name.
deviceFile="/sys/class/pwm/pwmchip2/pwm0/duty_cycle"
periodFile="/sys/class/pwm/pwmchip2/pwm0/period"
runFile="/sys/class/pwm/pwmchip2/pwm0/enable"
captureFile="/sys/class/pwm/pwmchip2/pwm0/capture"
/>
<servoMotor
port="S2"
deviceFile="/sys/class/pwm/pwmchip1/pwm0/duty_cycle"
periodFile="/sys/class/pwm/pwmchip1/pwm0/period"
runFile="/sys/class/pwm/pwmchip1/pwm0/enable"
captureFile="/sys/class/pwm/pwmchip1/pwm0/capture"
/>
<servoMotor
port="S3"
deviceFile="/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
periodFile="/sys/class/pwm/pwmchip0/pwm0/period"
runFile="/sys/class/pwm/pwmchip0/pwm0/enable"
captureFile="/sys/class/pwm/pwmchip0/pwm0/capture"
/>
<servoMotor
port="S4"
Expand All @@ -132,16 +135,19 @@ equal to its class name.
port="S1"
frequencyFile="/sys/class/pwm/ecap_cap.2/freq"
dutyFile="/sys/class/pwm/ecap_cap.2/duty_percentage"
captureFile="/sys/class/pwm/ecap_cap.2/capture"
/>
<pwmCapture
port="S2"
frequencyFile="/sys/class/pwm/ecap_cap.1/freq"
dutyFile="/sys/class/pwm/ecap_cap.1/duty_percentage"
captureFile="/sys/class/pwm/ecap_cap.1/capture"
/>
<pwmCapture
port="S3"
frequencyFile="/sys/class/pwm/ecap_cap.0/freq"
dutyFile="/sys/class/pwm/ecap_cap.0/duty_percentage"
captureFile="/sys/class/pwm/ecap_cap.0/capture"
/>
<powerMotor port="M1" i2cCommandNumber="0x14" invert="true" />
<powerMotor port="M2" i2cCommandNumber="0x15" />
Expand Down
8 changes: 8 additions & 0 deletions trikControl/src/pwmCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ PwmCapture::PwmCapture(const QString &port, const trikKernel::Configurer &config
, mDutyFile(hardwareAbstraction.createInputDeviceFile(configurer.attributeByPort(port, "dutyFile")))
, mState("PWM Capture on " + port)
{
QScopedPointer<trikHal::OutputDeviceFileInterface> captureFile
(hardwareAbstraction.createOutputDeviceFile(configurer.attributeByPort(port, "captureFile")));
if (!captureFile->open()) {
mState.fail();
return;
}
captureFile->write(QString::number(1));

if (!mFrequencyFile->open()) {
mState.fail();
}
Expand Down
11 changes: 11 additions & 0 deletions trikControl/src/servoMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ ServoMotor::ServoMotor(const QString &port, const trikKernel::Configurer &config
, mRun(false)
, mState("Servomotor on " + port)
{
QString captureFileAttribute = configurer.attributeByPortOrDefault(port, "captureFile");
if (!captureFileAttribute.isEmpty()) {
QScopedPointer<trikHal::OutputDeviceFileInterface> captureFile
(hardwareAbstraction.createOutputDeviceFile(captureFileAttribute));
if (!captureFile->open()) {
mState.fail();
return;
}
captureFile->write(QString::number(0));
}

const auto configure = [this, &port, &configurer](const QString &parameterName) {
return ConfigurerHelper::configureInt(configurer, mState, port, parameterName);
};
Expand Down
3 changes: 3 additions & 0 deletions trikKernel/include/trikKernel/configurer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class TRIKKERNEL_EXPORT Configurer
/// Returns value of given attribute of a device on given port.
QString attributeByPort(const QString &port, const QString &attributeName) const;

/// Returns value of given attribute of a device on given port or EMPTY string.
QString attributeByPortOrDefault(const QString &port, const QString &attributeName) const;

/// Returns true if device is enabled in current configuration (either explicitly enabled in model configuration
/// or can not be disabled at all).
bool isEnabled(const QString &deviceName) const;
Expand Down
10 changes: 10 additions & 0 deletions trikKernel/src/configurer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ QString Configurer::attributeByPort(const QString &port, const QString &attribut
.arg(attributeName).arg(deviceType).arg(port));
}

QString Configurer::attributeByPortOrDefault(const QString &port, const QString &attributeName) const
{
try{
return attributeByPort(port, attributeName);
} catch (const trikKernel::MalformedConfigException & ex){
QLOG_INFO() << ex.errorMessage();
return "";
}
}

bool Configurer::isEnabled(const QString &deviceName) const
{
if (mAdditionalModelConfiguration.contains(deviceName)) {
Expand Down

0 comments on commit 144bf3c

Please sign in to comment.