replace magic number

This commit is contained in:
Richie 2023-11-10 10:48:45 +08:00
parent cb80e7f883
commit c4908eb6f2
2 changed files with 18 additions and 11 deletions

View File

@ -13,11 +13,13 @@
#define DIRECTION_PIN_LEFT_IN3 19U #define DIRECTION_PIN_LEFT_IN3 19U
#define DIRECTION_PIN_LEFT_IN4 20U #define DIRECTION_PIN_LEFT_IN4 20U
// to turn one side
#define DIRECTION_RIGHT_FORWARD (1U << DIRECTION_PIN_RIGHT_IN2) #define DIRECTION_RIGHT_FORWARD (1U << DIRECTION_PIN_RIGHT_IN2)
#define DIRECTION_RIGHT_BACKWARD (1U << DIRECTION_PIN_RIGHT_IN1) #define DIRECTION_RIGHT_BACKWARD (1U << DIRECTION_PIN_RIGHT_IN1)
#define DIRECTION_LEFT_FORWARD (1U << DIRECTION_PIN_LEFT_IN4) #define DIRECTION_LEFT_FORWARD (1U << DIRECTION_PIN_LEFT_IN4)
#define DIRECTION_LEFT_BACKWARD (1U << DIRECTION_PIN_LEFT_IN3) #define DIRECTION_LEFT_BACKWARD (1U << DIRECTION_PIN_LEFT_IN3)
// to spin
#define DIRECTION_FORWARD (DIRECTION_LEFT_FORWARD | DIRECTION_RIGHT_FORWARD) #define DIRECTION_FORWARD (DIRECTION_LEFT_FORWARD | DIRECTION_RIGHT_FORWARD)
#define DIRECTION_BACKWARD (DIRECTION_LEFT_BACKWARD | DIRECTION_RIGHT_BACKWARD) #define DIRECTION_BACKWARD (DIRECTION_LEFT_BACKWARD | DIRECTION_RIGHT_BACKWARD)
#define DIRECTION_LEFT (DIRECTION_LEFT_BACKWARD | DIRECTION_RIGHT_FORWARD) #define DIRECTION_LEFT (DIRECTION_LEFT_BACKWARD | DIRECTION_RIGHT_FORWARD)
@ -25,15 +27,24 @@
#define DIRECTION_MASK (DIRECTION_FORWARD | DIRECTION_BACKWARD) #define DIRECTION_MASK (DIRECTION_FORWARD | DIRECTION_BACKWARD)
// wheel encoder sensor pins
#define SPEED_PIN_RIGHT 15U #define SPEED_PIN_RIGHT 15U
#define SPEED_PIN_LEFT 16U #define SPEED_PIN_LEFT 16U
// PWM parameters
#define PWM_CLK_DIV 50.f #define PWM_CLK_DIV 50.f
#define PWM_WRAP 100U #define PWM_WRAP 100U
#define MAX_PWM_LEVEL 99U #define MAX_PWM_LEVEL 99U
#define MIN_PWM_LEVEL 0U #define MIN_PWM_LEVEL 0U
// speed in cm/s; speed = distance / time
// distance = circumference / 20
// circumference = 2 * pi * 3.25 cm = 20.4203522483 cm
// distance = 20.4203522483 cm / 20 = 1.02101761242 cm
#define SLOT_DISTANCE_CM 1.02101761242f
#define SLOT_DISTANCE_CM_MODIFIED (SLOT_DISTANCE_CM * 1000000.f)
/*! /*!
* @brief Structure for the motor speed parameters * @brief Structure for the motor speed parameters
* @param current_speed_cms Current speed in cm/s * @param current_speed_cms Current speed in cm/s
@ -83,10 +94,10 @@ typedef struct
*/ */
typedef struct typedef struct
{ {
motor_speed_t speed; motor_speed_t speed;
motor_pwm_t pwm; motor_pwm_t pwm;
SemaphoreHandle_t * p_sem; SemaphoreHandle_t *p_sem;
bool * use_pid; bool *use_pid;
} motor_t; } motor_t;
@ -101,7 +112,7 @@ SemaphoreHandle_t g_left_sem;
SemaphoreHandle_t g_right_sem; SemaphoreHandle_t g_right_sem;
// for testing // for testing
//typedef struct // typedef struct
//{ //{
// motor_t * p_left_motor; // motor_t * p_left_motor;
// motor_t * p_right_motor; // motor_t * p_right_motor;

View File

@ -60,14 +60,10 @@ monitor_wheel_speed_task(void *ppp_motor)
elapsed_time = curr_time - prev_time; elapsed_time = curr_time - prev_time;
prev_time = curr_time; prev_time = curr_time;
// speed in cm/s; speed = distance / time
// distance = circumference / 20
// circumference = 2 * pi * 3.25 cm = 20.4203522483 cm
// distance = 20.4203522483 cm / 20 = 1.02101761242 cm
p_motor->speed.current_cms p_motor->speed.current_cms
= (float) (1021017.61242f / elapsed_time); = (float) (SLOT_DISTANCE_CM_MODIFIED / elapsed_time);
p_motor->speed.distance_cm += 1.02101761242f; p_motor->speed.distance_cm += SLOT_DISTANCE_CM;
printf("speed\n"); printf("speed\n");
} }