MKR CAN shield for dummies

I have acquired a Schnyder Electric ILA1 stepper motor with an integrated Lexium drive. This motor uses a CANopen communication protocol (not sure what commands yet). I am waiting to hear back from Schnidyer Electric on the specific commands. I bought a MKR zero and an MKR CAN shield to communicate with it. So far I have found no "nube friendly" help on how to build a sketch to run this thing. My end goal is to use it to open a sliding door. I have a working sketch for operation but I am clueless on CANopen protocol and syntax. From what I have read it seems to be a bit of a pain in the A$$ to work with, but that's what I have. Any help, reference material, magic wand, tutorial, or Trebuchet (to launch this at my rivals) would be appreciated.
This is the sketch I am using:

const int TRIGPin = 0;// Push Button to triger the open command
const int OLIMPin = 1;// Open limit switch to stop the open command
const int CLIMPin = 4;// Close limit switch to stop the close command
const int OpenTime = 10000;// Time to stay open before starting the close command

// variables will change:
volatile int TRIGState;  // variable for reading the pushbutton status
volatile int OLIMState;  // variable for reading the pushbutton status
volatile int CLIMState;  // variable for reading the pushbutton status
volatile int StatusCode; // 0 = Closed, 1 = Opening, 2 = Open, 3 = Timing out, 4 = Closing, 5 = Mid-close open

void OpenInterrupt () {
  int TRIGState = digitalRead(TRIGPin);
  int OLIMState = digitalRead(OLIMPin);
  int CLIMState = digitalRead(CLIMPin);
if (StatusCode == 0 && TRIGState == 1 && OLIMState == 0 && CLIMState == 1) { //Open button is pressed and openlimit is off then set status to 1 (Opening)
    StatusCode = 1;
  } 
if (TRIGState == 1 && OLIMState == 0 && CLIMState == 0) {
    StatusCode = 5;
  }
}
void OpenedInterrupt () {
  int OLIMState = digitalRead(OLIMPin);
  if (StatusCode == 1 && OLIMState == 1) { //When the open limit is reached and the status is 1, the set status to 2 (opened)
    StatusCode = 2;
  }
}
void ClosingInterrupt () {
  int TRIGState = digitalRead(TRIGPin);
  int CLIMState = digitalRead(CLIMPin);
  if (StatusCode == 4 && CLIMState == 1 && TRIGState == 0) {//When the door has closed then the status is reset to 0 (Closed)
    StatusCode = 0;
  }
}
void setup() {
  pinMode(TRIGPin, INPUT);
  pinMode(OLIMPin, INPUT);
  pinMode(CLIMPin, INPUT);
  Serial.begin(115200);
  attachInterrupt (TRIGPin, OpenInterrupt, RISING);
  attachInterrupt (OLIMPin, OpenedInterrupt, RISING);
  attachInterrupt (CLIMPin, ClosingInterrupt, RISING);
}

void loop() {
 if (StatusCode == 1) {
  // Door Open Code
  Serial.println("Opening");
 }
if (StatusCode == 2) {
  Serial.println("Holding Open");
  delay(OpenTime);
  StatusCode = 3;
 }
if (StatusCode == 3) {
  // Door close Code
  Serial.println("Closing");
  StatusCode = 4;
 }

if (StatusCode == 5) {
  // Door Stop
  Serial.println("Stoping");
  delay(2000);
  StatusCode = 1;
 }
 TRIGState = digitalRead(TRIGPin);
 OLIMState = digitalRead(OLIMPin);
 CLIMState = digitalRead(CLIMPin);
 Serial.print("StatusCode, ");
 Serial.println(StatusCode);
 Serial.print("Open Button, ");
 Serial.println(TRIGState);
 Serial.print("Open Limit, ");
 Serial.println(OLIMState);
 Serial.print("Close Limit, ");
 Serial.println(CLIMState);
 delay(1000);
 }const int TRIGPin = 0;// Push Button to triger the open command
const int OLIMPin = 1;// Open limit switch to stop the open command
const int CLIMPin = 4;// Close limit switch to stop the close command
const int OpenTime = 10000;// Time to stay open before starting the close command

// variables will change:
volatile int TRIGState;  // variable for reading the pushbutton status
volatile int OLIMState;  // variable for reading the pushbutton status
volatile int CLIMState;  // variable for reading the pushbutton status
volatile int StatusCode; // 0 = Closed, 1 = Opening, 2 = Open, 3 = Timing out, 4 = Closing, 5 = Mid-close open

void OpenInterrupt () {
  int TRIGState = digitalRead(TRIGPin);
  int OLIMState = digitalRead(OLIMPin);
  int CLIMState = digitalRead(CLIMPin);
if (StatusCode == 0 && TRIGState == 1 && OLIMState == 0 && CLIMState == 1) { //Open button is pressed and openlimit is off then set status to 1 (Opening)
    StatusCode = 1;
  } 
if (TRIGState == 1 && OLIMState == 0 && CLIMState == 0) {
    StatusCode = 5;
  }
}
void OpenedInterrupt () {
  int OLIMState = digitalRead(OLIMPin);
  if (StatusCode == 1 && OLIMState == 1) { //When the open limit is reached and the status is 1, the set status to 2 (opened)
    StatusCode = 2;
  }
}
void ClosingInterrupt () {
  int TRIGState = digitalRead(TRIGPin);
  int CLIMState = digitalRead(CLIMPin);
  if (StatusCode == 4 && CLIMState == 1 && TRIGState == 0) {//When the door has closed then the status is reset to 0 (Closed)
    StatusCode = 0;
  }
}
void setup() {
  pinMode(TRIGPin, INPUT);
  pinMode(OLIMPin, INPUT);
  pinMode(CLIMPin, INPUT);
  Serial.begin(115200);
  attachInterrupt (TRIGPin, OpenInterrupt, RISING);
  attachInterrupt (OLIMPin, OpenedInterrupt, RISING);
  attachInterrupt (CLIMPin, ClosingInterrupt, RISING);
}

void loop() {
 if (StatusCode == 1) {
  // Door Open Code
  Serial.println("Opening");
 }
if (StatusCode == 2) {
  Serial.println("Holding Open");
  delay(OpenTime);
  StatusCode = 3;
 }
if (StatusCode == 3) {
  // Door close Code
  Serial.println("Closing");
  StatusCode = 4;
 }

if (StatusCode == 5) {
  // Door Stop
  Serial.println("Stoping");
  delay(2000);
  StatusCode = 1;
 }
 //Debugging
 TRIGState = digitalRead(TRIGPin);
 OLIMState = digitalRead(OLIMPin);
 CLIMState = digitalRead(CLIMPin);
 Serial.print("StatusCode, ");
 Serial.println(StatusCode);
 Serial.print("Open Button, ");
 Serial.println(TRIGState);
 Serial.print("Open Limit, ");
 Serial.println(OLIMState);
 Serial.print("Close Limit, ");
 Serial.println(CLIMState);
 delay(1000);
 }

You have pasted your code twice.

Does that sketch open the door?
If so, why do you need CANopen?

The sketch does not open the door because there is no CANopen code to run the motor. The working code reacts to the limit switches and edits the status code. Motor commands will be sent based on the status code. I don't know why it copied the sketch twice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.