Important changes to forums

We’re making some changes to the Mbed forums. From 10th December 2019 all new discussions will take place on our new forum site. You can continue to reply to existing threads for the next two weeks. After that we will archive this forum so you can return to useful posts in the future.

a parellelism program

01 Aug 2011

i have a situation where i take readings from a certain sensor connected to the mbed and i if a reading fullfils certain conditions, i want the mbed to execute a certain action at the same time the mbed recieves and records the data comming from the sensor ... do anyone have any idea how to make this happen???

thank you in advance.

01 Aug 2011

Can you share a bit more info ..

But you could do something like ...

if ( (Sensor > MinVale) && (Sensor < MaxValue))
{

    // Do Something useful ..
    My_Led = 1;

    // Save it to  LocalFileSystem ..

    fprintf(fp, "Value Recorded = %d", Sensor);
    pc.printf ("Good reading of %d\r\n",Sensor);

}
else
{
    // did not match test criteria ..
    pc.printf ("Bad Reading ..\r\n");
}

Hope this helps,

Ceri

01 Aug 2011

thank you Ceri for trying to help me, but in this code snippet, when the criteria are matched, some useful actions are done in a sequential manner, but in my case, i use an accelerometer and a gyroscope, so i need when the accelerometer gets a certain reading that matches certain criteria, a certain action would take place at the same time the gyroscope gets the readings (which in turn if matches certain criteria,certain action is taken, which may be at the same time of execution of the action due to the accelerometer)

01 Aug 2011

I haven't seen this mentioned much on this forum, but it is an approach I use often: have you considered using multiple, interconnected mbeds?

01 Aug 2011

/me awaits an Igor slap, you just know it's coming ;)

01 Aug 2011

Quote:

...some useful actions are done in a sequential manner, but in my case, i use an accelerometer and a gyroscope...

All things are done sequentially, but things are so fast it appears to to "all happen at the same time". The first question you need to answer is how often do you get data from your sensors? Are the two sensors on the same board and sending their data together or are they discrete devices asynchronously sending data? Lets first get a handle on your "real time" needs before trying to make things happen "at the same time".

03 Aug 2011

my question is not about real time issue, but a conceptual problem. i'll give an example to clarify my point of view.

let's say i have two digital input pins, say p11 and p12, and two digital outputs, say LED1 and LED2, and i want LED1 to flash continously if p11 is high and LED2 to flash continously when p12 is high, according to this snippet

while (1) { LEDX = 1; wait(0.2); LEDX = 0; wait(0.2); }

where LEDX is LED1 or LED2 depending on the input.

now the question is, if i give p11 a high input, the above function will run continously, flashing LED1, and LED2 is off, and in this case, if i give p12 a high input, the above function will continue executing for LED1, but actually, i want the mbed to know that there might be another input so that the 2 leds flash together continously, so how this can be done???

i think that multithreadig techniques that synchronizes seemingly parallel operations will be the answer, so anyone has any idea?

03 Aug 2011

Don't use wait()....

psudeo code

#include "mbed.h"

DigitalIn P11(p11);
DigitalIn P12(p12);

DigitalOut led1(LED1);
DigitalOut led2(LED2);

Ticker t1, t2;

void onT1(void) {
  if (P11) {
    led1 = !led1;
  }
}

void onT2(void) {
  if (P12) {
    led2 = !led2;
  }
}

int main() {

  t1.attach(onT1, 0.2);
  t2.attach(onT2, 0.2);

  while(1) {
    if (!P11) led1 = 0;
    if (!P12) led2 = 0;
  }

}


03 Aug 2011

what about a loop, with a very short delay (1mS)


while (1)
{
    if (p11) { LED1_Flash = true; Led1Counter = 0; } else { LED1_Flash = False; LED1 = 0;}
    if (p12) { LED2_Flash = true; Led2Counter = 0 } else { LED2_Flash = False; LED2 = 0;}


    if (LED1_Flash )
    {
        if (Led1Counter ++ == 250) LED1 = 1;
        if (Led1Counter > 500) LED1 = 0;
    }
// .........
    if (LED2_Flash )
    {
        if (Led2Counter ++ == 250) LED21 = 1;
        if (Led2Counter > 500) LED2 = 0;
    }
// .........
    wait_ms (1);
}

Just an idea, but you could make it 10 uS, and change count values appropriately.

BTW, if you are using SPI sensors, then you can bit bash I/O lines, and have 2 or more devices, One of my Projects some years ago, had 8 Parallel SPI devices, being read in simultaneously !!!

Enjoy

Ceri

03 Aug 2011

@Ceri : i don't understand what is meant by "bit bach" ! :) could you please enlighten me? (because i think that this is what i exactly want, running several SPI sensors simultaneously)

03 Aug 2011

@BIT BASH,

your code drives the I/O pins for SPI, not the USART.

eg.

CS = 0; for x = 0 to 8 } Clock = 1 DataAllPINS = 0xff Clock = 0; }

