---
# System prepended metadata

title: ext

---

# ext
## button.c
### Functions
#### void btn_init()
Initialization of the button
Parameters: None
Returns: None

#### bool btn_pressed(ButtonID button_id)



## buzzer.c

### Functions
#### void buzzer_init()
Initialization of the buzzer.
Parameters: None
Returns: None

#### void buzzer_on()
Turn on the buzzer.
Parameters: None
Returns: None

#### void buzzer_off()
Turn off the buzzer.
Paramters: None
Returns: None

## can_motor.c
### Functions
#### static void feedback_decoding(CanRxMsg* msg)
Handler for decoding motor CAN feedback message.
The function first validates msg under the following conditions:
1. `msg->Data[0] == CAN_ENCODER_FEEDBACK` 
2. `msg->DLC == CAN_ENCODER_FEEDBACK_LENGTH`
3. `msg->StdId >= CAN_MOTOR_BASE`
4. `msg->StdId < CAN_MOTOR_BASE + CAN_MOTOR_COUNT`

Then, it would change the corresponding value in global array `encValue`.

Parameters:
1. msg: CanRxMsg*
The CAN message to be decoded

Returns: None

#### void can_motor_init()
Initialization of the CAN motor.
Parameters: None
Returns: None

#### void can_motor_set_vel(MotorID id, int32_t vel, CloseLoopFlag loop)
Set the CAN motor's velocity.
The function checks if `motor_id < CAN_MOTOR_COUNT` first.
Then, the CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. id: MotorID
The ID of the motor to control.
2. vel: int32_t
The velocity to set.
For open loop, the range is about $[-1799,1799]$.
For close loop, the range is about $[-150, 150]$.
3. loop: CloseLoopFlag
Whether an open loop or close loop control is used.

Returns: None

#### void can_motor_set_pos(MotorID id, int32_t vel, int32_t pos)
Set the CAN motor's position.
The function checks if `motor_id < CAN_MOTOR_COUNT` first.
Then, the CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. id: MotorID
The ID of the motor to control.
2. vel: int32_t (?)
The closed loop velocity to set.
For close loop, the range is about $[-150, 150]$.
3. pos: int32_t
The position needed to move to relative to current encoder value.

Returns: None

#### void MCU_LOCK()
Lock the motor control unit.
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void MCU_Enable()
Enable the motor control unit.
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void can_motor_set_accel(MotorID id, uint16_t accel)
Set the CAN motor's acceleration
The function checks if `motor_id < CAN_MOTOR_COUNT` first.
Then, the CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. id: MotorID
The ID of the motor to control.
2. accel: uint16_t
The acceleration of the motor.

Returns: None

#### void can_motor_lock(MotorID id)
Lock and stop the motor immediately
The function checks if `motor_id < CAN_MOTOR_COUNT` first.
Then, the CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. id: MotorID
The ID of the motor to control.

Returns: None

#### void BM_motor_lock()
Lock and stop the BM motor immediately
The CAN message would be encoded and put to the CAN queue for transmission.

Parameters: None
Returns: None

#### void BM_motor_enable()
Enable the BM motor.
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void BM_motor_clear_error()
Clear the error in the BM motor.
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void BM_motor_normal_direction()
Set the BM motor to the normal direction.
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void BM_motor_reverse_direction()
Reverse the direction of the BM motor
The CAN message would be encoded and put to the CAN queue for transmission.
Parameters: None
Returns: None

#### void BM_motor_set_torque(int16_t vel)
Set the torque for the BM motor
The CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. vel: int16_t
Range for the torque is about $[-32767, 32767].

Returns: None

#### void BM_motor_set_speed(int16_t vel)
Set the speed for the BM motor
The CAN message would be encoded and put to the CAN queue for transmission.

Parameters:
1. vel: int16_t
Range for the torque is about $[-10922, 10922].

Returns: None

#### int32_t can_get_encoder_value(MotorID id)
Get the motor encoder value based on CAN rx result

Parameters:
1. id: MotorID
The ID of the motor.

Returns:
1. encValue[id]: int32_t

### Variable

#### static int32_t encValue[CAN_MOTOR_COUNT]
Store the encoder values of CAN motors.


# User
## main.c
### Variables
|Name|Initial Value|Description|
|-|-|-|
|torque| 0||
|torque_per||
|adc5_per||
|adc6_per||
|pedal_diff||
|en_state|0|enable state|
|rev_drive|0|stop and reset|
|buzzer_state|0|buzzer|
|bp_error|0|break paddel error|

### Constants
|Name|Value|Description|
|-|-|-|
|max_torque| 32767||
|min_torque| 0||
|max_adc5|3560|Analog-to-digital Conversion Value(?) in terms of max voltage|
|min_adc5|1479|ADC value|
|max_adc6|1180|ADC value|
|min_adc6|512|ADC value|
|max_adc7|1440|ADC value|
|min_adc7|149|ADC value|

### Initialization
|Function|Description|
|-|-|
|SystemInit()||
|SystemCoreClockUpdate()||
|gpio_rcc_init_all()||
|ticks_init()||
|adc_init()||
|tft_init((TFT_ORIENTATION)ORIENTATION_SETTING, BLACK, WHITE, RED)||
|led_init()||
|buzzer_init()||
|btn_init()||
|timer_init()||
|servo_init()||
|can_init()||
|can_rx_init()||
|gun_can_motor_init()||
|pneu_init()||
|uart_init(COM1, 115200)||
|gpio_init(&PE3, GPIO_Mode_IN, GPIO_Medium_Speed, GPIO_OType_PP, GPIO_PuPd_UP )||
|pneu_control(PNEU_1, Bit_SET)|enable the state|
|pneu_control(PNEU_2, Bit_SET)|enable the state|
|gpio_init(&PE5, GPIO_Mode_IN, GPIO_Medium_Speed, GPIO_OType_PP, GPIO_PuPd_UP )|stop and reset|
|gpio_init (&PE8, GPIO_Mode_OUT , GPIO_Low_Speed , GPIO_OType_PP, GPIO_PuPd_UP )|ECU Failer|
|gpio_init (&PE11, GPIO_Mode_OUT, GPIO_Low_Speed, GPIO_OType_PP, GPIO_PuPd_NOPULL )|break light|
|gpio_write(&PE11, Bit_RESET)|break light|
|sr_init()||
|can_bl_init()||
|tft_LOGO()||
|tft_allclear()||

### Main Logic
1. 