C/C++ Question .hpp file only

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User

Johan Schoeman

Expert
Licensed User
Longtime User
can you check if this wrapper for luisllamasbinaburo/Arduino-PIDController-Library can help you ?
So, only had to tweak your b4r BasicPIDController sample project for my setup and scale the analog input and digital output to be in the 0 to 255 range - it works perfectly! Well done and thank you very much for this great wrapper. Lots for me to learn from it.
 

Johan Schoeman

Expert
Licensed User
Longtime User
can you check if this wrapper for luisllamasbinaburo/Arduino-PIDController-Library can help you ?
Attached is my sample B4R project making use of your library - your library works very well!

B4R Code:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private pinSetpoint As Pin
    Private pinFeedback As Pin
    Private pinOutput As Pin
    Private pinAdditionalOutput As Pin
    Private Parameters As PIDParameters
    Private parametersID As ULong
    Private pidController As PIDController
    
    Dim cont As CONTROLLINO
    
    Dim A  As Long = 1000                    'Resistance in darkness in KΩ
    Dim B As Int = 15                        'Light resistance (10 Lux) in KΩ
    Dim Rc As Int = 10                       'Calibration resistance in KΩ
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    pinSetpoint.Initialize(cont.CONTROLLINO_A1, pinSetpoint.MODE_INPUT)                  'Pin 55 - Pot wiper
    pinFeedback.Initialize(cont.CONTROLLINO_A0, pinFeedback.MODE_INPUT)                     'Pin 54 - LDR
    pinOutput.Initialize(cont.CONTROLLINO_D0, pinOutput.MODE_OUTPUT)                     'Pin 2 - standard LED
    pinAdditionalOutput.Initialize(cont.CONTROLLINO_D1, pinAdditionalOutput.MODE_OUTPUT) 'Pin 3 - 24VDC fan
    
    parametersID = Parameters.Initialize(0.4, 6.0, 0.0001)
    pidController.Initialize(parametersID,pidController.DIRECTION_DIRECT)
    
    pidController.Input = MapRange(pinFeedback.AnalogRead, 0, 1023, 0, 255)
    pidController.Setpoint = pinSetpoint.AnalogRead
    
    pidController.TurnOn(True)
    AddLooper("localLoop")
End Sub

Sub LocalLoop
    pidController.Setpoint = MapRange(pinSetpoint.AnalogRead, 0, 1023, 0, 255)
    
    pidController.Input = readFeedback
    
    Log("Setpoint = ", MapRange(pinSetpoint.AnalogRead, 0, 1023, 0, 255))
    Log("Feedback = ", readFeedback)
    Log("Output = ", pidController.Output)
    Log("Error = ", pidController.GetError)
    Log(" ")
    
    pidController.Update()
    
    pinOutput.AnalogWrite(pidController.Output)
    pinAdditionalOutput.AnalogWrite(pidController.Output)
End Sub

Sub readFeedback As Int                    'specific to the LDR I am using as feedback from the "plant"
    
    Dim mean As Float = 0
    Dim V As Float
    For i = 0 To 99
        V = pinFeedback.AnalogRead
        mean = mean +  (V * A * 10) / (B * Rc * (1024 - V))
    Next
    mean  = mean / 100
    Return mean
    
End Sub
 

Attachments

  • PIDControllerBasic_test.zip
    1.5 KB · Views: 109
Top