Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions WaveGen/Arduino/basicStepperDriver/basicStepperDriver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <Arduino.h>
#include "BasicStepperDriver.h"

// motor : https://www.omc-stepperonline.com/de/p-series-ip67-wasserdicht-nema-23-schrittmotor-5-0a-1-8nm-254-95oz-in-23ip67-20
// stepper controller: https://www.omc-stepperonline.com/de/digitaler-schrittmotortreiber-1-0-4-2a-20-50vdc-fuer-nema-17-23-24-schrittmotor-dm542t

// the motor has a 1.8 deg step so 200 steps
#define MOTOR_STEPS 200

// set a safty RPM
#define SAFTY_RPM 600

// step resolution on stepper controller set to 800 step/rev
#define MICROSTEPS 4

// All the wires needed for full functionality
#define DIR 4
#define STEP 3
#define ENABLE 2

//Uncomment line to use enable/disable functionality
//#define SLEEP 13

// set all value to default
float rpmVal = 60.0;
float timeVal = 0.0;
int angleVal = 0;
int start_delay = 0;

unsigned long start_time = 0;

// start bsic driver
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, ENABLE);

void setup() {

// enable serial
Serial.begin(9600);
while (!Serial) {;} // Wait for serial port to connect
Serial.println("Found serial connection of wavegen");

stepper.begin(rpmVal, MICROSTEPS);
stepper.setEnableActiveState(LOW);

stepper.disable();

}

void loop(){

// parse the message from python
if (Serial.available() > 0) {

// read in line
String input = Serial.readStringUntil('\n');
input.trim();

// Split command and value
int spaceIndex = input.indexOf(' ');
String command = input;
int value = 0;

// Split string into tokens
const int maxTokens = 10;
String tokens[maxTokens];
int tokenCount = 0;

// sort the messages
while (input.length() > 0 && tokenCount < maxTokens) {
int spaceIndex = input.indexOf(' ');
if (spaceIndex == -1) {

tokens[tokenCount++] = input;
break;

} else {

tokens[tokenCount++] = input.substring(0, spaceIndex);
input = input.substring(spaceIndex + 1);
input.trim();

}
}

// Parse name/value pairs
for (int i = 0; i < tokenCount - 1; i += 2) {
String name = tokens[i];
float value = tokens[i + 1].toFloat();

if (name == "a") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kann das arduino-SDK strings mit == vergleichen? In C geht das nicht, da braucht man typischerweise strcmp(name, "a") == 0

angleVal = tokens[i + 1].toInt();
Serial.print("Set angle to ");
Serial.println(angleVal);

} else if (name == "t") {

// time in ms
timeVal = value;
Serial.print("Set time to ");
Serial.println(timeVal);

} else if (name == "rpm") {

if(value > SAFTY_RPM){
rpmVal = SAFTY_RPM;
}
else if(value != 0.0){
rpmVal = value;
}

// set the new rpm value
stepper.begin(rpmVal, MICROSTEPS);

Serial.print("Set rpm to ");
Serial.println(rpmVal);

} else if(name == "delay"){

start_delay = value;
Serial.print("Delay before start: ");
Serial.println(start_delay);

} else {

Serial.print("Unknown parameter: ");
Serial.println(name);
}
}

delay(start_delay * 1000); // in sec
Serial.println("Starting");

}


if(angleVal != 0){
Serial.print("start rotation with angle and rpm: ");
Serial.print(angleVal);
Serial.print(" ");
Serial.println(rpmVal);

stepper.enable();
// if no rpm given the rotation speed is set to default
stepper.rotate(angleVal);

stepper.disable();
angleVal = 0;
rpmVal = 60.0;

Serial.println("Finished");

}

// only set in rpm if the angle is 0 -> else do angular rotation
if(rpmVal != 60.0 && angleVal == 0){

Serial.print("rotating at rpm ");
Serial.println(rpmVal);

// set the new rpm value
stepper.setRPM(rpmVal);

if(start_time = 0){
start_time = millis();
}

stepper.rotate(360);
}

