Show Posts
Pages: [1] 2 3 ... 7
1  Using Arduino / Programming Questions / serialEvent() string reconstruction and interpreting issue. on: September 17, 2014, 12:30:01 pm
Hello,

For my application I am communicating to a motor driver via the serial monitor by sending strings. To do this I am using a serialEvent() function to build the string before sending it off to Serial3. It looks something like this.

Code:
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    Command += inChar;
    if (inChar == '\n')
      commandComplete = true;
  }
}

void loop() {
  if (commandComplete) {
      Serial3.print(Command);
    }
    Command = "";
    commandComplete = false;
  }
}

I am having an issue whereby I want to interpret the string and if the string is required to something (someAction), it does, else it sends the command to Serial3. Something like:

Code:
  if (commandComplete) {
    if (Command == ("someAction") {
      //do someAction
    } else {
      Serial3.print(Command);
    }
    Command = "";
    commandComplete = false;
  }

The problem is that the code (possibly the string reconstruction) cannot interpret the "someAction" string and immediately sends it off to Serial3. I am sure this problem has to do with data structure but I cannot figure it out. I also tried adding a line feed, but that is not working.

Code:
  if (commandComplete) {
    if (Command == ("someAction" + '\n') {
      //do someAction
    } else {
      Serial3.print(Command);
    }
    Command = "";
    commandComplete = false;
  }
2  Products / Arduino Due / Re: Serial.print() Timer Routine - Arduino Due on: September 15, 2014, 03:03:42 pm
Perhaps this is a general timer interrupt question but I was wondering what changes would you do if you would like an interrupt every 5 seconds (0.2 Hz). It is not as intuitive to just using 0.5 as the final parameter in startTimer() as the function requires an int. I successfully did this by multiplying the variable 'rc' by 5, but I would like to figure out a more intuitive way of doing this.

I am going to post the repeatedly posted code for reference.

Code:
volatile boolean l;

//TC1 ch 0
void TC3_Handler()
{
        TC_GetStatus(TC1, 0);
        digitalWrite(13, l = !l);
}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
        pmc_set_writeprotect(false);
        pmc_enable_periph_clk((uint32_t)irq);
        TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
        uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        TC_SetRC(tc, channel, rc);
        TC_Start(tc, channel);
        tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
        tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
        NVIC_EnableIRQ(irq);
}

void setup(){
        pinMode(13,OUTPUT);
        startTimer(TC1, 0, TC3_IRQn, 4); //TC1 channel 0, the IRQ for that channel and the desired frequency
}

void loop(){
}

3  Products / Arduino Due / Re: Serial.print() Timer Routine - Arduino Due on: September 15, 2014, 02:37:01 pm
Why you can't you just use millis() to see if it's time?

PaulS,

Thank you but this method still requires polling of a variable and lots of overhead that is not needed. Perhaps I should have been a little more clear of my application. It is real-time embedded control. So mills() has shown to fail in these types of applications. Certainly mine it did.

here's a basic example using timer1, channel 0, at 4 Hz
the last argument to startTimer() sets the frequency

http://forum.arduino.cc/index.php?topic=219941.0

Thank you SignTorch,

I actually have found a thread explaining this more in detail. I appreciate the alternate link. Here is the one I found.

http://forum.arduino.cc/index.php?topic=130423.0
4  Products / Arduino Due / Serial.print() Timer Routine - Arduino Due on: September 15, 2014, 11:59:52 am
Hello,

I am trying to understand timers and figuring out how to print to a serial port at a specified frequency. I am having troubles finding some code or example to follow, specifically for the Arduino Due.

Thank you,

5  Using Arduino / Programming Questions / Re: Quadrature Encoder.h Issue on: September 09, 2014, 11:22:15 am
Hey Cattledog,

Yes, I can definitely share with you what I did. Before I continue, I just want to let you know that this is using Arduino Due. As I mentioned before I have 7 encoders I would like to interpret.

First off declaring pins of channels A and B from the encoders.

Code:
int m1_CHA = 21;    
int m1_CHB = 20;
int s1_CHA = 52;    
int s1_CHB = 50;

int m2_CHA = 19;  
int m2_CHB = 18;  
int s2_CHA = 24;  
int s2_CHB = 22;  

int m3_CHA = 17;    
int m3_CHB = 16;  
int s3_CHA = 53;    
int s3_CHB = 51;  

int s4_CHA = 25;
int s4_CHB = 23;

Secondly,  declaring volatile variables that represent the current position of the encoder.

Code:
volatile float m1p = 0;
volatile float s1p = 0;
volatile float m2p = 0;
volatile float s2p = 0;
volatile float m3p = 0;
volatile float s3p = 0;
volatile float s4p = 0;

Third, attaching interrupts on one of the channels of each encoder (in my case CHA). It is important to do both RISING and FALLINg in order to get quadrature counts (4x), with respecting ISR. As well as declaring the pinMode of CHB as inputs.

Code:
 attachInterrupt(m1_CHA, m1_ISR_RISING, RISING);
  attachInterrupt(s1_CHA, s1_ISR_RISING, RISING);
  attachInterrupt(m2_CHA, m2_ISR_RISING, RISING);
  attachInterrupt(s2_CHA, s2_ISR_RISING, RISING);
  attachInterrupt(m3_CHA, m3_ISR_RISING, RISING);
  attachInterrupt(s3_CHA, s3_ISR_RISING, RISING);
  attachInterrupt(s4_CHA, s4_ISR_RISING, RISING);
  
  attachInterrupt(m1_CHA, m1_ISR_FALLING, FALLING);
  attachInterrupt(s1_CHA, s1_ISR_FALLING, FALLING);
  attachInterrupt(m2_CHA, m2_ISR_FALLING, FALLING);
  attachInterrupt(s2_CHA, s2_ISR_FALLING, FALLING);
  attachInterrupt(m3_CHA, m3_ISR_FALLING, FALLING);
  attachInterrupt(s3_CHA, s3_ISR_FALLING, FALLING);
  attachInterrupt(s4_CHA, s4_ISR_FALLING, FALLING);

  pinMode(m1_CHB, INPUT);
  pinMode(s1_CHB, INPUT);
  pinMode(m2_CHB, INPUT);
  pinMode(s2_CHB, INPUT);
  pinMode(m3_CHB, INPUT);
  pinMode(s3_CHB, INPUT);
  pinMode(s4_CHB, INPUT);

Lastly, the RISING and FALLING ISRs required. Note in order for the counts to not cancel out with one another, RISING and FALLING edges have opposite signs (increment vs. decrement)

Code:
// RISING EDGE ISRs
void m1_ISR_RISING() { if (digitalRead(m1_CHB) == 0)  m1p += 1; else m1p -= 1; }
void m2_ISR_RISING() { if (digitalRead(m2_CHB) == 0)  m2p += 1; else m2p -= 1; }
void m3_ISR_RISING() { if (digitalRead(m3_CHB) == 0)  m3p += 1; else m3p -= 1; }
void s1_ISR_RISING() { if (digitalRead(s1_CHB) == 0)  s1p += 1; else s1p -= 1; }
void s2_ISR_RISING() { if (digitalRead(s2_CHB) == 0)  s2p += 1; else s2p -= 1; }
void s3_ISR_RISING() { if (digitalRead(s3_CHB) == 0)  s3p += 1; else s3p -= 1; }
void s4_ISR_RISING() { if (digitalRead(s4_CHB) == 0)  s4p += 1; else s4p -= 1; }

// FALLING EDGE ISRs
void m1_ISR_FALLING() { if (digitalRead(m1_CHB) == 0)  m1p -= 1; else m1p += 1; }
void m2_ISR_FALLING() { if (digitalRead(m2_CHB) == 0)  m2p -= 1; else m2p += 1; }
void m3_ISR_FALLING() { if (digitalRead(m3_CHB) == 0)  m3p -= 1; else m3p += 1; }
void s1_ISR_FALLING() { if (digitalRead(s1_CHB) == 0)  s1p -= 1; else s1p += 1; }
void s2_ISR_FALLING() { if (digitalRead(s2_CHB) == 0)  s2p -= 1; else s2p += 1; }
void s3_ISR_FALLING() { if (digitalRead(s3_CHB) == 0)  s3p -= 1; else s3p += 1; }
void s4_ISR_FALLING() { if (digitalRead(s4_CHB) == 0)  s4p -= 1; else s4p += 1; }

Cattledog, please expand and suggest how to implement and use bitRead, I have never used it before. I hope it would be easily portable into the my code above.

Thank you,

PS: I apologize for the late response.
6  Using Arduino / Programming Questions / Re: Quadrature Encoder.h Issue on: September 05, 2014, 01:08:57 pm
I ended up making my functions for the quadrature counting. Works fine now.
7  Using Arduino / Programming Questions / Re: Quadrature Encoder.h Issue on: September 05, 2014, 09:45:05 am
Hello Cattledog,

I tried a couple of methods.

1) Commenting out ENCODER_OPTIMIZE_INTERRUPTS. This did not change the results - the 2 encoders that weren't working were still not working.

