Khangai Robot Play - Naive  01
THe naive play of the Khangai Robot
wheel.h
Go to the documentation of this file.
1 /*
2  * wheel.h
3  *
4  * Created : 10/1/2018
5  * Author : n-is
6  * email : 073bex422.nischal@pcampus.edu.np
7  */
8 
9 #ifndef _WHEEL_H_
10 #define _WHEEL_H_
11 
12 #include "stm32f4xx_hal.h"
13 #include "tim.h"
14 
15 #include "defines.h"
16 
17 #include "pid_algorithms.h"
18 #include "pid.h"
19 
20 #include "logger.h"
21 
23 {
27 };
28 
30 {
31  uint8_t id;
32  float radius;
33 
34  GPIO_TypeDef *in1_port;
35  uint16_t in1_pin;
36  GPIO_TypeDef *in2_port;
37  uint16_t in2_pin;
38 
39  TIM_HandleTypeDef *htim; // PWM Generating Timer
40  uint32_t channel; // PWM Channel
41 
42  TIM_HandleTypeDef *henc;
43 
44  float tolerance; // Tolerance level for zero
45  float max_omega; // Max Omega Attained by the wheel(motor)
46  uint32_t enc_ppr;
47 
48  PID *pid_controller; // Pointer to the wheel's PID controller
49 };
50 
51 class Wheel
52 {
53 public:
54  Wheel() { wheel_ = 0; }
55  Wheel(Wheel_Config *wheel) { wheel_ = wheel; }
56  Wheel(Wheel &&) = default;
57  Wheel(const Wheel &) = default;
58  Wheel &operator=(Wheel &&) = default;
59  Wheel &operator=(const Wheel &) = default;
60  ~Wheel() { }
61 
62  void set_Config(Wheel_Config *wheel) { wheel_ = wheel; }
63  void set_Direction(enum Direction d) { dir_ = d; }
64  void set_Omega(float omega);
65  void update() const;
66 
67  float get_Omega(uint32_t dt_millis);
68  float get_MaxOmega() const { return wheel_->max_omega; }
69 
70  void start_Periphs();
71 
72  void set_PIDController(PID *pid) { wheel_->pid_controller = pid; }
74  return wheel_->pid_controller;
75  }
76 
77  void log(float omega, float new_omega) {
78  // Motor Packet is of the following form
79  // START_BYTE MOTOR_PACKET_ID MOTOR_ID SCALED_OMEGA SCALED_NEW_OMEGA TIME_HIGH_BYTE TIME_LOW_BYTE
80  // A total of 7 bytes including start byte
81  // uint32_t curr_time = HAL_GetTick();
82 
83  gLogging_Buffer.insert((uint8_t)(START_BYTE));
84  gLogging_Buffer.insert((uint8_t)(MOTOR_PACKET_ID));
85  gLogging_Buffer.insert((uint8_t)(wheel_->id));
86  gLogging_Buffer.insert((int8_t)(omega));
87  gLogging_Buffer.insert((int8_t)(new_omega));
88 
89  // uint8_t time_h = (uint8_t)(curr_time >> (8+4));
90  // uint8_t time_l = (uint8_t)(curr_time >> 4);
91  // gLogging_Buffer.insert(time_h);
92  // gLogging_Buffer.insert(time_l);
93 
94  // printf("%d, %d, %d, %d\n", (int8_t)(omga), (int8_t)(n_omga), time_h, time_l);
95  }
96 
97  void clear() {
99  }
100 
101 private:
104  float omega_; // This value should always be positive
105 };
106 
107 
108 #endif // _WHEEL_H_
Definition: wheel.h:24
Definition: wheel.h:25
~Wheel()
Definition: wheel.h:60
Wheel()
Definition: wheel.h:54
TIM_HandleTypeDef * htim
Definition: wheel.h:39
void set_PIDController(PID *pid)
Definition: wheel.h:72
void set_Config(Wheel_Config *wheel)
Definition: wheel.h:62
#define START_BYTE
Definition: defines.h:13
Wheel(Wheel_Config *wheel)
Definition: wheel.h:55
TIM_HandleTypeDef * henc
Definition: wheel.h:42
Definition: wheel.h:51
Wheel & operator=(Wheel &&)=default
uint16_t in1_pin
Definition: wheel.h:35
Definition: wheel.h:26
uint32_t channel
Definition: wheel.h:40
Direction
Definition: wheel.h:22
float radius
Definition: wheel.h:32
void clear()
Definition: wheel.h:97
void update() const
Definition: wheel.cpp:110
Queue< uint8_t, LOG_BUFFER_SIZE > gLogging_Buffer
Definition: logger.cpp:13
float max_omega
Definition: wheel.h:45
uint8_t id
Definition: wheel.h:31
Definition: wheel.h:29
void set_Direction(enum Direction d)
Definition: wheel.h:63
#define MOTOR_PACKET_ID
Definition: defines.h:16
PID * pid_controller
Definition: wheel.h:48
uint16_t in2_pin
Definition: wheel.h:37
Wheel_Config * wheel_
Definition: wheel.h:102
Definition: pid.h:14
PID * get_PIDController()
Definition: wheel.h:73
void clear()
Definition: pid.h:32
void start_Periphs()
Function that starts timer base, pwm and encoder of the wheel.
Definition: wheel.cpp:71
float get_MaxOmega() const
Definition: wheel.h:68
float omega_
Definition: wheel.h:104
float tolerance
Definition: wheel.h:44
GPIO_TypeDef * in1_port
Definition: wheel.h:34
enum Direction dir_
Definition: wheel.h:103
void set_Omega(float omega)
Function that sets the angular velocity of the wheel.
Definition: wheel.cpp:95
void log(float omega, float new_omega)
Definition: wheel.h:77
GPIO_TypeDef * in2_port
Definition: wheel.h:36
uint32_t enc_ppr
Definition: wheel.h:46
float get_Omega(uint32_t dt_millis)
Definition: wheel.cpp:125