Hey All,
I have the following code. It is currently imcomplete and the step I am working on right now involves using an analog signal from another device to move a linear actuator. The analog value is called travel_val and the motor moves using the move_pos function. When I plug it in nothing moves. I think this is due to the rapidly changing analog value. How do I narrow that value down? It could also be because I am trying to read two analog values.
links to individual parts
linear actuator
dynamixel shield
power hub board
https://www.reichelt.com/be/en/dynamixel-u2d2-power-hub-u2s2-phb-p263262.html?&nbc=1
#include "CytronMotorDriver.h"
#include <DynamixelShield.h>
const float DXL_PROTOCOL_VERSION = 2.0;
const uint8_t Pan_Motor = 1;
const uint8_t Left_Tilt = 2;
const uint8_t Right_Tilt = 3;
CytronMD motor(PWM_DIR, 3, 4); // PWM = Pin 3, DIR = Pin 4.
DynamixelShield dxl;
//relay variables
int relay=8;
volatile byte relayState = LOW;
// connect to ur10e
int UR10pin = A2;
int travel_val = analogRead(UR10pin);
// potentiometer variables
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 3
int pos_val = 0; // variable to store the value read
int sum = 0;
int dead_zone = 2;
//int max_extend = 670; //not used
//int max_retract = 358; // not used
//int travel_val =511; // analog val read from ur10e control box
int current_speed(int curr_dist, int max_speed){
int curr_speed = 8*abs(curr_dist);
if (curr_speed > max_speed) {
curr_speed = max_speed;
}
return curr_speed;
}
void move_pos(int travel_val){
sum = 0; //for loop to average results of analog reading due to deviation in resistance
int act_speed =0;
/*
if (abs(travel_val-pos_val) <= 17){
act_speed=112;
}
else{
act_speed=225;
}
*/
for (int i=0; i<15; i = i+1){
sum += analogRead(analogPin);
}
pos_val=sum/15;
act_speed = current_speed(abs(511-pos_val), 225);
if (pos_val <= travel_val && pos_val >= travel_val)
{motor.setSpeed(0);}
else if (travel_val<pos_val - dead_zone)
{motor.setSpeed(act_speed);}
else if (travel_val>pos_val + dead_zone)
{motor.setSpeed(-act_speed);}
}
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
pinMode(relay, OUTPUT);
dxl.begin(1000000);//begin communication to motors,Trossen set baud rate to 1000000
dxl.ping(Pan_Motor);// get motor info
dxl.ping(Left_Tilt);
dxl.ping(Right_Tilt);
dxl.torqueOff(Pan_Motor);
dxl.setOperatingMode(Pan_Motor, OP_POSITION);
dxl.torqueOn(Pan_Motor);
dxl.torqueOff(Left_Tilt);
dxl.setOperatingMode(Left_Tilt, OP_POSITION);
dxl.torqueOn(Left_Tilt);
dxl.torqueOff(Right_Tilt);
dxl.setOperatingMode(Right_Tilt, OP_POSITION);
dxl.torqueOn(Right_Tilt);
}
void loop() {
move_pos(travel_val);
}