-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActuator.cpp
125 lines (107 loc) · 2.97 KB
/
Actuator.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Actuator.cpp
*
* Created on: Nov 7, 2013
* Author: dominik
*/
#include "PirateDotty.h"
#include "Actuator.h"
#include "Pinout.h"
typedef enum directions {
FORWARD, BACKWARD, LEFT, RIGHT
};
void initActuators() {
pinMode(MOTORAIN1, OUTPUT);
pinMode(MOTORAIN2, OUTPUT);
pinMode(MOTORBIN1, OUTPUT);
pinMode(MOTORBIN2, OUTPUT);
pinMode(MOTORSTDBY, OUTPUT);
pinMode(MOTORAPWM, OUTPUT);
pinMode(MOTORBPWM, OUTPUT);
pinMode(STATE_LED, OUTPUT);
}
// --------------------------------------------------------------------
// Motor Utility Functions
void setmotors(int AIN1,int AIN2,int BIN1,int BIN2) {
digitalWrite(MOTORAIN1,AIN1);
digitalWrite(MOTORAIN2,AIN2);
digitalWrite(MOTORBIN1,BIN1);
digitalWrite(MOTORBIN2,BIN2);
}
// --------------------------------------------------------------------
void moveBot(int straightpow, int powdiff)
{
// uses 2 component speed decomposition
// int straightpow describes the forward (or backward) power
// -255 = backward full power, 255 = forward full power
// int powdiff describes the difference in power between wheels
// powdiff e [-255,255]
digitalWrite(MOTORSTDBY,LOW); // Disable Motors
int L = capSpeed(straightpow+powdiff);
int R = capSpeed(straightpow-powdiff);
setDirection(&L, &R);
analogWrite(MOTORBPWM,R);
analogWrite(MOTORAPWM,L);
digitalWrite(MOTORSTDBY,HIGH); // Enable Motors
}
void setDirection(int* leftSpeed, int* RightSpeed)
{
if (*leftSpeed>0)
{
if (*RightSpeed>0)
setmotors(HIGH, LOW, HIGH, LOW); /*Forward*/
else
{
setmotors(HIGH, LOW, LOW, HIGH); /*Left forw, Right back*/
*RightSpeed *= -1;
}
}
else
{
*leftSpeed *= -1;
if (*RightSpeed>0)
setmotors(LOW, HIGH, HIGH, LOW); /*Right forw, Left back*/
else
{
setmotors(LOW, HIGH, LOW, HIGH); /*Backward*/
*RightSpeed *= -1;
}
}
}
// --------------------------------------------------------------------
int capSpeed(int value)
{
return max(min(value,255),-255);
}
// --------------------------------------------------------------------
void drive(int leftSpeed, int rightSpeed)
{
int left = capSpeed(leftSpeed);
int right = capSpeed(rightSpeed);
LOGd(3, "drive(%d, %d)", leftSpeed, rightSpeed);
setDirection(&left, &right);
analogWrite(MOTORAPWM, left);
analogWrite(MOTORBPWM, right);
digitalWrite(MOTORSTDBY,HIGH); // enable motors
}
// --------------------------------------------------------------------
void domove(int drivespeed) {
digitalWrite(MOTORSTDBY,HIGH); // enable motors
analogWrite(MOTORAPWM, drivespeed);
analogWrite(MOTORBPWM, drivespeed);
}
// --------------------------------------------------------------------
void dostop() {
// LOGd("\tmotor stop");
digitalWrite(MOTORSTDBY,LOW); // disnable motors
}
// --------------------------------------------------------------------
void ledON(int ledPin)
{
digitalWrite(ledPin, HIGH); // sets the LED on
}
// --------------------------------------------------------------------
void ledOFF(int ledPin)
{
digitalWrite(ledPin, LOW); // sets the LED off
}