Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Reading from COM port on my arduino... on: May 27, 2013, 03:59:59 am
you can read anything you want. What I meant was you can make any variable to be read and v4 will read it via the rs232 node.  I use it all the time to debug the serial values im sending it via v4. 
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Reading from COM port on my arduino... on: May 24, 2013, 03:31:05 am
you can read anything you want from the serial port you, start with printing messages to the serial window with the arduino programming environment.  I use a graphical environment called VVVV to read things from arduino.  Have a look at the rs232 node and the things I posted about getting it to work
.
http://vvvv.org/forum/vvvv-to-arduino-using-rs232-%28not-arduino-to-vvvv%29
3  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 23, 2013, 10:45:38 am
Thanks GoForSmoke that did the trick:)  I posted the entire code should someone else want to repeat or expand on using the hardware and software I have used.

The serial input I got from a program called VVVV and if anyone is interested in the patch I can send it to them.
The motor driver I used was the L298n along with the higher frequency PWM.lib and the belt drive I have its almost completely silent..so yay! (because this is for a type of electronic instrument).

Right now I deal with a fixed set of values between 0 - 5000 so that I can easily map the difference between the serial and the global position to use as an intensity factor for the PWM.  Im looking to change this into a percentage value of the difference(not sure how but Ill poke around on the forums).

Thanks everyone for their help so far I really appreciate.

Code:
//AS5040 + VVVV over serial + L298N + PWM.lib
#include <PWM.h>

//AS5040 stuff
const int clockPin = 7; //output to clock
const int CSnPin = 6; //output to chip select
const int inputPin = 2; //read AS5040
int inputstream = 0; //one bit read from pin
long packeddata = 0; //two bytes concatenated from inputstream
long ReadEncoder = 0; //holds processed angle value
long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info
long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info
long statusbits; //holds status/error information
int DECn; //bit holding decreasing magnet field error data
int INCn; //bit holding increasing magnet field error data
int OCF; //bit holding startup-valid bit
int COF; //bit holding cordic DSP processing error data
int LIN; //bit holding magnet field displacement error data
int debug = 0; //SET THIS TO 0 TO DISABLE PRINTING OF ERROR CODES
int shortdelay = 1; // this is the microseconds of delay in the data clock
int longdelay = 100;
//int rotations = 0;  //how many times have I spun around
//int overallpostion = 0;  //count rotations plus postions into a new value
//int global_counter = 0;
int PreviousEncoder = 0;
int Position = 0;
int Encoder = 0;
int difference = 0;
int Globaladd = 0;
long GlobalPosition = 0;
// Serial for VVVV
String readString;
int Predifference = 0;
//PWM fast
int ENA = 9;//fast PWM
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 16000; //frequency (in Hz)


void setup_sv4()
{
  Serial.begin(115200);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  //Serial.println("V4_Arduino_L298N_AS5040");//track what I have loaded
 
}

void setup()
{
  Serial.begin(9600);
  pinMode(clockPin, OUTPUT); // SCK
  pinMode(CSnPin, OUTPUT); // CSn -- has to toggle high and low to signal chip to start data transfer
  pinMode(inputPin, INPUT); // SDA
 
  setup_sv4();
}
void loop_sv4() {

  if (Serial.available()) 
  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == 'a')
{
if (readString.length() >0)
        {
int SerialInput = readString.toInt();  //convert readString into a number
                        int Xaxis = GlobalPosition; //counted rotations and postion
                        int DiffLeft = SerialInput - Xaxis;
                        int DiffRight = Xaxis - SerialInput;
                     
                        if (SerialInput > Xaxis)
                        {
                        digitalWrite (IN1, HIGH);
                        digitalWrite (IN2, LOW);
                        int Lspd = map(DiffLeft, 0, 5000, 404, 490);
                        pwmWrite  (ENA, (Lspd) );
                        Serial.print("Lspd: ");
                        Serial.println(DiffLeft, DEC);
                        }
                        else //if (DiffRight > 50)
                        {
                        digitalWrite (IN1, LOW);
                        digitalWrite (IN2, HIGH);
                        int Rspd = map(DiffRight, 0, 5000, 404, 490);
                        pwmWrite  (ENA, (Rspd) );
                        Serial.print("Rspd: ");
                        Serial.println(DiffRight, DEC);
                        readString=""; //clears variable for new input
                        }
     
}

    else if (c == 'b')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

    else if (c == 'c')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

else
{     
/// be sure this value is >= '0' && <= '9'
readString += c; //makes the string readString
}
  }
}



