Skip to content

Commit 4691b15

Browse files
author
GrAndAG
committed
Added support of Adafruin Motor Shield v1
1 parent c07ccc2 commit 4691b15

7 files changed

+132
-55
lines changed

Configuration.h

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2016 by GrAndAG
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>
16+
*/
17+
18+
19+
#ifndef Configuration_h
20+
#define Configuration_h
21+
22+
/*
23+
* Version of Adafruit motor shield used
24+
*/
25+
26+
#define ADAFRUIT_MOTOR_SHIELD_VERSION 1
27+
//#define ADAFRUIT_MOTOR_SHIELD_VERSION 2
28+
29+
30+
/*
31+
* PINS definitions
32+
* Pen arm motor terminals : M1 & M2
33+
* Rotation motor terminals : M3 & M4
34+
* Arm servo port : SER1 (located closer to PCB corner
35+
*/
36+
37+
#define PEN_AXIS_PORT 1
38+
#define ROTATION_AXIS_PORT 2
39+
40+
#define SERVO_PIN 10
41+
42+
/*
43+
* Steppers Congiguration
44+
*/
45+
46+
/* Full steps per revolution. Well known NEMA17 1.8 degrees motors have 200 steps. */
47+
#define STEPS_PER_REVOLUTION 200
48+
49+
/* Suitable for Eggbot template and 200 steps/rev steppers at 16x microstepping. */
50+
#define DEFAULT_ZOOM_FACTOR 1.0
51+
52+
/*
53+
* Pen Arm Configuration
54+
*/
55+
56+
/* Pen servo gets clamped to these values. */
57+
#define MIN_PEN_POSITION 100
58+
#define MAX_PEN_POSITION 130
59+
60+
/* Default pen up position. */
61+
#define PEN_UP_POSITION 107
62+
63+
/* How long to take for pen down moves in ms. */
64+
#define PEN_DOWN_MOVE_TIME 200
65+
66+
/* X axis gets clamped to these values to prevent inadvertent damage. */
67+
#define MIN_PEN_AXIS_STEP -480
68+
#define MAX_PEN_AXIS_STEP 480
69+
70+
71+
72+
/* Version dependent stuff */
73+
#if ADAFRUIT_MOTOR_SHIELD_VERSION == 1
74+
75+
#include <AFMotor.h>
76+
77+
#define ADAFRUIT_CLASS AF_Stepper
78+
#define ONE_STEP_TIME 168
79+
// steps/s. A no-delay loop takes 0.17 ms per step, so this is the fastest we can go.
80+
#define MAX_FEEDRATE 2900.0
81+
82+
#else
83+
84+
#include <Adafruit_MotorShield.h>
85+
//#include <utility/Adafruit_MS_PWMServoDriver.h>
86+
87+
#define ADAFRUIT_CLASS Adafruit_StepperMotor
88+
#define ONE_STEP_TIME 1290
89+
// steps/s. A no-delay loop takes 1.29 ms per step, so this is the fastest we can go.
90+
#define MAX_FEEDRATE 775.0
91+
92+
#endif
93+
94+
95+
#if ARDUINO >= 100
96+
#include "Arduino.h"
97+
#else
98+
#include "WProgram.h"
99+
#endif
100+
101+
#endif /* Configuration_h */