2) Trying out ENCODER_USE_INTERRUPTS. Same results as above.

3) Trying out ENCODER_DO_NOT_USE_INTERRUPTS. The encoders worked but performance was absolutely horrid. Lots of missed counts were obviously shown.

Do you have any documentation on this library? From the website I linked previously, these #defines you provided me were never mentioned. Can you diagnose the problem from what I shared with you?

Looking at the Encoder.h file, it seems there is a interrupt_config.h file it includes if you #define ENCODER_OPTIMIZE_INTERRUPTS. Can a solution be to use add these pins to the config file? I have never attempted to change libraries before and this library is written in "ARM language", so it would be difficult for me to modify it - but I can give it a shot.

Thank you again,
8  Using Arduino / Programming Questions / Quadrature Encoder.h Issue on: September 04, 2014, 04:26:35 pm
Hello,

I am having an issue with PJRC's Encoder.h library, found linked below.

https://www.pjrc.com/teensy/td_libs_Encoder.html

I am attempting to read 7 quadrature encoders. Using this library, 5 out of 7 encoders are working properly.

I have scoped all these lines/pins to ensure the encoders are working properly and are consistent with the object declaration. I have checked all the pins to make sure they are properly digitalReading concluding that the Arduino Due is working properly. Does it have to something with the Encoder.h library whereby it does not accept RX1 (19), TX1 (18), RX2 (17), TX2 (16)? or am I completely blind?

