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

Pinecil v2 tune via PID #1827

Merged
merged 13 commits into from
Oct 20, 2023
Merged

Pinecil v2 tune via PID #1827

merged 13 commits into from
Oct 20, 2023

Conversation

Ralim
Copy link
Owner

@Ralim Ralim commented Oct 15, 2023

  • Please check if the PR fulfills these requirements
  • The changes have been tested locally
  • [] There are no breaking changes
  • What kind of change does this PR introduce?

Basically, I've given up with the previous control scheme and have written a fairly trivial PID implementation and tuned it for my PinecilV2.

This includes:

  • Made a workaround for glitchy / missed timer events
  • Wrote basic PID
  • Opted PinecilV2 into said PID
  • Tuned PinecilV2 for said PID
  • What is the current behavior?

#1824
#1688

  • What is the new behavior (if this is a feature change)?

  • Other information:
    This still isnt perfect but for my testing is largely improved. Any testing would be highly appreciated. If you still get the large oscillations please let me know what tip + power supply is being used so I can attempt to replicate.

@Ralim Ralim marked this pull request as ready for review October 15, 2023 10:40
@Ralim Ralim marked this pull request as draft October 15, 2023 10:46
Ralim added 5 commits October 15, 2023 22:00
Update BSP.cpp

Update BSP.cpp
move pid tuning to config

Update PIDThread.cpp
@Ralim Ralim force-pushed the Pinecil-v2-tune branch 2 times, most recently from a9a190f to 72e1a64 Compare October 15, 2023 11:09
@Ralim Ralim marked this pull request as ready for review October 15, 2023 11:16
Copy link
Collaborator

@discip discip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

image

image

image

These screenshots speak for themselves!

@Ralim
Copy link
Owner Author

Ralim commented Oct 16, 2023

@discip
Added a small filter over the displayed value on the gui to reduce flicker; curious your thoughts?

Still more to tune on this, but that will likely be 2.23

@discip
Copy link
Collaborator

discip commented Oct 16, 2023

I',m speechless.
I already started to type the request for adding a tranquilizer to the display, but then scrapped it so as not to seem ungrateful. 😀

Unfortunately not everything is covered by this as I've seen spikes of up to ~334°C (@ 320°C).

@Ralim
Copy link
Owner Author

Ralim commented Oct 16, 2023

I said I would get it eventually; life just got in the way. Finally trying to make back lost time.
Its all good, you know I value input.

By speeding up the PID; I made the screen a little aggressive 🤣 So while I still stand by the idea of "dont filter out the real world" like some devices do, this makes a lot of sense.

I've spotted the odd spike too, I need to look into them, I suspect its due to the a quirk with me skipping a cycle when the timer does weird things -- more debugging required to really know for certain.

@goga1978
Copy link

And if you use a median filter instead of a moving average?

@Ralim
Copy link
Owner Author

Ralim commented Oct 17, 2023

The type of filter in this instance wont really change things as the filter is really only going to average 1-2 readings at a time (and just saturate at the new reading between). The outliers mentioned above are real and are a quirk of supressing the adc cycle. I need to prevent the spike rather than filter it from view.

@goga1978
Copy link

moving average
[290, 300, 380] = 970 / 3 = 323

median filter
[290, 300, 380] = 300

@Ralim
Copy link
Owner Author

Ralim commented Oct 17, 2023

I'm aware of how a median filter works, but its not the best solution as our data looks like:

{319,319,319,319,319,319,321,321,321,321,321,321,318,318,318,318,318}
Or in the case of a spike
{319,319,319,319,319,319,340,340,340,340,340,340,318,318,318,318,318}

As its called per gui redraw.
Also a median filter is not O(1) as far as I recall, and O(n) and above is nice to avoid in the fast loop for the gui.

I'll just focus on preventing the spike in the first place :)

@goga1978
Copy link

median filter
{319,319,319,319,319,319,340,340,340,340,340,340,318,318,318,318,318}
sort
{318,318,318,318,318,319,319,319,319,319,319,340,340,340,340,340,340} = x[8] = 319
with a surge , there will be such a value

sorted in ascending order and the middle is taken

@Ralim
Copy link
Owner Author

Ralim commented Oct 18, 2023

@discip I've done a small tweak to hopefully reduce the spikes a little.
See how that looks; I think this should be ok to merge now unless you spot any further issues 😓

@goga1978
yes, but a sort is not O(1) time to run.
Secondly of that dataset, only a sliding window of ~8 readings is being used at a time; the goal is not to hide the spike.
The goal is to prevent small jitter, so the spike should still be visible if it occurs.

@discip
Copy link
Collaborator

discip commented Oct 18, 2023

See how that looks;

