This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo.h
93 lines (78 loc) · 3.15 KB
/
servo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef SERVO_H
#define SERVO_H
#include "MKL46Z4.h" // Device header
/**
@brief Define minimum PWM lenght which servo can tolerate
This macro defines the minimal PWM which can be used,
i.e. how far to the LEFT this servo can move
@warning Setting this value below 800 may damage the servo.
If you hear noisy clicking, power off the system!
*/
#define SERVO_MOVEMENT_MIN 2200*SERVO_TICKS_PER_US
/**
@brief Define maximum PWM lenght which servo can tolerate
This macro defines the maximal PWM which can be used,
i.e. how far to the RIGHT this servo can move
@warning Setting this value above 2200 may damage the servo.
If you hear noisy clicking, power off the system!
*/
#define SERVO_MOVEMENT_MAX 800*SERVO_TICKS_PER_US
/**
@brief Define how many degrees this servo can turn
This marco defines how far to the left and to the right can servo operate
It is used to map degrees into PWM width.
This value may differ from servo to servo. It is recomended to measure it yourself.
*/
#define SERVO_MOVEMENT_RANGE 82
/**
@brief Define sweep step in degrees
This marco defines a sweep step. Every step servo will rotate by this value.
*/
#define SERVO_STEP_DEG 15 /* Sweep step in degrees */
/**
@brief Define Servo angular velocity in deg/s
Due to lack of feedback from servo, we have to estimate its travel time
240deg/s is our observed velocity with a sonar as a load.
You can try tweeking this value for better performance in your setup
@warning Do not set this value too high as it may damage the servo
*/
#define SERVO_ANGULAR_VELOCITY 200
/**
@brief Define Servo work modes
*/
typedef enum { SWEEP, /**< Constant sweep with step ::SERVO_STEP_DEG */
MANUAL, /**< Manual operation. You can set servo position by ::ServoMoveByDegree */
} ServoMode_t;
/**
@brief Define Servo Sweep modes
*/
typedef enum { SCAN_AND_GO, /**<In this mode Sonar will scan each direction and proceed with next one */
SCAN_AND_LOCK, /**<In this mode Sonar will scan each direction and
lock when It finds something under ::ServoLockRange
When lock is lost it will procced with next direction */
} ServoSweep_t;
/**
@brief Define Servo states
@warning Due to lack of feedback, this states are only predictions! Real state may be different!
*/
typedef enum { MOVING, /**<Servo is moving */
IDLE, /**< Servo reached its final position and is idle */
LOCKED, /**< Servo is locked in ::SCAN_AND_LOCK sweep mode */
} ServoState_t;
typedef enum { SWEEP_LEFT,
SWEEP_RIGHT
}ServoSweepDir_t;
/* Global variables */
extern ServoMode_t ServoMode;
extern volatile ServoState_t ServoState;
extern volatile int32_t ServoPosition;
extern volatile ServoSweepDir_t ServoSweepDir;
/* Functions */
void Servo_init(ServoMode_t InitialWorkMode, ServoSweep_t InitialSweepMode);
void ServoMoveByDegree(int32_t degree);
void ServoSweepStep(uint16_t distance);
void ServoChangeMode(ServoMode_t NewMode);
void ServoChangeSweepMode(ServoSweep_t NewSweep);
void ServoChangeLockRange(uint16_t NewRange);
void ServoPITHandler(void);
#endif