void loop()
{
  loop_sv4();
// CSn needs to cycle from high to low to initiate transfer. Then clock cycles. As it goes high
// again, data will appear on sda
  digitalWrite(CSnPin, HIGH); // CSn high
  digitalWrite(clockPin, HIGH); // CLK high
  delay(shortdelay);// time between readings
  digitalWrite(CSnPin, LOW); // CSn low: start of transfer
  delayMicroseconds(shortdelay); // delay for chip initialization
  digitalWrite(clockPin, LOW); // CLK goes low: start clocking
  delayMicroseconds(shortdelay); // hold low
  for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin
  {
    digitalWrite(clockPin, HIGH); //clock goes high
    delayMicroseconds(shortdelay); //
    inputstream =digitalRead(inputPin); // read one bit of data from pin
    packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value
    digitalWrite(clockPin, LOW);
    delayMicroseconds(shortdelay); // end of one clock cycle
  }

  ReadEncoder = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle.
  ReadEncoder = (ReadEncoder >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
  Position = ReadEncoder; //get current position of sensor
  difference = Position - PreviousEncoder;  //first time round its == 0         
         
         
         if ( difference > 512 ) //condition 1
         {
             Globaladd = difference - 1024;
         }
         else if ( difference < -512 )
         {
             Globaladd = difference + 1024;
         }
         else
         {
             Globaladd = difference;
         }
                     
 //Serial.print("Globaladd: "); // and, finally, print it.
// Serial.println(Globaladd, DEC);                     
                     
GlobalPosition = GlobalPosition + Globaladd;

PreviousEncoder = Position;

 //Serial.print("Position: "); // and, finally, print it.
// Serial.println(Position, DEC);
// Serial.print("PreviousEncoder: "); // and, finally, print it.
 //Serial.println(PreviousEncoder, DEC);
 //Serial.print("difference: "); // and, finally, print it.
 //Serial.println(difference, DEC);
 //Serial.print("GlobalPosition: "); // and, finally, print it.
 //Serial.println(GlobalPosition, DEC);
  if (debug)
  {
    statusbits = packeddata & statusmask;
    DECn = statusbits & 2; // goes high if magnet moved away from IC
    INCn = statusbits & 4; // goes high if magnet moved towards IC
    LIN = statusbits & 8; // goes high for linearity alarm
    COF = statusbits & 16; // goes high for cordic overflow: data invalid
    OCF = statusbits & 32; // this is 1 when the chip startup is finished.
   // if (DECn && INCn) { Serial.println("magnet moved out of range"); }
    //else
    {
     // if (DECn) { Serial.println("magnet moved away from chip"); }
    //  if (INCn) { Serial.println("magnet moved towards chip"); }
    }
   // if (LIN) { Serial.println("linearity alarm: magnet misaligned? Data questionable."); }
   // if (COF) { Serial.println("cordic overflow: magnet misaligned? Data invalid."); }
  }

  packeddata = 0; // reset both variables to zero so they don't just accumulate
  //angle = 0;
 
 
}

4  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 23, 2013, 06:39:47 am
Ok so thanks for that managed to get a valid difference using the below section.  I tried to make the function calculate a global value but seems I am not really getting something right there..any more pointers?
Code:

//All variables are global
  Position = ReadEncoder; //get current position of sensor
  difference = Position - PreviousEncoder;  //first time round its == 0          
        
        
         if (difference > 512 && difference != 0) //condition 1
             {Globaladd = difference - 1024;}
            
             if (difference < -512 && difference != 0)
                  {Globaladd = difference + 1024;}
            
                      else Globaladd = 0;
                      
GlobalPosition = GlobalPosition + Globaladd;

PreviousEncoder = Position;

 Serial.print("Position: ");
 Serial.println(Position, DEC);
 Serial.print("PreviousEncoder: ");
 Serial.println(PreviousEncoder, DEC);
 Serial.print("difference: ");
 Serial.println(difference, DEC);

5  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 22, 2013, 08:04:55 am
Code:
////////////////////// as5040
#include <PWM.h>

//AS5040 stuff
const int clockPin = 7; //output to clock
const int CSnPin = 6; //output to chip select
const int inputPin = 2; //read AS5040
int inputstream = 0; //one bit read from pin
long packeddata = 0; //two bytes concatenated from inputstream
long ReadEncoder = 0; //holds processed angle value
long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info
long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info
long statusbits; //holds status/error information
int DECn; //bit holding decreasing magnet field error data
int INCn; //bit holding increasing magnet field error data
int OCF; //bit holding startup-valid bit
int COF; //bit holding cordic DSP processing error data
int LIN; //bit holding magnet field displacement error data
int debug = 0; //SET THIS TO 0 TO DISABLE PRINTING OF ERROR CODES
int shortdelay = 1; // this is the microseconds of delay in the data clock
int longdelay = 100;
//int rotations = 0;  //how many times have I spun around
//int overallpostion = 0;  //count rotations plus postions into a new value
//int global_counter = 0;
int PreviousEncoder = 0;
int Position = 0;
int Encoder = 0;
int difference = 0;
int Globaladd = 0;
long GlobalPosition = 0;
// Serial for VVVV
String readString;

//PWM fast
int ENA = 9;//fast PWM
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 16000; //frequency (in Hz)


void setup_sv4()
{
  Serial.begin(115200);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  //Serial.println("V4_Arduino_L298N_AS5040");//track what I have loaded
 
}

void setup()
{
  Serial.begin(9600);
  pinMode(clockPin, OUTPUT); // SCK
  pinMode(CSnPin, OUTPUT); // CSn -- has to toggle high and low to signal chip to start data transfer
  pinMode(inputPin, INPUT); // SDA
 
  setup_sv4();
}
void loop_sv4() {

  if (Serial.available()) 
  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == 'a')
{
if (readString.length() >0)
{
int SerialInput = readString.toInt();  //convert readString into a number
                        int Xaxis = ReadEncoder;//magnet position
                     //   Serial.print("global_counter: "); // and, finally, print it.
                       // Serial.println(global_counter, DEC);
                        int DiffLeft = SerialInput - Xaxis;
                        int DiffRight = Xaxis - SerialInput;
                       
                        if (SerialInput > Xaxis)
                                            {
                                             digitalWrite (IN1, HIGH);
                                             digitalWrite (IN2, LOW);
                                             int Lspd = map(DiffLeft, 0, 1023, 404, 490);
                                             pwmWrite  (ENA, (Lspd) );
                                           //  Serial.println(DiffLeft, DEC);
                                             }
                       
                        else
                        {
                        digitalWrite (IN1, LOW);
                        digitalWrite (IN2, HIGH);
                        int Rspd = map(DiffRight, 0, 1023, 404, 490);
                        pwmWrite  (ENA, (Rspd) );
                        readString=""; //clears variable for new input
                      //  Serial.println(DiffRight, DEC);
                       
                        }
                       // Serial.println(ReadEncoder, DEC);
                       
                         
}

    else if (c == 'b')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

    else if (c == 'c')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

else
{     
/// be sure this value is >= '0' && <= '9'
readString += c; //makes the string readString
}
  }
}



