-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbridge.ino
62 lines (52 loc) · 952 Bytes
/
hbridge.ino
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
#include "hbridge.h"
#include "utils.h"
HBridge::HBridge(uint8_t pwmPin, uint8_t dirPin, uint8_t brakePin):
pwmPin(pwmPin),
dirPin(dirPin),
brakePin(brakePin)
{
}
void HBridge::setup()
{
pinMode(pwmPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(brakePin, OUTPUT);
digitalWrite(brakePin,LOW);
analogWriteFrequency(pwmPin, PWM_FREQ);
}
void HBridge::setSpeed(int32_t newSpeed)
{
int pwm;
int direction;
speed=MIN(HB_VMAX,MAX(HB_VMIN,newSpeed));
//avoid annoying noise when speed is to low to make
//the motor actually move
if(abs(speed)<ZONE_51)
speed=0;
if (speed>0)
{
direction=1;
pwm=speed;
}else{
direction=0;
pwm=-speed;
}
// DUMP_VAR(pwm);
analogWrite(pwmPin,pwm);
digitalWrite(dirPin,direction);
}
int HBridge::getSpeed()
{
return speed;
}
void HBridge::brakeOn()
{
analogWrite(pwmPin,255);
speed = 0;
digitalWrite(brakePin,HIGH);
}
void HBridge::brakeOff()
{
setSpeed(0);
digitalWrite(brakePin,LOW);
}