04 Aug 2011

@Andy: thank you very much for trying to help me and i really appreciate it, but honestly, i think the code you attached doesn't achieve what i need :). i think i didn't state what i need clearly, i'm sorry for that !

frankly, i didn't try the code, but as far as i understood, the code needs that the input to be high all the time or low all the time (at pins p11 and p12) and the ticker keeps invoking the funtion that actually makes the flash (or the blink of the leds).

but actually, in my application, i use two tack switches connected to the input pins, and what i need to do is to press one of the switches once, so one of the leds keeps flashing all the time and when i press it again, it switches off, and i need this to happen in a parallel manner for the two leds, meaning that i can press one of the switches so the coresponding led keeps flashing at the same time the other led keeps flashing if its switch was presed.

sorry for being so long :) thank you in advence.

04 Aug 2011

@Ceri: thank you very much for trying to help me, but could you please clarify more the meaning of BIT BASH? because frankly, i didn't understand well what you meant, so would you please tell me more about this?

thank you in advance.

05 Aug 2011

Hassan,

Re. your request to Andy about the push buttons 'toggeling' the blinking LED's enable/disable.

I will start by address your original request for some insight on how to program for parallel actions -

The first trick is to change your focus from the on-going results you desire to the transitions between the on-going results. That is, pressing a button causes an input signal to change from high-to-low (or low-to-high). Releasing the button causes the opposite change. Andy's Ticker object signals each time the allowed LED outputs can change from on-to-off, or off-to-on.

Each of these changes can cause an interrupt, which in turn can have a small subroutine 'attached' to the interrupt. Each such subroutine will then run when the corresponding change occurs.

One subtle trap is that mechanical switches actually produce several short pulses before setteling into the new 'pushed' or 'released' state. So, you want to use the library routines for 'de-bounced' signals on the push-button inputs.

The second trick is to have the subroutines pass the changes on to 'global' variables. That way, the results from all changes are available to any of the subroutines whenever they run.

So, you can modify Andy's code to have the 2 push-button routines each toggle a different global variable (say, 'int LED0_enable, LED1_enable;') in response to a rising (or falling) de-bounced signal. You only need one Ticker if you want both LEDs to march in-step to another toggled variable (say, 'int Pulse_phase;'). Finally, in all 3 subroutines (after they have up-dated the one global variable they are responsible for), calculate and set the desired values for both LED's based on the global variables..

e.g. 'LED0 = LED0_enabled & Pulse_phase; LED1 = LED1_enabled & Pulse_phase;'

Having taken care of all possible transitions, you will then find that the on-going parallel blinking of both LEDs vs the button-pushes has also been taken care of.

Now, re. Ceri's 'BIT BASH'. 'BIT' is a designation for each of the digital input and output signals. 'BASH' is a term that refers to forming things in a crude or unsophisticated manner, as if by hammer blows. So, 'BIT BASH' means that to run the SPI's in parallel, you have to give up using the 'sophisticated' SPI channel in the 1788, and instead write all the code yourself which will set the pins you choose to in, out, or open-collector mode; set each of the clock, chip enable, and data bits (pins) to their needed states; wait for the needed minimum delays on each set of bits sent or received; monitor any open-collector timing extensions from the slaves; etc.

05 Aug 2011

Hi Hassan,

there are many ways to solve it. I'm a Java programmer, which means my code is usually a bit "simpler" than other code here !

it also means it ends up being a little bit "procedural" and very "flag driven".

For THIS specifically, I've not used flags, I'd just drive the LED's in the interrupt routine.

However.. USUALLY, I'd set some flags in the interrupt routine, and then act on them in the main loop. (where you can spend as long as you like acting on them)

because I go by the rule that interrupt routine should be FAST, main loop can be slow.

anyway.. here is some PSEUDO CODE.

there's more than 1 way to skin a cat.. I'm quite new to C, and embedded programming, so I'm more than happy for anyone else to point out any flaws in my suggestion !

#include "mbed.h"

DigitalIn P11(p11);
DigitalIn P12(p12);

DigitalOut led1(LED1);
DigitalOut led2(LED2);

Ticker t1; 

void testTheButtons() {
  //an input on p11 will toggle LED1.
  //may need to add some sort of debounce here.
  if (p11) {
   led1 = !led1;
  }

  //an input on p12 will toggle LED2.
  //may need to add some sort of debounce here.
  if (P12) {
   led2 = !led2;
  }
}

int main() {

  led1=0;
  led2=0;
  t1.attach(testTheButtons, 0.2);

  while(1) {
  wait(0.5);
  // do nothing, the ticker will notice when things happen, and set the leds.
  //might as well put a wait in here, to stop the CPU spinning ?
  }

}

05 Aug 2011

Regarding to David's "set flags in IRQ" and "IRQ should be fast, main loop slow" see this article for some more details and practicle implementations

You need to log in to post a reply