Hi!
Im having problem controling my stepper motor via CAN, im not sure im using "currentPosition" right tho...
When im trying the motor is just wibrating...
Im using PCAN.
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Stepper.h>
#include <CustomStepper.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <mcp_can.h>
#include <mcp_can_dfs.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2,4,6,7);
AccelStepper stepper;
int stepCount = 0; // number of steps the motor has taken
int currentPosition = 0;
//long currentPosition (); //current position in the start
void goToPosition (int steps){
//long currentPosition;
Serial.print("JAG ÄR INNE!");
if (steps <= 0 && stepper.currentPosition() <= 0){ // 0% to 50%
Serial.print("STEPS < CURRENT");
Serial.println(steps);
myStepper.step(steps-stepper.currentPosition());
currentPosition=stepper.currentPosition();
Serial.print("Current = " );
Serial.print(currentPosition);
Serial.print("\t Goal = ");
// if (steps < currentPosition){
//
// myStepper.step(steps - currentPosition));
// }
// if (steps > currentPosition){
//
// myStepper.step(steps - currentPosition);
// }
if (stepper.currentPosition() == 0){
myStepper.step(steps);
currentPosition=stepper.currentPosition();
}
//currentPosition == steps;
}
if (steps >= 0 && currentPosition >= 0){ // 0% to 100%
Serial.print("STEPS>CURRENT");
if(steps > currentPosition){
myStepper.step(steps - currentPosition);
}
if(steps < currentPosition){
myStepper.step(currentPosition-steps);
}
//currentPosition == steps;
}
}
void setup (){
myStepper.setSpeed(60); // 60 rpm
Serial.begin(9600);
if(Canbus.init(CANSPEED_250)){
Serial.println("CAN Init ok");
}
else{
Serial.println("Can't init CAN");
}
Serial.println("");
delay(1000);
Serial.println("port manipulation commenced");
DDRD = DDRD | B11111000;
Serial.println("33%");
DDRB = DDRB | B00000001;
Serial.println("66%");
DDRC = DDRC | B00111111;
Serial.println("100%");
Serial.println("port manipulation complete");
Serial.println("");
}
void loop() {
int steps;
tCAN message;
if (mcp2515_check_message()) {
Serial.print("Check msg ");
if (mcp2515_get_message(&message)){
Serial.print("Get msg ");
if(message.id == 0x300){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 0%");
//myStepper.step(-59);
steps = -59;
goToPosition(steps);
Serial.print("DONE MOVE!");
}
if (b1 == 0){
Serial.print("DONT MOVE!!!");
myStepper.step(0);
}
}
if (message.id == 0x325){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position in between 0% and click");
steps == -23;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
if (message.id ==0x337){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To left click");
steps == -13;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
if (message.id ==0x350){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 50%");
steps == 0;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
if(message.id == 0x363){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position 100%");
steps == 59;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
if (message.id == 0x381){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To position in between 100% and click");
steps == 23;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
if (message.id ==0x400){
int b1 = message.data[0];
int b2 = message.data[1];
int b3 = message.data[2];
if(b1 == 1){
Serial.print("To right click");
steps == 13;
goToPosition(steps);
}
if (b1 == 0){
myStepper.step(0);
}
}
}
}
}