void loop()
{
  loop_sv4();
// CSn needs to cycle from high to low to initiate transfer. Then clock cycles. As it goes high
// again, data will appear on sda
  digitalWrite(CSnPin, HIGH); // CSn high
  digitalWrite(clockPin, HIGH); // CLK high
  delay(shortdelay);// time between readings
  digitalWrite(CSnPin, LOW); // CSn low: start of transfer
  delayMicroseconds(shortdelay); // delay for chip initialization
  digitalWrite(clockPin, LOW); // CLK goes low: start clocking
  delayMicroseconds(shortdelay); // hold low
  for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin
  {
    digitalWrite(clockPin, HIGH); //clock goes high
    delayMicroseconds(shortdelay); //
    inputstream =digitalRead(inputPin); // read one bit of data from pin
    packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value
    digitalWrite(clockPin, LOW);
    delayMicroseconds(shortdelay); // end of one clock cycle
  }

  ReadEncoder = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle.
  ReadEncoder = (ReadEncoder >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
 
  Position = ReadEncoder;
  PreviousEncoder = Position;
  Encoder = ReadEncoder;
  difference = Position - PreviousEncoder;
 
             if (difference == 0)
             {
             Globaladd = 0;}
             else if (difference < 512){
             Globaladd = difference - 1024;}
             else if (difference > 512){
             Globaladd = difference + 1024;}
             
 GlobalPosition = GlobalPosition + Globaladd;
                                     
 //Serial.print("difference: "); // and, finally, print it.
 //Serial.println(difference, DEC);
 
// Serial.print("GlobalPosition: "); // and, finally, print it.
 Serial.println(GlobalPosition, DEC);

 // angle = angle * 0.3515; // angle * (360/1024) == actual degrees
  //Serial.print("angle: "); // and, finally, print it.
  //Serial.println(angle, DEC);

  if (debug)
  {
    statusbits = packeddata & statusmask;
    DECn = statusbits & 2; // goes high if magnet moved away from IC
    INCn = statusbits & 4; // goes high if magnet moved towards IC
    LIN = statusbits & 8; // goes high for linearity alarm
    COF = statusbits & 16; // goes high for cordic overflow: data invalid
    OCF = statusbits & 32; // this is 1 when the chip startup is finished.
    if (DECn && INCn) { Serial.println("magnet moved out of range"); }
    else
    {
      if (DECn) { Serial.println("magnet moved away from chip"); }
      if (INCn) { Serial.println("magnet moved towards chip"); }
    }
    if (LIN) { Serial.println("linearity alarm: magnet misaligned? Data questionable."); }
    if (COF) { Serial.println("cordic overflow: magnet misaligned? Data invalid."); }
  }

  packeddata = 0; // reset both variables to zero so they don't just accumulate
  //angle = 0;
 
 
}

6  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 22, 2013, 07:51:28 am
like I say the sensor itself works fine..any slight linearity errors don't both this application as its on gear reduction that it becomes barley noticeable..my problem is setting up the code to accumulate the value of the 0 -1023 positions it gives out..
7  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 22, 2013, 07:24:42 am
yep I can see its not doing anything..maybe there is a way to store this value outside this loop somehow any pointers?
8  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 22, 2013, 06:41:08 am
i did try and write something but still need some pointers of how to actually get it to read and store those difference values..here is what I have so far but it just returns 0 for the difference.

Code:
ReadEncoder = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle.
  ReadEncoder = (ReadEncoder >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
 
  Position = ReadEncoder;
  PreviousEncoder = Position;
  Encoder = ReadEncoder;
  difference = Position - PreviousEncoder;
 
             if (difference < 512){
             Globaladd = difference - 1024;}
             else if (difference > 512){
             Globaladd = difference + 1024;}
             
 GlobalPosition = GlobalPosition + Globaladd;
9  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 22, 2013, 05:30:14 am
Ok im starting to get the basics of the idea, however where is the best place an if statement to calculate the old vs new position.  If I put it in my main loop then the angle position is going to be the same so it will always return 0. I cant quite get my head around that.

The magnet always puts out an absolute value each time my void loop_sv4(); function runs then I compare the serial input value with the encoder position to determine how fast the PWM should run(which works)..but still how to store the read position of the encoder to the newly read position of the encoder is a bit baffling 

10  Using Arduino / Programming Questions / Re: magnetic encoder accumulation into new value on: May 21, 2013, 02:12:23 pm
the as5040 I got as samples and is working great so far..but its more of an issue for me on how to code the counts and set the position for multiple rotations..so I want it to go to postion 4000 which is a few rotations..how would I tell it too based on the fact that i only have 0 to 1024 plus a count of the rotation...how would i et that point to go beyond the 1024.
11  Using Arduino / Programming Questions / magnetic encoder accumulation into new value on: May 21, 2013, 01:00:53 pm
I have a magnetic encoder that I can read values in the range of 0 to 1024.  I need a way to be able to accumulate this value into a new value for example 2 rotations would be 2048 or -2048.  I'm using a serial output variable to set this new position but cant seem to get my head around how I might tell it to do so.  The attached code is working for the range in which I have mapped 1024 to 360.  I want to use this accumulating value to be able to keep track of the position even if i rotate many times.

Most of the important things are in
void loop_sv4()

Code:
////////////////////// as5040
#include <PWM.h>

//AS5040 stuff
const int clockPin = 7; //output to clock
const int CSnPin = 6; //output to chip select
const int inputPin = 2; //read AS5040
int inputstream = 0; //one bit read from pin
long packeddata = 0; //two bytes concatenated from inputstream
long angle = 0; //holds processed angle value
long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info
long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info
long statusbits; //holds status/error information
int DECn; //bit holding decreasing magnet field error data
int INCn; //bit holding increasing magnet field error data
int OCF; //bit holding startup-valid bit
int COF; //bit holding cordic DSP processing error data
int LIN; //bit holding magnet field displacement error data
int debug = 0; //SET THIS TO 0 TO DISABLE PRINTING OF ERROR CODES
int shortdelay = 1; // this is the microseconds of delay in the data clock
int rotations = 0;  //how many times have I spun around
int overallpostion = 0;  //count rotations plus postions into a new value

//PID stuff
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

// Serial for VVVV
String readString;

//PWM fast
int ENA = 9;//fast PWM
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 16000; //frequency (in Hz)


void setup_sv4()
{
  Serial.begin(115200);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  //Serial.println("V4_Arduino_L298N_AS5040");//track what I have loaded
  
}

void setup()
{
  Serial.begin(9600);
  pinMode(clockPin, OUTPUT); // SCK
  pinMode(CSnPin, OUTPUT); // CSn -- has to toggle high and low to signal chip to start data transfer
  pinMode(inputPin, INPUT); // SDA
  
  setup_sv4();
}
void loop_sv4() {

  if (Serial.available())  
  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == 'a')
{
if (readString.length() >0)
{
int SerialInput = readString.toInt();  //convert readString into a number
                        int Xaxis = angle;//magnet position
                        int DiffLeft = SerialInput - Xaxis;
                        int DiffRight = Xaxis - SerialInput;
                        //int BreakL = 1;
                        //int BreakR = 1;
                    
                        //if (SerialInput > Xaxis && DiffLeft > BreakL) //> <
                        if (SerialInput > Xaxis) //>
                                            {
                   
                                             digitalWrite (IN1, HIGH);
                                             digitalWrite (IN2, LOW);
                                             int Lspd = map(DiffLeft, 0, 360, 404, 490);
                                             pwmWrite  (ENA, (Lspd) );
                                             Serial.println(DiffLeft, DEC);
                                             }
                        //f (DiffRight > BreakR)
                        else
                        {
                        digitalWrite (IN1, LOW);
                        digitalWrite (IN2, HIGH);
                        int Rspd = map(DiffRight, 0, 360, 404, 490);
                        pwmWrite  (ENA, (Rspd) );
                        readString=""; //clears variable for new input
                        Serial.println(DiffRight, DEC);
                      
                        }
                        Serial.println(angle, DEC);
                        
                        
}
}  
    else if (c == 'b')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}
}  
    else if (c == 'c')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}
}  
else
{    
/// be sure this value is >= '0' && <= '9'
readString += c; //makes the string readString
}
  }
}



