multicog variable sharing
Beavis3215
Posts: 229
When programming the propeller in C, If you have processes in cog 0, 1, and 2, how do you share data generated in cog 1 with 0 and 2?
In this example, cog 0 can't see Imax generated in cog 1.
Cog 2 needs to see imv generated in cog 1.
What is a good approach?
/* GFI for safety extension cord */ #include "simpletools.h" #include "adcDCpropab.h" void measure_imax(); void throw_breaker(); volatile int imv; volatile int imax; int main() // Main function runs 7 segment LED's in cog 0 { cog_run(measure_imax,128); // Start littlebird measurments cog_run(throw_breaker,128); adc_init(21, 20, 19, 18); set_directions(15, 1, 0b111111111111111); // pins 15 through 0 are outputs int d[] = {0b1110111, 0b1000100, 0b1101011, 0b1101110, 0b1011100, //digits 0 through 9 0b0111110, 0b0011111, 0b1100100, 0b1111111, 0b1111100}; pause(1000); // Let hardware boot up while(1) { float amps = (imax - 2700) *.003; // Scale Littlebird voltage to mean amps int a = (int) amps; // intger part of amps float b = (10 * (amps - a)); int c = (int)b; // fractinal part of amps print("%f, %d, %d\n", imax, a, c); set_outputs(15, 9, d[c]); // c is the decimal value set_output(1,1); // pin 1 is the decimal digit driver pause(8); set_output(1,0); set_outputs(15, 9, d[a]); // a is the integer value set_output(2,1); // pin 2 is the integer digit driver pause(8); set_output(2,0); } } void measure_imax() { while(1) { for (int sample = 0; sample <= 60; sample++) // Littlebird samples per loop { float i = adc_volts(2); int imv = (int)(i * 1000); // // if (imv < 2700) // { // float temp = 2700 - imv; // imv = temp + 2700; // } // float j = adc_volts(2); int jmv = (int)(j * 1000); // Invert samples below 2.7 volts if (jmv < 2700) // to the same value above 2.7 volts { // int temp = 2700 - jmv; // jmv = temp + 2700; // } // if (jmv > imv) { imv = jmv; } } imax = imv; // Stores maximum voltage sample } } void throw_breaker() { set_direction(0,1); set_output(0,1); while(1) { if (imv > 2900) { set_output(0,0); cog_end; } } }
In this example, cog 0 can't see Imax generated in cog 1.
Cog 2 needs to see imv generated in cog 1.
What is a good approach?
Comments
Whether this is a "good" approach I can't say but it does work for me.
This is strictly a user controlled method, and does not rely on any C library or function other than global scope.
EDIT: The function measure_imax declares a local variable named imv. This causes it to use the local variable imv instead of the global variable imv. However, imax should be updated.
EDIT2: The statement "imax = imv" references the global variable imv that never gets updated. Eliminate the declaration of the local variable imv.
EDIT3: You should also fix the format used in your print. You are using %f for imax, which is an int. You should use %d.