Skip to content

Commit 1ec94a1

Browse files
committed
add GRBL13.2 Example code
1 parent 0193b36 commit 1ec94a1

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Description: GRBL 13.2 Module TEST Example.
3+
*/
4+
#include <M5Stack.h>
5+
#include "GrblControl.h"
6+
7+
/*
8+
* The I2C address of GRBL 13.2 Module is 0x70 by default.
9+
* You could use the DIP Switch for modify I2C address to 0x71
10+
*/
11+
12+
#define STEPMOTOR_I2C_ADDR 0x70
13+
// #define STEPMOTOR_I2C_ADDR 0x71
14+
15+
GRBL _GRBL = GRBL(STEPMOTOR_I2C_ADDR);
16+
17+
void setup() {
18+
// put your setup code here, to run once:
19+
M5.begin();
20+
M5.Power.begin();
21+
_GRBL.Init();
22+
Serial.begin(115200);
23+
m5.Lcd.setTextColor(WHITE, BLACK);
24+
m5.Lcd.setTextSize(3);
25+
m5.lcd.setBrightness(100);
26+
M5.Lcd.setCursor(80, 40);
27+
M5.Lcd.println("GRBL 13.2");
28+
M5.Lcd.setCursor(50, 80);
29+
M5.Lcd.println("Press Btn A/B");
30+
M5.Lcd.setCursor(50, 120);
31+
M5.Lcd.println("Control Motor");
32+
_GRBL.SetMode("absolute");
33+
}
34+
35+
void loop() {
36+
/*
37+
If Button A was pressed,
38+
stepmotor will rotate back and forth at a time
39+
*/
40+
if (M5.BtnA.wasPressed()) // A button
41+
{
42+
Serial.print(_GRBL.ReadStatus());
43+
_GRBL.SetMotor(5,5,5,200);
44+
_GRBL.SetMotor(0,0,0,200);
45+
}
46+
47+
if (M5.BtnB.wasPressed())
48+
{
49+
//USE Gcode
50+
_GRBL.Gcode("G1 X5Y5Z5 F200");
51+
_GRBL.Gcode("G1 X0Y0Z0 F200");
52+
}
53+
54+
if (M5.BtnC.wasReleased()) {
55+
_GRBL.UnLock();
56+
}
57+
M5.update();
58+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "GrblControl.h"
2+
#include <Wire.h>
3+
4+
GRBL::GRBL(int addr){
5+
this->addr = addr;
6+
}
7+
8+
void GRBL::Init()
9+
{
10+
Wire.begin();
11+
}
12+
13+
void GRBL::Init(uint32_t x_step,uint32_t y_step,uint32_t z_step,uint32_t acc)
14+
{
15+
Wire.begin();
16+
if(x_step){
17+
char code[256];
18+
sprintf(code,"$0=%d",x_step);
19+
Gcode(code);
20+
}
21+
if(x_step){
22+
char code[256];
23+
sprintf(code,"$1=%d",y_step);
24+
Gcode(code);
25+
}
26+
if(x_step){
27+
char code[256];
28+
sprintf(code,"$2=%d",z_step);
29+
Gcode(code);
30+
}
31+
if(acc){
32+
char code[256];
33+
sprintf(code,"$8=%d",acc);
34+
Gcode(code);
35+
}
36+
}
37+
38+
void GRBL::Gcode(char *c)
39+
{
40+
Wire.beginTransmission(addr);
41+
while ((*c) != 0) {
42+
Wire.write(*c);
43+
c++;
44+
}
45+
Wire.write(0x0d);
46+
Wire.write(0x0a);
47+
Wire.endTransmission();
48+
}
49+
50+
void GRBL::SendByte(byte b) {
51+
Wire.beginTransmission(addr);
52+
Wire.write(b);
53+
Wire.endTransmission();
54+
}
55+
56+
void GRBL::SendBytes(uint8_t *data, size_t size) {
57+
Wire.beginTransmission(addr);
58+
Wire.write(data,size);
59+
Wire.endTransmission();
60+
}
61+
62+
void GRBL::ReadClean(){
63+
while(1){
64+
uint8_t i = 0;
65+
char data[10];
66+
Wire.requestFrom(addr, 10);
67+
while (Wire.available() > 0) {
68+
data[i++] = Wire.read();
69+
}
70+
if(data[9] == 0xff) break;
71+
}
72+
}
73+
74+
void GRBL::UnLock() {
75+
this->SendByte(0x18);
76+
delay(5);
77+
char bytes[] = "$X\r\n";
78+
this->SendBytes((uint8_t *)bytes, 4);
79+
}
80+
81+
void GRBL::SetMotor(int x, int y, int z, int speed) {
82+
83+
char code[256];
84+
memset(code,0,sizeof(char)*256);
85+
sprintf(code,"G1 X%dY%dZ%d F%d",x,y,z,speed);
86+
return this->Gcode(code);
87+
}
88+
89+
void GRBL::SetMode(String mode){
90+
if(mode == "distance"){
91+
char bytes[] = "G91\n";
92+
this->SendBytes((uint8_t *)bytes, 4);
93+
this->mode = mode;
94+
}else if(mode == "absolute"){
95+
char bytes[] = "G90\n";
96+
this->SendBytes((uint8_t *)bytes, 4);
97+
this->mode = mode;
98+
}
99+
}
100+
101+
void GRBL::WaitIdle(){
102+
this->ReadClean();
103+
while(1){
104+
this->SendByte('@');
105+
char state;
106+
Wire.requestFrom(addr, 1);
107+
if (Wire.available() > 0) {
108+
state = Wire.read();
109+
}
110+
if(state == 'I'){
111+
break;
112+
}
113+
delay(5);
114+
}
115+
}
116+
117+
//read grbl return message
118+
119+
String GRBL::ReadLine() {
120+
String Data = "";
121+
while(1){
122+
uint8_t i = 0;
123+
char data[10];
124+
Wire.requestFrom(addr, 10);
125+
while (Wire.available() > 0) {
126+
data[i] = Wire.read();
127+
i++;
128+
}
129+
Data += data;
130+
if (data[9] == 0xff) {
131+
break;
132+
}
133+
}
134+
return Data;
135+
}
136+
137+
String GRBL::ReadStatus() {
138+
this->ReadClean();
139+
this->SendByte('@');
140+
return this->ReadLine();
141+
}
142+
143+
// read grbl state
144+
bool GRBL::ReadIdle() {
145+
return this->ReadStatus()[0] == 'I';
146+
}
147+
148+
bool GRBL::InLock() {
149+
return this->ReadStatus()[0] == 'A';
150+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <M5Stack.h>
2+
3+
class GRBL
4+
{
5+
6+
private:
7+
void SendByte(byte b);
8+
void SendBytes(uint8_t *data, size_t size);
9+
public:
10+
11+
GRBL(int addr=0x70);
12+
void Init();
13+
void Init(uint32_t x_step, uint32_t y_step,uint32_t z_step,uint32_t acc);
14+
int addr;
15+
String mode;
16+
void Gcode(char *c);
17+
void UnLock();
18+
void ReadClean();
19+
void WaitIdle();
20+
void SetMotor(int x=0, int y=0, int z=0, int speed=300);
21+
void SetMode(String mode);
22+
String ReadLine();
23+
String ReadStatus();
24+
bool ReadIdle();
25+
bool InLock();
26+
};

0 commit comments

Comments
 (0)