Running Joy node with Accelstepper.h in Arduino
Hi all,
I am trying to take input from the Joy node and use it to run a motor using Accelstepper.h in Arduino, yet with my code I am not able to get the stepper motor to run. It is able to run when I code it directly to move but not with the Joy node code. The Joy node is returning a value, it just is not setting the speed and running the motor. I am using ROS Melodic.
Here is my code:
#include <stdint.h>
#include <stdlib.h>
#include <ros.h>
#include <std_msgs/Float32.h>
#include <sensor_msgs/Joy.h>
#include <AccelStepper.h>
ros::NodeHandle nh; //Node Handler
// Lower Platform Stepper Pins
int lowerPlatformEnable = 37; // Active HIGH
int lowerPlatformPUL = 36;
int lowerPlatformDIR = 35; // (LOW: Forwards | HIGH: Backwards)
float lowerPlatformMaxSpeed = 10000; // Steps per second
float lowerPlatformAccel = 2250; // Steps per second per second
AccelStepper lowerStepperMotor(AccelStepper::DRIVER, lowerPlatformPUL, lowerPlatformDIR);
//Joystick callback
char output[6];
char motorSpeed[10];
void joydata (const sensor_msgs::Joy& joy){
dtostrf(joy.axes[1], 4, 3, output);
nh.loginfo(output);
lowerStepperMotor.setSpeed(joy.axes[1]*100);
lowerStepperMotor.runSpeed();
}
//Joystick subscriber
ros::Subscriber<sensor_msgs::Joy> sub1("joy", joydata);
void setup(){
// Lower Platform Stepper
pinMode(lowerPlatformEnable, OUTPUT);
pinMode(lowerPlatformPUL, OUTPUT);
pinMode(lowerPlatformDIR, OUTPUT);
lowerStepperMotor.setMaxSpeed(lowerPlatformMaxSpeed); // In steps per second
lowerStepperMotor.setAcceleration(lowerPlatformAccel);
nh.initNode();
nh.subscribe(sub1);
}
void loop() {
nh.spinOnce();
delay(10);
}
Thank you all!
EDIT:
I added
if(lowerStepperMotor.isRunning()){
nh.loginfo("Running");
}
below the runSpeed() call and it does say it is running when I move the joystick, yet the motors still don't move.