cant assign the value to 'cmd_msg.data' in runtime.
I am doing a stepper motor programming.what i want is "when i enter an angle on command line,then stepper should rotate that angle". but i cant assign the value in runtime. help me please.
#include <ros.h>
#include <std_msgs/Empty.h>
#include <std_msgs/UInt16.h>
signed int angle;
int enable2 = 13;
const int stepPin = 9;
const int dirPin = 8;
int x;
ros::NodeHandle nh;
void pwm( const std_msgs::UInt16& cmd_msg)
{
analogWrite(enable2, cmd_msg.data);
angle=analogRead(enable2);
}
ros::Subscriber<std_msgs::UInt16> sub("servo", pwm);
void setup(){
// pinMode(angle, OUTPUT);
pinMode(enable2, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
nh.initNode();
nh.subscribe(sub);
// servo.attach(9); //attach it to pin 9
}
void loop()
{
if (angle<0)
{
cw();
delay(100);
}
else
{
ccw();
delay(100);
}
delay(10000);
nh.spinOnce();
delay(1);
}
void cw()
{
digitalWrite(dirPin,LOW);
for(x = 0; x < 17.77*(angle*-1); x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}}
void ccw()
{
digitalWrite(dirPin,HIGH);
for(x = 0; x < 17.77*angle; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}}