I am using Arduino along with a driver CL860 along with 8.5N stepper motor. This motor is being utilised to drive an oscillating arm at desired speed and at desired angle (maximum 160 degree and minimum to 70 degree). Angle and Arm speed is being controlled by two different potentiometers of 10K.
The driver has input of pulse and direction which is given by Arduino. Motor should run from centre
a. Gradually accelerate to input speed then run at desired speed then deaccelerate to Zero speed,
b. Change direction, gradually accelerate to desired speed run at desired speed then deaccelerate to Zero speed
c. Change direction, gradually accelerate to desired speed run at desired speed
d. And continue to do this till exit command is given.
Problem Statement: Motor gives jerk while running at user input speed not while acceleration / deaccelerating.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
int x , i , ang , spe , AM , TS , AC , j1 ;
int dir = 6;
int pul = 7;
int a[7] ;
int A[7] ;
int n = 7 ;
int sp1 ,val ;
const byte address[6] = "00001";
void setup()
{
// Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening();
pinMode(dir , OUTPUT);
pinMode(pul , OUTPUT);
for (int i = 0; i < 7; i++)
{
a[i] = 0;
}
}
void loop()
{
Data_recieve(a, n);
assign_values();
knob( &ang , &spe );
arm_control( &AC );
temp_stop( &TS );
arm_move();
}
void Data_recieve(int x[], int n)
{
int c = 0;
while (c < n)
{
if (radio.available())
{
Serial.println("data recieved 1");
radio.read( A, sizeof(A));
for (int i = 0; i < 7; i++)
{
x[i] = A[i];
Serial.print(" value at ");
Serial.print(i);
Serial.print(" : ");
Serial.println(A[i]);
}
break;
}
break;
}
}
void assign_values()
{
for ( int i = 0 ; i < 1 ; i++ )
{
j1 = A[i];
ang = A[i + 2];
spe = A[i + 3];
AM = A[i + 4];
AC = A[i + 5];
TS = A[i + 6];
}
}
void knob(int* pot1, int* pot2)
{
Data_recieve(a, n);
for ( int i = 0 ; i < 1 ; i++ )
{
*pot1 = A[i + 2] ;
*pot2 = A[i + 3] ;
}
}
void arm_control( int* arm)
{
Data_recieve(a, n);
for ( int i = 0 ; i < 1 ; i++ )
{
*arm = A[i + 5];
}
}
void temp_stop( int* stp)
{
Data_recieve(a, n);
for ( int i = 0 ; i < 1 ; i++ )
{
*stp = A[i + 6];
}
}
void angle_speed( int* j , int* sp )
{
knob( &ang , &spe );
int k = map( ang , 0 , 1023 , 70 , 160 );
*j = k * 44 ;
*sp = map( spe , 0 , 1023 , 1000 , 250 );
}
void arm_move()
{
if( TS == 1 )
{
if( AC == 1 )
{
int sp2 = 1000,j=0;
angle_speed( &val , &sp1 );
// delay(1000);
digitalWrite( dir , HIGH );
for( i = 1 ; i <= val/8 ; i++ )
{
if(j>=val/16)
{
j = sp1/2;
}
sp2 = map(j,0,val/16,1000,sp1/2);
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
j = j+1;
}
for( i = val/8 ; i <= val/4 ; i++ )
{
sp2 = sp1;
if(i>=3*val/16)
{
sp2 = map(i,3*val/16,val/4,sp1/2,1000);
}
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
}
temp_stop( &TS );
arm_control( &AC );
// delay(1000);
j=0;sp2=1000;
digitalWrite( dir , LOW );
for( i = 1 ; i <= val/4 ; i++ )
{
if(j>=val/8)
{
j = sp1/2;
}
sp2 = map(j,0,val/8,1000,sp1/2);
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
j = j+1;
temp_stop( &TS );
arm_control( &AC );
}
for( i = val/4 ; i <= val/2 ; i++ )
{
sp2 = sp1;
if(i>=3*val/4)
{
sp2 = map(i,3*val/4,val/2,sp1/2,1000);
}
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
}
temp_stop( &TS );
arm_control( &AC );
// delay(1000);
j=0;sp2=1000;
digitalWrite( dir , HIGH );
for( i = 1 ; i <= val/8 ; i++ )
{
if(j>=val/16)
{
j = sp1/2;
}
sp2 = map(j,0,val/16,1000,sp1/2);
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
j = j+1;
}
for( i = val/8 ; i <= val/4 ; i++ )
{
sp2 = sp1;
if(i>=3*val/16)
{
sp2 = map(i,3*val/16,val/4,sp1/2,1000);
}
digitalWrite(pul , HIGH);
delayMicroseconds(sp2);
digitalWrite(pul , LOW);
delayMicroseconds(sp2);
temp_stop( &TS );
arm_control( &AC );
}
temp_stop( &TS );
arm_control( &AC );
if(AC == 0)
{ exit; }
if(TS == 0)
{ exit; }
}
temp_stop( &TS );
arm_control( &AC );
}
}