The encoders that are not working, I have marked in my code with a "//*".

Code:
#define ENCODER_OPTIMIZE_INTERRUPTS

#include <Encoder.h>

// Encoder Signals

long m1p = 0;
long m2p = 0;
long m3p = 0;

long s1p = 0;
long s2p = 0;
long s3p = 0;
long s4p = 0;

Encoder m1l(21, 20);
Encoder m2(19, 18); //*
Encoder m3(17, 16); //*

Encoder s1(52,50);
Encoder s2(24, 22);
Encoder s3(53, 51);
Encoder s4(25, 23);

void setup()
{

}

void loop()
{
  m1p = m1.read();
  m2p = m2.read();
  m3p = m3.read();
  
  s1p = s1.read();
  s2p = s2.read();
  s3p = s3.read();
  s4p = s4.read();
}
9  Using Arduino / Programming Questions / Efficient Way of Sending Data via Serial on: August 27, 2014, 01:31:08 pm
Hello,

I am trying to establish a method of sending data to my PC (via serial) efficiently.  The application includes a complex control system and various sensors - so continuously polling may not be recommended.

I understand that there is a buffer associated with the serial port - and I know that in Atmel Studio you can program the buffer to initiate an interrupt in order to ask for the next byte of data in order to be sent. This, in my opinion, is the best way of doing it because it does't interrupt the rest of your code. I do not know how to implement and I come here for help and other suggestions.

For now I do:

Code:
if (millis() % 100  == 0) {
    Serial.println(data);
  }

But at faster rates the control system becomes unstable and data packets are never sent periodically. What other efficient ways of sending data serially (without interrupting the rest of the code) can I implement?

Thanks,
10  Using Arduino / Microcontrollers / Re: Replacing Arduino Due Chip (ATSAM3X8E) on: August 21, 2014, 11:23:48 am
So during my first attempt, I use some flush wire cutters - I should have been a little bit smarter of how I did that.

I do not have a proper hot air gun station. I have a hot air gun that can produce enough heat but the nozzle is around 1'' - 1.5'' in diameter (more like a high-powered hairdryer).  Controller the heat would be difficult and not melt the plastic female headers.  I still have a couple of burnt out Dues, so maybe I will just give it a shot.  

Daniel
11  Using Arduino / Networking, Protocols, and Devices / XBee: Not Working Properly on: August 21, 2014, 11:16:46 am
Hello,

I have two S1 XBee (linked below).  I have had them for a while now and decided to implement them into my design.

https://www.sparkfun.com/products/11215

One of them is working perfectly fine, while the other is giving me some issues.

1) Using XCTUNG (Mac version) I am unable to detect the XBee that isn't working properly

2) The operating voltage is interesting. Using the following shield from robotshop.com, I am doing to rudimentary testing by transmitting serial commands. I am noticing that the operating voltage, which I am scoping from the transmitting pin, is 700mV rather than 3V normal operation.

http://www.robotshop.com/ca/en/motomama-h-bridge-motor-driver-shield.html

What can I do to get the XBee to working properly?

Thanks,
12  Using Arduino / Microcontrollers / Re: Replacing Arduino Due Chip (ATSAM3X8E) on: August 20, 2014, 01:51:52 pm
First attempt went horribly wrong.  After cutting the pins, the pads are extremely sensitive and did peel right off.
13  Using Arduino / Microcontrollers / Re: Replacing Arduino Due Chip (ATSAM3X8E) on: August 20, 2014, 01:13:24 pm
Since the chip is burnt out, I can just cut off the leads and touch up the pads. And do some thermal air soldering for the new chip.

What do you think?
14  Using Arduino / Microcontrollers / Replacing Arduino Due Chip (ATSAM3X8E) on: August 20, 2014, 11:33:09 am
Hello,

Over the months I keep burning my Arduino Due - since I am prototyping and making my own PCBs and getting them to integrate nicely.

Now these boards have been piling up and I was wondering if I can easily replace the chip. I understand that the AVR Arduinos require a boot loader. I was wondering if the ARM chip needs one as well.

Thanks,

Daniel
15  Using Arduino / Programming Questions / Re: Using Quadrature Encoder Library - Issue on: August 14, 2014, 02:02:14 pm
So I am still not sure why this library wasn't working properly.

I resorted back my original method. It is 4x less resolved (since I am only using 1 interrupt on one channel) but it will do for now.

If someone can figure out why it isn't working. I will be happy to learn the explanation.
Pages: [1] 2 3 ... 7