Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FW-98] PWM Tests #462

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Src/HALALMock/Services/PWM/DualPWM/DualPWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ float DualPWM::get_duty_cycle()const{
}
void DualPWM::set_dead_time(std::chrono::nanoseconds dead_time_ns)
{
*(this->dead_time_ns)=dead_time_ns;
if(*positive_is_on || *negative_is_on){
if(*positive_is_on || *negative_is_on)
ErrorHandler("%s","This function can not be called if the PWM is on");
}
return;

else
*(this->dead_time_ns)=dead_time_ns;
}

10 changes: 3 additions & 7 deletions Src/HALALMock/Services/PWM/PWM/PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ float PWM::get_duty_cycle(){
}
void PWM::set_dead_time(std::chrono::nanoseconds dead_t_ns)
{
*dead_time_ns=dead_t_ns;
if(*is_on){
if(*is_on)
ErrorHandler("%s","This function can not be called if the PWM is on");
}
/*
Code that creates a dead time in the mock where duty_cycle is 0
*/
return;
else
*dead_time_ns=dead_t_ns;
}
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable(${STLIB_TEST_EXECUTABLE}
${CMAKE_CURRENT_LIST_DIR}/Time/test_time.cpp
${CMAKE_CURRENT_LIST_DIR}/EncoderTest.cpp
${CMAKE_CURRENT_LIST_DIR}/InputCaptureTest.cpp
${CMAKE_CURRENT_LIST_DIR}/PWMTest.cpp
)

set_target_properties(${STLIB_TEST_EXECUTABLE} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED C_STANDARD 11 C_STANDARD_REQUIRED)
Expand Down
107 changes: 107 additions & 0 deletions Tests/PWMTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <gtest/gtest.h>
#include "HALALMock/Services/PWM/PWM/PWM.hpp"
#include "HALALMock/Services/SharedMemory/SharedMemory.hpp"

TEST(PWM, Turn_on){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, true);

SharedMemory::close();
}

TEST(PWM, Turn_off){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();
pwm.turn_off();

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, false);

SharedMemory::close();
}

TEST(PWM, Duty_cycle){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();
pwm.set_duty_cycle(50);
float duty=pwm.get_duty_cycle();

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, true);
EXPECT_EQ(pwm_pin.PinData.pwm.duty_cycle, 50);
EXPECT_EQ(duty, 50);

SharedMemory::close();
}

TEST(PWM, Frequency){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();
pwm.set_frequency(10000);
uint32_t freq=pwm.get_frequency();

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, true);
EXPECT_EQ(pwm_pin.PinData.pwm.frequency, 10000);
EXPECT_EQ(freq, 10000);

SharedMemory::close();
}

TEST(PWM, Deadtime_while_on){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();
pwm.set_dead_time(std::chrono::nanoseconds(1000000));

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, true);
EXPECT_EQ(pwm_pin.PinData.pwm.dead_time_ns, std::chrono::nanoseconds(0));

SharedMemory::close();
}

TEST(PWM, Deadtime_while_off){

SharedMemory::start("GPIO_Name", "State_Machine_Name");

PWM pwm(PA0);

pwm.turn_on();
pwm.turn_off();
pwm.set_dead_time(std::chrono::nanoseconds(1000000));

EmulatedPin& pwm_pin = SharedMemory::get_pin(PA0);

EXPECT_EQ(pwm_pin.PinData.pwm.is_on, false);
EXPECT_EQ(pwm_pin.PinData.pwm.dead_time_ns, std::chrono::nanoseconds(1000000));

SharedMemory::close();
}
Loading