###### tags: `程式組教程`
# Timed Mechanics (Basics)
## Why we need robot run automatically ?
In autonomous mode , drivers can't control their robot . But you still can get the point in this mode , so let robot run automatically to complete the requirement of the rule will increase your winning percentage . The below progragh will teach you run the robot automatically simply .
## The simple way to run robot automatically
Try to image that you can control the robot by telling command , like telling it forward 10 second , turn left and forward 5 second , then you can caculate the distance by the time and the speed . Even though i am not the physicist , but that sound good , right ? Now , change youself to the code.
## The important class in this part
Ya ! As you think it's **Timer** . It's replace you at the previous imagination and allow the robot know how many times passed . There are some method to use the Timer below.
---
import Timer class
```java=
import edu.wpi.first.wpilibj.Timer;
```
Create the Timer object
```java=
Timer timer = new Timer();
```
Some Timer function
| Function | Type | Description |
| -------- | -------- | -------- |
| advanceIfElapsed (double seconds) | boolean | Check if the period specified has passed and if it has, advance the start time by that period. |
|==**delay (double seconds)**==|static void|Pause the thread for a specified time.|
|==**get()**==|double|Get the current time from the timer.|
|getFPGATimestamp()|static double|Return the system clock time in seconds.|
|getMatchTime()|static double|Return the approximate match time.|
|hasElapsed(double seconds)|boolean|Check if the period specified has passed.|
|==**reset()**==|void|Reset the timer by setting the time to 0.|
|start()|void|Start the timer running.|
|stop()|void|Stop the timer.|
:::info
The thing in () is the require thing to put in, if it is double just put double , if there is nothing just call the function
:::
## The example to use Timer
```java=
@Override
public void autonomousInit() {
timer.reset();
}
public void autonomousPeriodic() {
if(timer.get() <= 10){
Left.set(0.5);
Right.set(0.5);
//forward for 10 seconds
}
else{
Left.set(0);
Right.set(0);
}
}
```
```java=
@Override
public void robotInit() {
Left.follow(Right);
}
@Override
public void autonomousInit() {
timer.reset();
}
public void autonomousPeriodic() {
Right.set(0.5);
delay(10);
Right.set(0);
//forward for 10 seconds
}
```
```java=
double time_i_need = 5;
double error = abs(timer.get()-time_i_need);
error = error - int(error);
double speed = error * 0.5;
@Override
public void autonomousInit() {
timer.reset();
public void autonomousPeriodic() {
}
```
:::warning
The example are not the only way to code , there are lots of way , try to think how to code in your way
:::
:::success
This way to autonomous is not the best way , it may impact by lots of fators , the advanced way to autonomous is coming soon , like using encoder , path planning
:::