Will do as soon as I'm back home. 😅

@discip
Copy link
Collaborator

discip commented Oct 18, 2023

  1. I noticed the iron to display 1°C less than PineSAM
  2. Unfortunately the spikes still occur (334°C @ 320°C)!
    The graph does look unchanged, almost stationary.

image

@goga1978
Copy link

HI.
https://files.pine64.org/doc/Pinecil/Pinecil_schematic_v2.0_20220608.pdf
I looked at the soldering iron diagram, it's terrible. Marked with red circles.

Soldering iron tip-
Kazam_screenshot_00280

The amplifier for the sting thermocouple. Connects to point A. Attention at SGM8557 - there is no protection of the entrance!!! Do not turn on without a sting!!!

Kazam_screenshot_00281

The output of the SGM8557 amplifier is connected to Tip_Sence.

Kazam_screenshot_00283

On the Tip_Sence pin there is a capacitor C22 with a capacity of 22000pf. So the first values in the interval between PWMs will be overestimated !!!

@goga1978
Copy link

HI.
FILE IRQ.cpp
// Returns the current raw tip reading after any cleanup filtering
// For Pinecil V2 we dont do any rolling filtering other than just averaging all 4 readings in the adc snapshot
uint16_t getTipRawTemp(uint8_t sample) { return ADC_Tip.average() >> 1;

where is it set ? that only 4 temperature values will be recorded?

@goga1978
Copy link

how do I skip the first two values? And write down the following ?
Constantly skip the first two values.
so that the capacitor c22 has time to discharge

@goga1978
Copy link

diode limitation input voltage up to 0.5 volts

Kazam_screenshot_00281_M

@Ralim
Copy link
Owner Author

Ralim commented Oct 20, 2023

@goga1978 This is off topic to this PR, please use a suitable thread.

The input to the op-amp is fine; the 100K resistor limits our current to 280uA max which is handled fine by the internal clamping diodes. As confirmed by manufacturer.

so that the capacitor c22 has time to discharge

C22 is not a problem as we wait for the charge change before we start the ADC, and it helps keep readings stable while the ADC is charging its internal measurement cap. There is no requirement to discard readings. The delay is configured via the holdoffTicks variable. This is adjusted in the functions for changing PWM rate for example here

where is it set ? that only 4 temperature values will be recorded?

In your IDE, if you look at references to the ADC_Tip variable you will find it is updated in the IRQ handler near the top of the file.

@Ralim
Copy link
Owner Author

Ralim commented Oct 20, 2023

@discip

  1. I noticed the iron to display 1°C less than PineSAM

    1. Unfortunately the spikes still occur (334°C @ 320°C)!
      The graph does look unchanged, almost stationary.

image

Ah, PineSAM is not affected by any GUI filtering I do. Bluetooth reads directly with no filters, so that its up to app developers how they want to handle that

Dang on the spike still occuring. Ill spend a bit longer trying to reproduce them.
I might merge this in as-is and look at spikes in a new PR so this doesnt get too big of a change 😓

@goga1978
Copy link

goga1978 commented Oct 20, 2023

Hi

#define ADC_Filter_Smooth 1 /* This basically smooths over one PWM cycle / set of readings */

void switchToFastPWM(void) {
inFastPWMMode = true;
holdoffTicks = 12;
tempMeasureTicks = 10;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);

// ~10Hz
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks);

// Set divider to 10 ~= 10.5Hz
uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR);

tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 20);

BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
}

void switchToSlowPWM(void) {
// 5Hz
inFastPWMMode = false;
holdoffTicks = 12;
tempMeasureTicks = 10;
totalPWM = powerPWM + tempMeasureTicks + holdoffTicks;

TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_2, totalPWM);
// Adjust ADC
TIMER_SetCompValue(TIMER_CH0, TIMER_COMP_ID_0, powerPWM + holdoffTicks);

// Set divider for ~ 5Hz

uint32_t tmpVal = BL_RD_REG(TIMER_BASE, TIMER_TCDR);

tmpVal = BL_SET_REG_BITS_VAL(tmpVal, TIMER_TCDR2, 20);

BL_WR_REG(TIMER_BASE, TIMER_TCDR, tmpVal);
}

I did so and it seems like the races were gone )))

@discip
Copy link
Collaborator

discip commented Oct 20, 2023

Ah, PineSAM is not affected by any GUI filtering I do. Bluetooth reads directly with no filters,

I somehow assumed that to be the case. 😊

Ill spend a bit longer trying to reproduce them.

👍🏻

@Ralim Ralim merged commit c308fe8 into dev Oct 20, 2023
30 checks passed
@Ralim Ralim deleted the Pinecil-v2-tune branch October 20, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants