Severson-Group / AMDC-Firmware

Embedded system code (C and Verilog) which runs the AMDC Hardware
http://docs.amdc.dev/firmware
BSD 3-Clause "New" or "Revised" License
33 stars 6 forks source link

Manual Merge of #392 into Staging Branch #397

Closed codecubepi closed 5 months ago

codecubepi commented 5 months ago

This PR was created to merge the changes authored by @annikaolson into the v1.3-staging branch. I have also made a few additional changes which are noted at the very end of this comment.

Changes authored by Annika in #392

It was necessary to manually merge these changes, as we both had made changes to the FPGA, and the amdc_revf.bd block design file could not be automatically merged with my changes from #394 and these changes. To perform the manual merge, I looked through all the changed files in #392 and, after approval from @npetersen2 , I created a single commit (bd8ae30) for Annika's changes. The following description is copied from #392 and describes the changes made:

This PR addresses issues #391, #314, and #377. Previously, the scheduler ISR was triggered by the SysTick private timer in timer.c. This was initialized to a predetermined control rate, with the default at 10kHz - in other words, the scheduler would fire every 100 μs. The goal here was to instead synchronize the scheduler with the PWM carrier.

Two Scheduler Modes

There are now two modes for the scheduler source/functionality. This is user-configurable in usr/user_config.h by USER_CONFIG_ISR_SOURCE.

Mode 0

The scheduler runs the tasks (i.e. ISR is called to move out of the idle state) on every "trigger" assertion.

The user can change the PWM event qualifier (carrier high, low, or both) and the user ratio to change the control frequency of the tasks.

Mode 1

When none of the sensors are enabled, this mode operates the same as mode 0. When any sensors are enabled, the scheduler is synchronized with the sensors. The ISR is called when all of the sensors have completed their acquisition cycles (positive edge of all_done).

FPGA Changes

Sensors

https://github.com/Severson-Group/AMDC-Firmware/blob/47d34ecf31241087504ffe84d59e5e3b1ca63290/ip_repo/amdc_timing_manager_1.0/src/timing_manager.v#L207-L223

C-Code Changes

cmd_hw.c

To report the time, the code before was directly calling timing_manager_get_time_per_sensor(), but it is now using the statistics struct for each sensor. It's pulling the value (which is the most recent value), but the stats also hold the max, min, and mean value for each sensor time.

timing manager changes

scheduler changes

Block Design Changes

Additional Changes

npetersen2 commented 5 months ago

Nice work, @codecubepi

I reviewed the last 3 commits on this branch, as in, the changes not co-authored by @annikaolson. those look good to me.

I presume you squashed @annikaolson's previous PR into the single commit here, so since I already reviewed that over there, that should be good.

So.... I approve! :) Now, we just need to confirm this code actually works on the hardware

codecubepi commented 5 months ago

There is one build issue that we should resolve before merging this:

https://github.com/Severson-Group/AMDC-Firmware/blob/8f60d5c1bf2f9b7deea53740cb298e157c3190de/sdk/app_cpu1/common/sys/cmd/cmd_log.c#L64-L68

This fails to build since LOG_UPDATES_PER_SEC was deleted... how should I resolve?

npetersen2 commented 5 months ago

Good catch.

In the lastest code, the log task update rate is not constant... it floats, depending on the PWM settings.

So, I think this would change to:

double log_update_rate = 1.0 / (timing_manager_get_tick_delta() * 1e-6);
 if (samples_per_sec > log_update_rate || samples_per_sec <= 0) { 
     // ERROR 
     return CMD_INVALID_ARGUMENTS; 
 } 

or, something like this

codecubepi commented 5 months ago

@npetersen2

The problematic define was also present in log.c so I just updated log.h with a new define that is then available in both log.c and cmd_log.c