Skip to content

Conversation

@BigLiban
Copy link

No description provided.

// convert resolution to # of counts
uint16_t counts = adcValToCounts(resolution);
// have the timer start
HAL_TIM_Base_Start(&htim1);

Choose a reason for hiding this comment

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

Close, but you need to use the PWM version of this function.

Also the timer should have started before the event loop of the program

Copy link
Author

Choose a reason for hiding this comment

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

Fixed this, thank you

for(int i = 0; i < sizeof(TRANSMISSION_DATA); ++i){
send_buf[i] = TRANSMISSION_DATA[i];
}

Choose a reason for hiding this comment

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

Bit redundant

Copy link
Author

Choose a reason for hiding this comment

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

removed it, thank you

/* USER CODE BEGIN 0 */

uint16_t adcValToCounts(uint16_t resolution){
return (resolution / maxADCVal) * maxCounterVal;

Choose a reason for hiding this comment

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

This formula doesn't linearly scale between 5 and 10 percent of your chosen value

Copy link
Author

Choose a reason for hiding this comment

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

Let me know if the formula is now correct


/* USER CODE BEGIN PV */
const char TRANSMISSION_DATA[3] = {0x01, 0x80, 0x00}; // 00000001 10000000 (SGL/DIFF = 1, CH0 config) 00000000
const uint16_t maxCounterVal = 2400;

Choose a reason for hiding this comment

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

You can have a max counter val be 2400, but there is an optimal solution we're looking for in terms of prescalar and period values.

Also looking at your commit history it seems like any changes you made to the timer peripheral weren't saved since there's no diff for the .ioc

Copy link
Author

Choose a reason for hiding this comment

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

For now, let me know if my scaling function is correct, then I will look into the optimal solution in terms of prescalar and period values. Also pushed all the STM files


/* USER CODE BEGIN PV */
const uint16_t maxCounterVal = 2400;
const uint16_t minOnPeriodCounterVal = 2160; // This is 10% off of 2400, so the on period would be 2160-2400

Choose a reason for hiding this comment

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

The logic would actually be the other way around, so from 0-2160 the pwm signal would be high.

Copy link
Author

Choose a reason for hiding this comment

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

Oh ok, so is it just the comment and variable name that are incorrect?

/* USER CODE BEGIN 0 */

uint16_t adcValToCounts(uint16_t resolution){
return (resolution / maxADCVal) * (maxCounterVal - minOnPeriodCounterVal) + minOnPeriodCounterVal; // scale value up to a range of 2160-2400 (10% duty cycle)

Choose a reason for hiding this comment

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

this is good

HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)&send_buf, (uint8_t*)&recv_buf, 3, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);

uint16_t resolution = ( ((uint16_t)recv_buf[2] << 8) & (uint16_t)recv_buf[3]) & 0x0003FF; // 10 bit value of ADC,

Choose a reason for hiding this comment

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

I would revisit the bitwise logic for this line

Copy link
Author

Choose a reason for hiding this comment

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

Oops, should be an OR there, not AND.

void runMotor(uint16_t resolution) {
// convert resolution to # of counts
uint16_t counts = adcValToCounts(resolution);
// if it reaches the value we have in the resolution -> motor pin gets set to high
Copy link

@dechair3 dechair3 Jan 12, 2026

Choose a reason for hiding this comment

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

The logic actually works the other way around

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.

2 participants