2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-11-15 19:33:50 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
This example shows how to use the HAL directly, and what is needed to run a
|
|
|
|
|
basic program. The sample is compiled in C, however the functionality works from
|
|
|
|
|
C++ as well.
|
|
|
|
|
|
|
|
|
|
The HAL is considered a stable but changeable API. The API is stable during a
|
|
|
|
|
season, and is safe to use for events. However, between seasons there might be
|
|
|
|
|
changes to the API. This is an advanced sample, and should only be used by users
|
|
|
|
|
that want even more control over what code runs on their robot.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
#include <hal/HAL.h>
|
2018-11-15 19:33:50 -08:00
|
|
|
|
|
|
|
|
enum DriverStationMode {
|
|
|
|
|
DisabledMode,
|
|
|
|
|
TeleopMode,
|
|
|
|
|
TestMode,
|
|
|
|
|
AutoMode,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum DriverStationMode getDSMode(void) {
|
|
|
|
|
// Get Robot State
|
|
|
|
|
HAL_ControlWord word;
|
|
|
|
|
HAL_GetControlWord(&word);
|
|
|
|
|
|
|
|
|
|
// We send the observes, otherwise the DS disables
|
|
|
|
|
if (!word.enabled) {
|
|
|
|
|
HAL_ObserveUserProgramDisabled();
|
|
|
|
|
return DisabledMode;
|
|
|
|
|
} else {
|
|
|
|
|
if (word.autonomous) {
|
|
|
|
|
HAL_ObserveUserProgramAutonomous();
|
|
|
|
|
return AutoMode;
|
|
|
|
|
} else if (word.test) {
|
|
|
|
|
HAL_ObserveUserProgramTest();
|
|
|
|
|
return TestMode;
|
|
|
|
|
} else {
|
|
|
|
|
HAL_ObserveUserProgramTeleop();
|
|
|
|
|
return TeleopMode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
|
// Must initialize the HAL, 500ms timeout
|
|
|
|
|
HAL_Bool initialized = HAL_Initialize(500, 0);
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
printf("Failed to initialize the HAL\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
// For DS to see valid robot code
|
|
|
|
|
HAL_ObserveUserProgramStarting();
|
|
|
|
|
|
|
|
|
|
// Create a Motor Controller
|
|
|
|
|
status = 0;
|
2021-05-01 10:28:30 -07:00
|
|
|
HAL_DigitalHandle pwmPort =
|
|
|
|
|
HAL_InitializePWMPort(HAL_GetPort(2), NULL, &status);
|
2018-11-15 19:33:50 -08:00
|
|
|
|
|
|
|
|
if (status != 0) {
|
2021-04-29 09:56:54 -07:00
|
|
|
const char* message = HAL_GetLastError(&status);
|
2018-11-15 19:33:50 -08:00
|
|
|
printf("%s\n", message);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set PWM config to standard servo speeds
|
|
|
|
|
HAL_SetPWMConfig(pwmPort, 2.0, 1.501, 1.5, 1.499, 1.0, &status);
|
|
|
|
|
|
|
|
|
|
// Create an Input
|
|
|
|
|
status = 0;
|
2021-05-01 10:28:30 -07:00
|
|
|
HAL_DigitalHandle dio =
|
|
|
|
|
HAL_InitializeDIOPort(HAL_GetPort(2), 1, NULL, &status);
|
2018-11-15 19:33:50 -08:00
|
|
|
|
|
|
|
|
if (status != 0) {
|
2021-04-29 09:56:54 -07:00
|
|
|
const char* message = HAL_GetLastError(&status);
|
2018-11-15 19:33:50 -08:00
|
|
|
printf("%s\n", message);
|
|
|
|
|
status = 0;
|
|
|
|
|
HAL_FreePWMPort(pwmPort, &status);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
// Wait for DS data, with a timeout
|
|
|
|
|
HAL_Bool validData = HAL_WaitForDSDataTimeout(1.0);
|
|
|
|
|
if (!validData) {
|
|
|
|
|
// Do something here on no packet
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
enum DriverStationMode dsMode = getDSMode();
|
|
|
|
|
switch (dsMode) {
|
|
|
|
|
case DisabledMode:
|
|
|
|
|
break;
|
|
|
|
|
case TeleopMode:
|
|
|
|
|
status = 0;
|
|
|
|
|
if (HAL_GetDIO(dio, &status)) {
|
|
|
|
|
HAL_SetPWMSpeed(pwmPort, 1.0, &status);
|
|
|
|
|
} else {
|
|
|
|
|
HAL_SetPWMSpeed(pwmPort, 0, &status);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case AutoMode:
|
|
|
|
|
break;
|
|
|
|
|
case TestMode:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clean up resources
|
|
|
|
|
status = 0;
|
|
|
|
|
HAL_FreeDIOPort(dio);
|
|
|
|
|
HAL_FreePWMPort(pwmPort, &status);
|
|
|
|
|
}
|