Skip to content

Commit

Permalink
update 4 files and delete 4 files
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen-Lung-Huang committed Jul 11, 2024
1 parent 0d60e7d commit d9362e4
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 218 deletions.
2 changes: 1 addition & 1 deletion Application_Library/Best_System_Robot/Include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "C" {
#include "servo.h"
#include "74HC595.h"
#include "motor_shield.h"
#include "move.h"
#include "motor_control.h"
#include "sensor.h"
#include "robotic_arm.h"

Expand Down
25 changes: 25 additions & 0 deletions Application_Library/Best_System_Robot/Include/motor_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef MOTOR_CONTROL_H
#define MOTOR_CONTROL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "motor_types.h"
#include "motor_shield.h"

extern const MotorThresholds WHEEL_THRESHOLDS[];

// Function prototypes
void adjust_thresholds(DC_Motor_TypeDef *motor, bool success);
void soft_motor_control(void *motor_shield, Motor_Shield_Type type, uint8_t dc_motor_number, int target_speed);
void adjust_motor_parameters(DC_Motor_TypeDef *motor, MovementMode mode);
void set_wheel_modes(MovementMode mode);
void set_wheel_speeds(int m1, int m2, int m3, int m4);
void set_wheel_speeds_with_mode(int m1, int m2, int m3, int m4, MovementMode mode);

#ifdef __cplusplus
}
#endif

#endif /* MOTOR_CONTROL_H */
46 changes: 0 additions & 46 deletions Application_Library/Best_System_Robot/Include/motor_controller.h

This file was deleted.

44 changes: 23 additions & 21 deletions Application_Library/Best_System_Robot/Include/motor_shield.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ extern "C" {
#include "servo.h"
#include "74HC595.h"
#include "timing_delays.h"
#include "motor_controller.h"
#include "motor_types.h"

/* MACRO DEFINITIONS */
#define DC_MOTOR_MIN 0
#define DC_MOTOR_MAX 999
#define MAX_ACCELERATION 1000
#define MAX_ADJUSTMENT_FACTOR 0.8f
#define MIN_ADJUSTMENT_FACTOR 0.3f
#define STOP_ACCELERATION_FACTOR 1.5f // Increase deceleration when stopping
#define USE_BACK_EMF_COMPENSATION 0 // Set to 1 to enable back EMF compensation


// Define a structure for EN pin
Expand Down Expand Up @@ -68,37 +75,32 @@ typedef struct {
} L29XX;


// Declare the global objects with extern keyword
// Declare global objects
extern Motor_Shield_V1 motor_shield_v1;
extern Motor_Shield_L29XX motor_shield_l29xx;

// Define an enum type to indicate the type of motor shield
enum Motor_Shield_Type {MS_V1, MS_L29XX};
// // Define an enum type to indicate the type of motor shield
// enum Motor_Shield_Type {MS_V1, MS_L29XX};

// Define an enum type variable to store the motor names and values
enum Motor_Shield_Motor {M1, M2, M3, M4};
// // Define an enum type variable to store the motor names and values
// enum Motor_Shield_Motor {M1, M2, M3, M4};

// Define an enum type variable to store the servo names and values
// enum motor_shield_v1_servo {S1, S2};
// // Define an enum type variable to store the servo names and values
// // enum motor_shield_v1_servo {S1, S2};


/* FUNCTION (Prototype) DEFINITIONS */
DC_Motor_TypeDef *get_dc_motor(void *motor_shield, enum Motor_Shield_Type type, uint8_t dc_motor_number);

void ms_init(enum Motor_Shield_Type type, bool enable_pwm);
// Function prototypes
void ms_init(Motor_Shield_Type type, bool enable_pwm);
void init_motor_shield_v1();
void init_motor_shield_l29xx();
void ms_pwm_init(enum Motor_Shield_Type type);
void ms_gpio_init(enum Motor_Shield_Type type);
void ms_encoder_init(enum Motor_Shield_Type type);

void ms_pwm_init(Motor_Shield_Type type);
void ms_gpio_init(Motor_Shield_Type type);
void ms_encoder_init(Motor_Shield_Type type);
DC_Motor_TypeDef *get_dc_motor(void *motor_shield, Motor_Shield_Type type, uint8_t dc_motor_number);
uint8_t get_motor_bit(uint8_t dc_motor_number, uint8_t bit_index);
void set_motor_speed(void *motor_shield, enum Motor_Shield_Type type, uint8_t dc_motor_number, int target_speed);

void ms_motor_control(void *motor_shield, enum Motor_Shield_Type type, uint8_t dc_motor_number, float motor_input);
void set_motor_speed(void *motor_shield, Motor_Shield_Type type, uint8_t dc_motor_number, int target_speed);
void ms_motor_control(void *motor_shield, Motor_Shield_Type type, uint8_t dc_motor_number, float motor_input);
void ms_v1_servo_control(Motor_Shield_V1 *motor_shield, uint8_t servo_number, float servo_input, bool mode);
void soft_motor_control(void *motor_shield, enum Motor_Shield_Type type, uint8_t dc_motor_number, int target_speed);


#ifdef __cplusplus
}
Expand Down
65 changes: 65 additions & 0 deletions Application_Library/Best_System_Robot/Include/motor_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef MOTOR_TYPES_H
#define MOTOR_TYPES_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdint.h>

typedef enum {
MOVE,
TURN,
STRAFE,
DIAGONAL
} MovementMode;

typedef enum {
MS_V1,
MS_L29XX
} Motor_Shield_Type;

typedef enum {
M1,
M2,
M3,
M4
} Motor_Shield_Motor;

typedef struct {
int static_friction_threshold;
int low_speed_threshold;
int success_count;
int failure_count;
float kp;
float ki;
float kd;
float k_back_emf; // Added this from your original code
} MotorThresholds;

typedef struct {
int target_speed;
int current_speed;
bool static_friction_overcome;
float integral_error;
float previous_error;
uint32_t last_update_time;
MotorThresholds thresholds;
MovementMode mode;
} MotorController;

// // Forward declarations
// typedef struct My_GPIO_TypeDef My_GPIO_TypeDef;
// typedef struct PWM_TypeDef PWM_TypeDef;
// typedef struct DC_Motor_TypeDef DC_Motor_TypeDef;
// typedef struct Motor_Shield_V1 Motor_Shield_V1;
// typedef struct Motor_Shield_L29XX Motor_Shield_L29XX;
// typedef struct NonBlockingDelay_TypeDef NonBlockingDelay_TypeDef;
// typedef struct HC595 HC595;

#ifdef __cplusplus
}
#endif

#endif /* MOTOR_TYPES_H */
Loading

0 comments on commit d9362e4

Please sign in to comment.