if(timeVal != 0.0){
//Serial.println(millis() - start_time);
if(millis() - start_time > timeVal){
// set RPM and set time to 0
timeVal = 0.0;
rpmVal = 60.0;
stepper.disable();

Serial.println("Stopped motor due to time");
Serial.println("Finished");
}
}

}
22 changes: 22 additions & 0 deletions WaveGen/Arduino/libraries/StepperDriver/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Laurentiu Badea

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

57 changes: 57 additions & 0 deletions WaveGen/Arduino/libraries/StepperDriver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Default build architecture and board
TARGET ?= arduino:avr:uno
CORE = $(shell echo $(TARGET) | cut -d: -f1,2)

# Where to save the Arduino support files, this should match what is in arduino-cli.yaml
ARDUINO_DIR ?= .arduino

default:
#################################################################################################
# Initial setup: make .arduino/arduino-cli setup
#
# Build all the examples: make all TARGET=adafruit:samd:adafruit_feather_m0
#
# Install more cores: make core TARGET=adafruit:samd:adafruit_feather_m0
# (edit arduino-cli.yaml and add repository if needed)
#################################################################################################

# See https://arduino.github.io/arduino-cli/installation/
ARDUINO_CLI_URL = https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Linux_64bit.tar.gz
ARDUINO_CLI ?= $(ARDUINO_DIR)/arduino-cli --config-file arduino-cli.yaml
EXAMPLES := $(shell ls examples)

COMPILE = $(ARDUINO_CLI) compile --warnings all --fqbn $(TARGET)

all: # Build all example sketches
all: $(EXAMPLES:%=%.hex)
ls -l build

%.hex: # Generic rule for compiling sketch to uploadable hex file
%.hex: examples/% core
$(ARDUINO_CLI) compile --warnings all --fqbn $(TARGET) --output-dir build $<

# Remove built objects
clean:
rm -rfv build

core: $(ARDUINO_DIR)/arduino-cli
$(ARDUINO_CLI) core install $(CORE)

$(ARDUINO_DIR)/arduino-cli: # Download and install arduino-cli
$(ARDUINO_DIR)/arduino-cli:
mkdir -p $(ARDUINO_DIR)
cd $(ARDUINO_DIR)
curl -L -s $(ARDUINO_CLI_URL) \
| tar xfz - -C $(ARDUINO_DIR) arduino-cli
chmod 755 $@
$(ARDUINO_CLI) version

setup: # Configure cores and libraries for arduino-cli (which it will download if missing)
setup: $(ARDUINO_DIR)/arduino-cli
mkdir -p $(ARDUINO_DIR)/libraries
ln -sf $(CURDIR) $(ARDUINO_DIR)/libraries/
$(ARDUINO_CLI) config dump
$(ARDUINO_CLI) core update-index
$(ARDUINO_CLI) core list

