Skip to content

Instantly share code, notes, and snippets.

@jadonk jadonk/IMG_1160.JPG

Last active Mar 27, 2019
Embed
What would you like to do?
Stepper motor on BeagleBone Blue

Parts

Tools

  • Soldering iron
  • Exacto knife

Modified unipolar stepper to bipolar

I followed a blog article on how to modify this unipolar stepper to be bipolar, because BeagleBone Blue has built-in H-bridges

Cut common

Wiring

I used an EduMIP, which has DC motors on M2 and M3, so I grabbed M1 and M4.

I used a wiring housing to line up the wires to the stepper motor header.

  • M1+: Pink (2)
  • M1-: Orange (4)
  • M4+: Yellow (3)
  • M4-: Blue (1)
  • center-tap (unused): Red (5)

Stepper wiring EduMIP wiring

Software

git clone https://gist.github.com/jadonk/1da9f20423c2e6c8a954eca1a3e8f7f2
cd 1da9f20423c2e6c8a954eca1a3e8f7f2
make
./stepper_example
# This is a general use makefile for librobotcontrol projects written in C.
# Just change the target name to match your main source code filename.
TARGET = stepper_example
# compiler and linker binaries
CC := gcc
LINKER := gcc
# compiler and linker flags
WFLAGS := -Wall -Wextra -Werror=float-equal -Wuninitialized -Wunused-variable -Wdouble-promotion
CFLAGS := -g -c -Wall
LDFLAGS := -pthread -lm -lrt -l:librobotcontrol.so.1
SOURCES := $(wildcard *.c)
INCLUDES := $(wildcard *.h)
OBJECTS := $(SOURCES:$%.c=$%.o)
prefix := /usr/local
RM := rm -f
INSTALL := install -m 4755
INSTALLDIR := install -d -m 755
SYMLINK := ln -s -f
SYMLINKDIR := /etc/robotcontrol
SYMLINKNAME := link_to_startup_program
# linking Objects
$(TARGET): $(OBJECTS)
@$(LINKER) -o $@ $(OBJECTS) $(LDFLAGS)
@echo "Made: $@"
# compiling command
$(OBJECTS): %.o : %.c $(INCLUDES)
@$(CC) $(CFLAGS) $(WFLAGS) $(DEBUGFLAG) $< -o $@
@echo "Compiled: $@"
all: $(TARGET)
debug:
$(MAKE) $(MAKEFILE) DEBUGFLAG="-g -D DEBUG"
@echo " "
@echo "$(TARGET) Make Debug Complete"
@echo " "
install:
@$(MAKE) --no-print-directory
@$(INSTALLDIR) $(DESTDIR)$(prefix)/bin
@$(INSTALL) $(TARGET) $(DESTDIR)$(prefix)/bin
@echo "$(TARGET) Install Complete"
clean:
@$(RM) $(OBJECTS)
@$(RM) $(TARGET)
@echo "$(TARGET) Clean Complete"
uninstall:
@$(RM) $(DESTDIR)$(prefix)/bin/$(TARGET)
@echo "$(TARGET) Uninstall Complete"
runonboot:
@$(MAKE) install --no-print-directory
@$(SYMLINK) $(DESTDIR)$(prefix)/bin/$(TARGET) $(SYMLINKDIR)/$(SYMLINKNAME)
@echo "$(TARGET) Set to Run on Boot"
/**
* @file stepper_example.c
*/
#include <stdio.h>
#include <robotcontrol.h> // includes ALL Robot Control subsystems
void step(int direction);
/**
* This template contains these critical components
* - ensure no existing instances are running and make new PID file
* - start the signal handler
* - initialize subsystems you wish to use
* - while loop that checks for EXITING condition
* - cleanup subsystems at the end
*
* @return 0 during normal operation, -1 on error
*/
int main()
{
int position = 0;
int direction = 1;
// make sure another instance isn't running
// if return value is -3 then a background process is running with
// higher privaledges and we couldn't kill it, in which case we should
// not continue or there may be hardware conflicts. If it returned -4
// then there was an invalid argument that needs to be fixed.
if(rc_kill_existing_process(2.0)<-2) return -1;
// start signal handler so we can exit cleanly
if(rc_enable_signal_handler()==-1){
fprintf(stderr,"ERROR: failed to start signal handler\n");
return -1;
}
// make PID file to indicate your project is running
// due to the check made on the call to rc_kill_existing_process() above
// we can be fairly confident there is no PID file already and we can
// make our own safely.
rc_make_pid_file();
// Keep looping until state changes to EXITING
rc_set_state(RUNNING);
rc_led_set(RC_LED_GREEN, 1);
rc_led_set(RC_LED_RED, 0);
while(rc_get_state()!=EXITING){
// do stepping here
if(position >= 800){
direction = -1;
} else if(position <= 0) {
direction = 1;
}
step(direction);
position += direction;
// always sleep at some point
rc_usleep(10000);
}
// turn off LEDs and close file descriptors
rc_led_set(RC_LED_GREEN, 0);
rc_led_set(RC_LED_RED, 0);
rc_remove_pid_file(); // remove pid file LAST
return 0;
}
#define M1 1
#define M2 4
void step(int direction)
{
static int state = 0;
static int enabled = 0;
if(!enabled) {
rc_motor_init();
enabled = 1;
}
if(direction > 0) {
state++;
} else if (direction < 0) {
state--;
}
if(state > 4) state = 0;
else if(state < 0) state = 3;
switch(state) {
case 0:
default:
rc_motor_set(M1,1);
rc_motor_set(M2,1);
break;
case 1:
rc_motor_set(M1,-1);
rc_motor_set(M2,1);
break;
case 2:
rc_motor_set(M1,-1);
rc_motor_set(M2,-1);
break;
case 3:
rc_motor_set(M1,1);
rc_motor_set(M2,-1);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.