-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKB_Motor.cpp
More file actions
170 lines (140 loc) · 3.64 KB
/
KB_Motor.cpp
File metadata and controls
170 lines (140 loc) · 3.64 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Dependencies */
#include <Wire.h>
#include "KB_Motor.h"
KB_MOTOR::KB_MOTOR()
{
}
void KB_MOTOR::begin(uint8_t address1, uint8_t address2) {
/* Store the I2C address and init the Wire library */
_address1 = address1;
_address2 = address2;
Wire.begin(4, 5); // join another i2c bus edit KB_Relay.h default for i2c KidBright32
// WriteCommand(0x00, 0x00);
Wire.beginTransmission(_address1);
Wire.write(0x01);
Wire.write(0b100000000);
Wire.endTransmission();
Wire.beginTransmission(_address2);
Wire.write(0x01);
Wire.write(0b100000000);
Wire.endTransmission();
}
void KB_MOTOR::WriteCommand1(uint8_t level1, uint8_t cw_or_ccw1) {
int m1 = level1 << 2 | cw_or_ccw1;
Wire.beginTransmission(0x66);
Wire.write(0x00);
Wire.write(m1);
// Serial.println(m, DEC);
Wire.endTransmission(true);
// Serial.println(Wire.endTransmission(true));
}
void KB_MOTOR::WriteCommand2(uint8_t level2, uint8_t cw_or_ccw2) {
int m2 = level2 << 2 | cw_or_ccw2;
Wire.beginTransmission(0x68);
Wire.write(0x00);
Wire.write(m2);
// Serial.println(m, DEC);
Wire.endTransmission(true);
// Serial.println(Wire.endTransmission(true));
}
//speed 0-63
void KB_MOTOR::i2c_motor_write(int ch, int mspeed, int stat) {
int _ch = ch;
uint8_t _mspeed = mspeed / 4;
int _stat = stat;
uint8_t temp_stat;
int flag_stat = 0;
if (_stat >= 0) {
if (_stat == 0) {
temp_stat = 0x00;
flag_stat = 1;
} else if (_stat == 1) {
temp_stat = 0x02;
flag_stat = 1;
} else if (_stat == 2) {
temp_stat = 0x01;
flag_stat = 1;
} else {
Serial.println("format error");
flag_stat = 0;
}
}
if (flag_stat == 1 ) {
if (_mspeed > 63) {
Serial.println("Over Speed!!!!");
flag_stat = 0;
} else if (_mspeed < 0 ) {
Serial.println("Lower Speed!!!!");
flag_stat = 0;
} else {
switch (_ch)
{
case 1:
/* code */
WriteCommand1(_mspeed, temp_stat);
flag_stat = 0;
break;
case 2:
/* code */
WriteCommand2(_mspeed, temp_stat);
flag_stat = 0;
break;
default:
Serial.println("Channel Error");
flag_stat = 0;
break;
}
flag_stat = 0;
}
}
}
void KB_MOTOR::faultCheck1(int _address1) {
int _addr1 = _address1;
Wire.beginTransmission(_addr1);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(_addr1, 1);
byte error = Wire.read();
// printf(error);
// printf("\n");
if (error & 0b00000011) {
Serial.println("Over current1");
}
if (error & 0b00000101) {
// Serial.println("Low voltage1");
}
if (error & 0b00001001) {
Serial.println("Over Temperature1");
}
if (error & 0b00010001) {
Serial.println("Limit1");
}
Wire.beginTransmission(_addr1);
Wire.write(0x01);
Wire.write(0b100000000);
Wire.endTransmission();
}
void KB_MOTOR::faultCheck2(int _address2) {
int _addr2 = _address2;
Wire.beginTransmission(_addr2);
Wire.write(0x01);
Wire.endTransmission();
Wire.requestFrom(_addr2, 1);
byte error = Wire.read();
if (error & 0b00000011) {
Serial.println("Over current2");
}
if (error & 0b00000101) {
// Serial.println("Low voltage2");
}
if (error & 0b00001001) {
Serial.println("Over Temperature2");
}
if (error & 0b00010001) {
Serial.println("Limit2");
}
Wire.beginTransmission(_addr2);
Wire.write(0x01);
Wire.write(0b100000000);
Wire.endTransmission();
}