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

🐛 Fix the problem of millis() #208

Merged
merged 1 commit into from
Feb 2, 2020

Conversation

Jerrylum
Copy link
Contributor

@Jerrylum Jerrylum commented Feb 2, 2020

Summary:

The millis() number reset to zero every 1h 11m 34s and 967ms.

According to the source code:

pros/src/rtos/tasks.c

Lines 2050 to 2062 in 1e07ae9

uint32_t millis(void)
{
uint32_t xTicks;
/* Critical section required if running on a 16 bit processor. */
portTICK_TYPE_ENTER_CRITICAL();
{
xTicks = xTickCount;
}
portTICK_TYPE_EXIT_CRITICAL();
return xTicks * configTICK_RATE_HZ / 1000;
}

Ok, so we are using uint32_t and the maximum value is 2^32 = 4294967296
At the same time. 1h 11m and 35s equal to 71*60*1000+35*1000 = 4295000

Please pay attention to line 2061, return xTicks * configTICK_RATE_HZ / 1000;

That equals to 4295000 * 1000 / 1000 -> 4295000000 / 1000 and 4295000000 > 4294967296
The millis() overflow.

Motivation:

Running a program for over an hour.

References (optional):

My time

Test Plan:

  • run the test code
void opcontrol() {
	while (1) {
        pros::lcd::print(6, "Time: %d", pros::millis());
        pros::lcd::print(7, "H:%d M:%d S:%d", pros::millis()/1000/60/60,
                                              pros::millis()/1000/60%60,
                                              pros::millis()/1000%60);
        pros::delay(20);
    }
}

@Jerrylum
Copy link
Contributor Author

Jerrylum commented Feb 2, 2020

100 hours challenge
image

@HotelCalifornia HotelCalifornia merged commit bbf6b32 into purduesigbots:develop Feb 2, 2020
@kunwarsahni01 kunwarsahni01 mentioned this pull request Sep 13, 2020
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