.PHONY: clean %.hex all setup
124 changes: 124 additions & 0 deletions WaveGen/Arduino/libraries/StepperDriver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
[![arduino-library-badge](https://www.ardu-badge.com/badge/StepperDriver.svg?)](https://www.ardu-badge.com/StepperDriver)
[![Actions Status](https://github.com/laurb9/StepperDriver/workflows/PlatformIO/badge.svg)](https://github.com/laurb9/StepperDriver/actions)
[![Actions Status](https://github.com/laurb9/StepperDriver/workflows/Arduino/badge.svg)](https://github.com/laurb9/StepperDriver/actions)

StepperDriver
=============

A4988, DRV8825, DRV8834, DRV8880 and generic two-pin stepper motor driver library.
Features:
- Constant speed mode (low rpms)
- Linear (accelerated) speed mode, with separate acceleration and deceleration settings.
- Non-blocking mode (yields back to caller after each pulse)
- Early brake / increase runtime in non-blocking mode

Hardware currently supported:
- <a href="https://www.pololu.com/product/2134">DRV8834</a> Low-Voltage Stepper Motor Driver
up to 1:32
- <a href="https://www.pololu.com/product/1182">A4988</a> Stepper Motor Driver up to 1:16
- <a href="https://www.pololu.com/product/2131">DRV8825</a> up to 1:32
- <a href="https://www.pololu.com/product/2971">DRV8880</a> up to 1:16, with current/torque control
- any other 2-pin stepper via DIR and STEP pins, microstepping up to 1:128 externally set

Microstepping
=============

The library can set microstepping and generate the signals for each of the support driver boards.

High RPM plus high microstep combinations may not work correctly on slower MCUs, there is a maximum speed
achieveable for each board, especially with acceleration on multiple motors at the same time.

Motors
======

- 4-wire bipolar stepper motor or
- some 6-wire unipolar in 4-wire configuration (leaving centers out) or
- 28BYJ-48 (commonly available) with a small modification (search for "convert 28byj-48 to 4-wire").

Connections
===========

Minimal configuration from <a href="https://www.pololu.com/product/2134">Pololu DRV8834 page</a>:

<img src="https://a.pololu-files.com/picture/0J4344.600.png">

Wiring
======

This is suggested wiring for running the examples unmodified. All the pins below can be changed.

- Arduino to driver board:
- DIR - D8
- STEP - D9
- GND - Arduino GND
- GND - Motor power GND
- VMOT - Motor power (check driver-specific voltage range)
- A4988/DRV8825 microstep control
- MS1/MODE0 - D10
- MS2/MODE1 - D11
- MS3/MODE2 - D12
- DRV8834/DRV8880 microstep control
- M0 - D10
- M1 - D11
- ~SLEEP (optional) D13

- driver board to motor (this varies from motor to motor, check motor coils schematic).
- 100uF capacitor between GND - VMOT
- Make sure to set the max current on the driver board to the motor limit (see below).
- Have a motor power supply that can deliver that current.
- Make sure the motor power supply voltage is within the range supported by the driver board.

Set Max Current
===============

The max current is set via the potentiometer on board.
Turn it while measuring voltage at the passthrough next to it.
The formula is V = I*5*R where I=max current, R=current sense resistor installed onboard

- DRV8834 or DRV8825 Pololu boards, R=0.1 and V = 0.5 * max current(A).
For example, for 1A you will set it to 0.5V.

For latest info, see the Pololu board information pages.

Code
====

See the BasicStepperDriver example for a generic driver that should work with any board
supporting the DIR/STEP indexing mode.

The Microstepping example works with a DRV8834 board.

For example, to show what is possible, here is the ClockStepper example that moves a
stepper motor like the seconds hand of a watch:

```C++
#include <Arduino.h>
#include "A4988.h"

// using a 200-step motor (most common)
#define MOTOR_STEPS 200
// configure the pins connected
#define DIR 8
#define STEP 9
#define MS1 10
#define MS2 11
#define MS3 12
A4988 stepper(MOTOR_STEPS, DIR, STEP, MS1, MS2, MS3);

void setup() {
// Set target motor RPM to 1RPM and microstepping to 1 (full step mode)
stepper.begin(1, 1);
}

void loop() {
// Tell motor to rotate 360 degrees. That's it.
stepper.rotate(360);
}
```

Hardware
========
- Arduino-compatible board
- A <a href="https://www.pololu.com/category/120/stepper-motor-drivers">stepper motor driver</a>, for example DRV8834, DRV8825, DRV8824, A4988.
- A <a href="http://www.circuitspecialists.com/stepper-motor">Stepper Motor</a>.
- 1 x 100uF capacitor
1 change: 1 addition & 0 deletions WaveGen/Arduino/libraries/StepperDriver/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-modernist
Loading