DualStepper.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,10 @@ DualStepper::moveTo(int ax, int ay, float speed)
114114
void
115115
DualStepper::plotLine(SingleStepper *xAxis, SingleStepper *yAxis, uint8_t xdir, uint8_t ydir, unsigned int dx, unsigned int dy)
116116
{
117-
unsigned long usPerStep = 1000000L / majorAxisSpeed;
118-
117+
unsigned long usPerStep, usDelay = (1000000L / majorAxisSpeed) - ONE_STEP_TIME;
119118
// Serial.print("delay: ");
120119
// Serial.println(usPerStep);
121120

122-
if (usPerStep >= 1290)
123-
usPerStep = usPerStep - 1290; // loop takes 1.29 ms without any delay.
124-
else
125-
usPerStep = 0;
126-
127121
int error = 2 * dy - dx;
128122

129123
unsigned int y = 0;
@@ -134,8 +128,10 @@ DualStepper::plotLine(SingleStepper *xAxis, SingleStepper *yAxis, uint8_t xdir,
134128
xAxis->step(xdir);
135129
if (error > 0) {
136130
yAxis->step(ydir);
131+
usPerStep = usDelay - ONE_STEP_TIME;
137132
error += 2 * dy - 2 * dx;
138133
} else {
134+
usPerStep = usDelay;
139135
error += 2 * dy;
140136
}
141137
if (usPerStep > 0)

DualStepper.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef DualStepper_h
1919
#define DualStepper_h
2020

21+
#include "Configuration.h"
2122
#include "SingleStepper.h"
2223

2324
class DualStepper

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This is a fork of Eberhard Rensch's SphereBot firmware. This version has been mo
1010

1111
**IMPORTANT**: This sketch needs the following non-standard library (install them in the Arduino library directory):
1212

13+
* Adafruit Motor Shield v1: https://github.com/adafruit/Adafruit-Motor-Shield-library
14+
OR
1315
* Adafruit Motor Shield v2: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
1416

1517
To use:

SingleStepper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "SingleStepper.h"
1919

20-
SingleStepper::SingleStepper(Adafruit_StepperMotor *sm)
20+
SingleStepper::SingleStepper(ADAFRUIT_CLASS *sm)
2121
{
2222
stepper = sm;
2323
pos = 0;

SingleStepper.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
#ifndef SingleStepper_h
1919
#define SingleStepper_h
2020

21-
#include <Adafruit_MotorShield.h>
21+
#include "Configuration.h"
2222

2323
class SingleStepper {
2424
public:
25-
SingleStepper(Adafruit_StepperMotor *sm);
25+
SingleStepper(ADAFRUIT_CLASS *sm);
26+
2627
void step(uint8_t dir);
2728
void release() { stepper->release(); }
2829

2930
volatile int pos;
3031
int targetPos;
3132

3233
protected:
33-
Adafruit_StepperMotor *stepper;
34+
ADAFRUIT_CLASS *stepper;
3435
};
3536

36-
#endif
37+
#endif

SphereBot.ino

+19-43
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@
1818
* by Martin Price <http://www.HeliumFrog.com>
1919
*
2020
* Updated to run on Adafruit motor shield by Jin Choi <[email protected]>.
21+
* Utated to support both versions of Adafruit motor shield by GrAndAG
2122
*
2223
* !!!!!!!!
2324
* This sketch needs the following non-standard library (install it in the Arduino library directory):
2425
25-
* Adafruit Motor Shield: https://github.com/adafruit/Adafruit-Motor-Shield-library
26+
* Adafruit Motor Shield:
27+
* v1: https://github.com/adafruit/Adafruit-Motor-Shield-library
28+
* v2: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
29+
*
30+
* Also tune your configuration in "Configuration.h" file.
31+
*
2632
* !!!!!!!!
2733
*/
2834

29-
/* Adafruit Motor Shield libraries */
35+
#include "Configuration.h"
3036
#include <Wire.h>
31-
#include <Adafruit_MotorShield.h>
32-
#include "utility/Adafruit_PWMServoDriver.h"
3337

3438
/* DualStepper */
3539
#include "DualStepper.h"
@@ -39,38 +43,6 @@
3943

4044
#include <EEPROM.h>
4145

42-
43-
/*
44-
* PINS
45-
*/
46-
47-
#define PEN_AXIS_PORT 1
48-
#define ROTATION_AXIS_PORT 2
49-
50-
#define SERVO_PIN 9
51-
52-
/*
53-
* Other Configuration
54-
*/
55-
56-
/* Pen servo gets clamped to these values. */
57-
#define MIN_PEN_POSITION 100
58-
#define MAX_PEN_POSITION 130
59-
60-
/* Default pen up position. */
61-
#define PEN_UP_POSITION 107
62-
63-
/* How long to take for pen down moves in ms. */
64-
#define PEN_DOWN_MOVE_TIME 200
65-
66-
67-
/* X axis gets clamped to these values to prevent inadvertent damage. */
68-
#define MIN_PEN_AXIS_STEP -480
69-
#define MAX_PEN_AXIS_STEP 480
70-
71-
/* Suitable for Eggbot template and 200 steps/rev steppers at 16x microstepping. */
72-
#define DEFAULT_ZOOM_FACTOR 1.0
73-
7446
enum {
7547
VALUES_SAVED_EEPROM_LOCATION, MIN_PEN_EEPROM_LOCATION, MAX_PEN_EEPROM_LOCATION, PEN_UP_EEPROM_LOCATION
7648
};
@@ -85,10 +57,15 @@ byte current_pen_position;
8557
/* --------- */
8658

8759
/* Set up steppers */
60+
#if ADAFRUIT_MOTOR_SHIELD_VERSION == 1
61+
SingleStepper *xStepper = new SingleStepper(new AF_Stepper(STEPS_PER_REVOLUTION, ROTATION_AXIS_PORT));
62+
SingleStepper *yStepper = new SingleStepper(new AF_Stepper(STEPS_PER_REVOLUTION, PEN_AXIS_PORT));
63+
#else
8864
Adafruit_MotorShield MS = Adafruit_MotorShield();
89-
SingleStepper *xStepper = new SingleStepper(MS.getStepper(200, ROTATION_AXIS_PORT));
90-
SingleStepper *yStepper = new SingleStepper(MS.getStepper(200, PEN_AXIS_PORT));
91-
DualStepper *steppers = new DualStepper(xStepper, yStepper, 200 * MICROSTEPS);
65+
SingleStepper *xStepper = new SingleStepper(MS.getStepper(STEPS_PER_REVOLUTION, ROTATION_AXIS_PORT));
66+
SingleStepper *yStepper = new SingleStepper(MS.getStepper(STEPS_PER_REVOLUTION, PEN_AXIS_PORT));
67+
#endif
68+
DualStepper *steppers = new DualStepper(xStepper, yStepper, STEPS_PER_REVOLUTION * MICROSTEPS);
9269

9370
Servo servo;
9471

@@ -106,9 +83,6 @@ boolean absoluteMode = true;
10683
double feedrate = 160.0; // steps/s
10784
double zoom = DEFAULT_ZOOM_FACTOR;
10885

109-
// steps/s. A no-delay loop takes 1.29 ms per step, so this is the fastest we can go.
110-
#define MAX_FEEDRATE 775.0
111-
11286
// ------
11387

11488
void load_pen_configuration()
@@ -164,9 +138,11 @@ void setup()
164138
Serial.begin(115200);
165139
clear_buffer();
166140

141+
#if ADAFRUIT_MOTOR_SHIELD_VERSION == 2
167142
MS.begin();
168143
TWBR = ((F_CPU / 400000L) - 16) / 2; // Change the i2c clock to 400KHz for faster stepping.
169-
Serial.println("Ready");
144+
#endif
145+
Serial.println("EggBot Ready");
170146

171147
steppers->setMaxSpeed(MAX_FEEDRATE);
172148

0 commit comments

Comments
 (0)