void loop()
{
  loop_sv4();
// CSn needs to cycle from high to low to initiate transfer. Then clock cycles. As it goes high
// again, data will appear on sda
  digitalWrite(CSnPin, HIGH); // CSn high
  digitalWrite(clockPin, HIGH); // CLK high
  delay(shortdelay);// time between readings
  digitalWrite(CSnPin, LOW); // CSn low: start of transfer
  delayMicroseconds(shortdelay); // delay for chip initialization
  digitalWrite(clockPin, LOW); // CLK goes low: start clocking
  delayMicroseconds(shortdelay); // hold low
  for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin
  {
    digitalWrite(clockPin, HIGH); //clock goes high
    delayMicroseconds(shortdelay); //
    inputstream =digitalRead(inputPin); // read one bit of data from pin
    packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value
    digitalWrite(clockPin, LOW);
    delayMicroseconds(shortdelay); // end of one clock cycle
  }

  angle = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle.
  angle = (angle >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
  angle = angle * 0.3515; // angle * (360/1024) == actual degrees
  //Serial.print("angle: "); // and, finally, print it.
  //Serial.println(angle, DEC);

  if (debug)
  {
    statusbits = packeddata & statusmask;
    DECn = statusbits & 2; // goes high if magnet moved away from IC
    INCn = statusbits & 4; // goes high if magnet moved towards IC
    LIN = statusbits & 8; // goes high for linearity alarm
    COF = statusbits & 16; // goes high for cordic overflow: data invalid
    OCF = statusbits & 32; // this is 1 when the chip startup is finished.
    if (DECn && INCn) { Serial.println("magnet moved out of range"); }
    else
    {
      if (DECn) { Serial.println("magnet moved away from chip"); }
      if (INCn) { Serial.println("magnet moved towards chip"); }
    }
    if (LIN) { Serial.println("linearity alarm: magnet misaligned? Data questionable."); }
    if (COF) { Serial.println("cordic overflow: magnet misaligned? Data invalid."); }
  }

  packeddata = 0; // reset both variables to zero so they don't just accumulate
  //angle = 0;
  
  
}

12  Using Arduino / Programming Questions / Re: Pwm + L298n + As5040 basic servo type loop help on: May 21, 2013, 09:52:32 am
sorry had the wrong sketch..fixed now
13  Using Arduino / Programming Questions / Pwm + L298n + As5040 basic servo type loop help on: May 21, 2013, 09:39:30 am
I have the basics of what im working functioning and that is

A: reading an AS5040 magnetic encoder position
B: reading in serial data
C: Writing PWM using the custom PWM lib
...but the last part
D:  Controlling the speed and position of the DC motor depending on how far or close the the magnet is compared to the serial input im giving it..doesnt really work

Im thinking Im not storing the value of Angle for if statement in void loop_sv4() or something else im missing..any help would be appreciated

The values from the serial are in the range of 0 - 360 just like the angle..and I do a  little basic math in that function to determine and map the PWM ..but its erratic and not functioning

Code:

////////////////////// as5040
#include <PID_v1.h>  //for tuning
#include <PWM.h>

//AS5040 stuff
const int clockPin = 7; //output to clock
const int CSnPin = 6; //output to chip select
const int inputPin = 2; //read AS5040
int inputstream = 0; //one bit read from pin
long packeddata = 0; //two bytes concatenated from inputstream
long angle = 0; //holds processed angle value
long anglemask = 65472; //0x1111111111000000: mask to obtain first 10 digits with position info
long statusmask = 63; //0x000000000111111; mask to obtain last 6 digits containing status info
long statusbits; //holds status/error information
int DECn; //bit holding decreasing magnet field error data
int INCn; //bit holding increasing magnet field error data
int OCF; //bit holding startup-valid bit
int COF; //bit holding cordic DSP processing error data
int LIN; //bit holding magnet field displacement error data
int debug = 0; //SET THIS TO 0 TO DISABLE PRINTING OF ERROR CODES
int shortdelay = 1; // this is the microseconds of delay in the data clock

//PID stuff
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

// Serial for VVVV
String readString;

//PWM fast
int ENA = 9;//fast PWM
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 16000; //frequency (in Hz)


void setup_sv4()
{
  Serial.begin(115200);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  //Serial.println("V4_Arduino_L298N_AS5040");//track what I have loaded
 
}

void setup()
{
  Serial.begin(9600);
  pinMode(clockPin, OUTPUT); // SCK
  pinMode(CSnPin, OUTPUT); // CSn -- has to toggle high and low to signal chip to start data transfer
  pinMode(inputPin, INPUT); // SDA
 
  setup_sv4();
}
void loop_sv4() {

  if (Serial.available()) 
  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == 'a')
{
if (readString.length() >0)
{
int SerialInput = readString.toInt();  //convert readString into a number
                        int Xaxis = angle;//magnet position
                       
                        if (SerialInput > Xaxis) //if serial is greater than the magnet position
                        {
int DiffLeft = SerialInput - Xaxis;

                            digitalWrite (IN1, HIGH);
                            digitalWrite (IN2, LOW);
                            int Lspd = map(DiffLeft, 0, 360, 290, 510);//leftspeed should be a percentage of diff
                            pwmWrite  (ENA, (Lspd) );
                           
                           
                        }
                        else
                        {
                        int DiffRight = Xaxis - SerialInput;
                        digitalWrite (IN1, LOW);
                        digitalWrite (IN2, HIGH);
                        int Rspd = map(DiffRight, 0, 360, 290, 510);
                        pwmWrite  (ENA, (Rspd) );
                        readString=""; //clears variable for new input
                        }
                       
}

    else if (c == 'b')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

    else if (c == 'c')
{

if (readString.length() >0)
{
int n = readString.toInt();  //convert readString into a number
readString=""; //clears variable for new input
}

else
{     
/// be sure this value is >= '0' && <= '9'
readString += c; //makes the string readString
}
  }
}



void loop()
{
  loop_sv4();
// CSn needs to cycle from high to low to initiate transfer. Then clock cycles. As it goes high
// again, data will appear on sda
  digitalWrite(CSnPin, HIGH); // CSn high
  digitalWrite(clockPin, HIGH); // CLK high
  delay(shortdelay);// time between readings
  digitalWrite(CSnPin, LOW); // CSn low: start of transfer
  delayMicroseconds(shortdelay); // delay for chip initialization
  digitalWrite(clockPin, LOW); // CLK goes low: start clocking
  delayMicroseconds(shortdelay); // hold low
  for (int x=0; x <16; x++) // clock signal, 16 transitions, output to clock pin
  {
    digitalWrite(clockPin, HIGH); //clock goes high
    delayMicroseconds(shortdelay); //
    inputstream =digitalRead(inputPin); // read one bit of data from pin
    packeddata = ((packeddata << 1) + inputstream);// left-shift summing variable, add pin value
    digitalWrite(clockPin, LOW);
    delayMicroseconds(shortdelay); // end of one clock cycle
  }

  angle = packeddata & anglemask; // mask rightmost 6 digits of packeddata to zero, into angle.
  angle = (angle >> 6); // shift 16-digit angle right 6 digits to form 10-digit value
  angle = angle * 0.3515; // angle * (360/1024) == actual degrees
  //Serial.print("angle: "); // and, finally, print it.
  Serial.println(angle, DEC);

  if (debug)
  {
    statusbits = packeddata & statusmask;
    DECn = statusbits & 2; // goes high if magnet moved away from IC
    INCn = statusbits & 4; // goes high if magnet moved towards IC
    LIN = statusbits & 8; // goes high for linearity alarm
    COF = statusbits & 16; // goes high for cordic overflow: data invalid
    OCF = statusbits & 32; // this is 1 when the chip startup is finished.
    if (DECn && INCn) { Serial.println("magnet moved out of range"); }
    else
    {
      if (DECn) { Serial.println("magnet moved away from chip"); }
      if (INCn) { Serial.println("magnet moved towards chip"); }
    }
    if (LIN) { Serial.println("linearity alarm: magnet misaligned? Data questionable."); }
    if (COF) { Serial.println("cordic overflow: magnet misaligned? Data invalid."); }
  }

  packeddata = 0; // reset both variables to zero so they don't just accumulate
  angle = 0;
 
 
}




14  Development / Other Software Development / Re: PWM frequency library on: April 15, 2013, 06:57:21 am
one question, how do I determine the PWM range fora a given pin with a modified frequency.  Take for example my sketch below that uses 20khz on pin 9.  Normally the values 0 256 work fine without the mod but I had to raise it to 256 to 512..it doesn't go as high as 512 more like in the 400 range..just want to know what I'm missing to understand that and if there is an easy calculation.

Code:
// simple move forward and backward one motor on the l298n using pins 12 13 9/ 9 being PWM
#include <PWM.h>
int ENA = 9;
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 20000; //frequency (in Hz)

void setup ()
{
  Serial.begin(9600);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
}

void test_motor_1 ()
{
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (30);
    Serial.println(i);
  }

  delay (100);
  digitalWrite (IN1, LOW);
  
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    pwmWrite  (ENA, i);  //was analogWrite
    delay (30);
   Serial.println(i);
  }

  delay (100);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}
15  Using Arduino / Motors, Mechanics, and Power / Re: DIY L298n motor controller - need a little help on: April 15, 2013, 06:54:02 am
that completely resolves the PWM noise issue it runs at 25k no problem and the l298 seems to handle that well.  I have no heating issues and a fair amount of torque.
Here is a little test sketch I made that runs PWM on pin 9 forwards and backwards

Code:
// simple move forward and backward one motor on the l298n using pins 12 13 9/ 9 being PWM
#include <PWM.h>
int ENA = 9;
int IN1 = 12;
int IN2 = 13;
int32_t frequency = 20000; //frequency (in Hz)

void setup ()
{
  Serial.begin(9600);
  InitTimersSafe();
  bool success = SetPinFrequencySafe(ENA, frequency);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
}

void test_motor_1 ()
{
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite (ENA, i);
    delay (30);
    Serial.println(i);
  }

  delay (100);
  digitalWrite (IN1, LOW);
 
  for (int i = 256; i < 512; i++)
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite (ENA, i);
    delay (30);
   Serial.println(i);
  }

  delay (100);
  digitalWrite (IN2, LOW);
}

void loop()
{
  test_motor_1();
}
Pages: [1] 2 3 4