Pololu a-star 32u4 and roboclaw with an encoder

analog input using potentiometer or joystick to pololu a-star, advanced packet serial out to RoboClaw with encoder feedback for position control hooked up exactly like on page 48 in the manual. pot wiper or center connection is wired to pin A0. on the a-star
I have been trying to teach myself the arduino language. I can get individual parts working, but I cant seem to get all the parts talking to each other. I was having some hard ware issues I think I have them figured out. still having problems with the sketches.
an example is.

//Position PID example.  Velocity PID is not used. Speeds are not controlled by feedback.
//This example is the most like a standard RC servo

//Warning!!!!
//Arduino Mega and Leonardo chips only support some pins for receiving data back from the RoboClaw
//This is because only some pins of these boards support PCINT interrupts or are UART receivers.
//Mega: 0,10,11,12,13,14,15,17,19,50,51,52,53,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15
//Leonardo: 0,8,9,10,11

//Arduino Due currently does not support software serial. Only hardware uarts can be used, pins 0/1, 14/15, 16/17 or 18/19.

//Includes required to use Roboclaw library
#include "BMSerial.h"
#include "RoboClaw.h"

//Roboclaw Address
#define address 0x80

//Note: PID coeffcients will need to be tuned for this code to work with your motor.

//Velocity PID coefficients
#define Kp 0
#define Ki 0
#define Kd 0
#define Qpps 3600

//Position PID coefficients
#define PosKp 20000
#define PosKi 0
#define PosKd 100000
#define KiMax 0
#define DeadZone 0
#define Min -1500
#define Max 1500

//Definte terminal for display. Use hardware serial pins 0 and 1
BMSerial terminal(0,1);

//Setup communcaitions with roboclaw. Use pins 10 and 11 with 10ms timeout
RoboClaw roboclaw(10,11,10000);

//Display Encoder and Speed for Motor 1
void displayspeed(void)
{
  uint8_t status1,status2;
  bool valid1,valid2;
  int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
  int32_t speed1 = roboclaw.ReadSpeedM1(address, &status2, &valid2);

  if(valid1){
    terminal.print("Encoder1:");
    terminal.print(enc1,DEC);
    terminal.print(" ");
    terminal.print(status1,HEX);
    terminal.print(" ");
  }
  if(valid2){
    terminal.print("Speed1:");
    terminal.print(speed1,DEC);
    terminal.print(" ");
  }

  terminal.println();
}

//This function will delay the program so the loop it
//executes in will run no more than every val milliseconds
int lasttime=0;
void rate(int val)
{
  while(millis()-lasttime<val);
  lasttime = millis();
}

void SetPosition(int pos)
{
  //Send position command. The Roboclaw library is using acknowledgement
  //When a valid command is received the roboclaw sends back a byte to ackowledge it.
  if(!roboclaw.SpeedAccelDeccelPositionM1(address,0,3600,0,pos,1))
    terminal.println("SpeedAccelDeccelPositionM1 Failed!");

  displayspeed();
}

//This is the first function arduino runs on reset/power up
void setup() {
  //Open terminal and roboclaw serial ports
  terminal.begin(57600);
  roboclaw.begin(38400);

  terminal.println("Starting...");

  //Use Encoder and Enable encoders in RC mode
  //roboclaw.SetM1EncoderMode(address,0x81);

  //Set PID Coefficients
  roboclaw.SetM1VelocityPID(address,Kd,Kp,Ki,Qpps);
  roboclaw.SetM1PositionPID(address,PosKd,PosKp,PosKi,KiMax,DeadZone,Min,Max);

  //Save settings in Roboclaw eeprom.  Uncomment to save settings.
  //roboclaw.WriteNVM(address);
}

//Main loop
void loop() {
  for(int pos = -1500;pos<1500;pos+=55){
    rate(10);
    SetPosition(pos);
  }

  for(int pos = 1500;pos>-1500;pos-=55){
    rate(10);
    SetPosition(pos);
  }
}

when I try to adapt this to my needs I get errors like(SpeedAccelDeccelPositionM1 Failed!)
tried using others sketchs but none of them are suted for what I am doing.
please send me in the right direction…

Hello.

What mode is your RoboClaw set to? Could you double check that the RoboClaw’s baud rate matches the one you are using in your code (38400)? Could you expand on what happens when you run your program (maybe take a screen capture of the resulting errors)? Also, can you post a wiring diagram and pictures of your setup that show all of your connections?

-Brandon

here are a couple of pic’s of my equiptment and connections.
ALs SBS ELECTRIC CONTROL PROJECT.pdf (133 KB)ALs SBS ELECTRIC CONTROL PROJECT picture.pdf (730 KB)
hope you can see them.
the mode is (7 packet serial mode - Address 0x80 (128) and options 4 (38400)
an example from the serial monitor screen:
Encoder1:230571 80 Speed1:3090
SpeedAccelDeccelPositionM1 Failed!
Encoder1:230612 80 Speed1:3092
SpeedAccelDeccelPositionM1 Failed!
all it does is read the encoder1 position and speed.
and it keeps on rotating like its just a motor and not a servo like it should.
And it does not appear like it is reading the joystick.
sorry it took me a while to get back to this i was busy doing other things.

Hello.

Thank you for the pictures. It is a little bit difficult to follow some of your wiring since it uses similar colored wires, but I did not notice anything obvious that would cause problems from your diagram (although, it looks like your diagram has two motors while the setup in your picture only has one). You might try contacting the RoboClaw manufacturer, Orion Robotics, directly since you are using the RoboClaw with their example program. Also, you might need to tune the velocity PID coefficients for your motor; it looks like that code is setting them all to 0. You might also try using one of the other sample programs to see if it works, such as the “PacketSerialEncoderRead” example sketch.

By the way, I am not sure what you expect to happen when you move your joystick, but I did not see anything in your code to handle the joystick inputs. You might consider simplifying your system while you troubleshoot by taking the joystick out of your setup for now.

-Brandon

well with the program for windows i can get the RoboClaw to work flawlessly just the computer and the RoboClaw.
and with a program in the a-star 32u4 to read the joystick it also functions flawlessly outputting to the serial monitor.
the problem is i don’t now how to integrate the joystick prog. and the roboclaw prog. in the same sketch.
and i did try to run the roboclaw sketch not worying about the joystick and i still could not get them to communicate correctly.

oh ya i forgot that i was told that i do not need to have any velocity settings can i just take them or comment them out of the sketch do you think might be part of my problem.

I wanted to post here for anyone else following this thread that you called and talked with us about making sure the RoboClaw was in the correct mode for the particular example program you are using. I also noticed that you posted in a different thread about the new RoboClaw software that Orion Robotics now has available, which makes calibrating and setting up the RoboClaw much easier than before. Thank you again for posting about it, and as Amanda said in her response on that thread, we have now made it available on the “Resources” tabs of the appropriate RoboClaw product pages.

-Brandon