Пример #1
0
int main(void) {

    int value;
   int fd;
   int value2;
   int fd2;
  char buf[MAX_BUFF];
  char ch[5];   //Update
  char ch2[5];   //Update
  ch[4] = 0;    //Update
  
    
   int GPIOPin=60, /* GPIO1_28 or pin 12 on the P9 header */
 
    printf("\nStarting GPIO output program\n");
    FILE *myOutputHandle = NULL;
    char setValue[4], GPIOString[4], GPIOValue[64], GPIODirection[64];
    sprintf(GPIOString, "%d", GPIOPin);
    sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
    sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
    
    // Export the pin
    if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
    }
    
    strcpy(setValue, GPIOString);
    fwrite(&setValue, sizeof(char), 2, myOutputHandle);
    fclose(myOutputHandle);
 
    // Set direction of the pin to an output
     if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
        printf("Unable to open direction handle\n");
        return 1;
    }
    
    strcpy(setValue,"out");
    fwrite(&setValue, sizeof(char), 3, myOutputHandle);
    fclose(myOutputHandle);
    
    
    // export pin 2
    GPIOPin=50;
    sprintf(GPIOString, "%d", GPIOPin);
    sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
    sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
    
    // Export the pin
    if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
    }
    
    strcpy(setValue, GPIOString);
    fwrite(&setValue, sizeof(char), 2, myOutputHandle);
    fclose(myOutputHandle);
 
    // Set direction of the pin to an output
     if ((myOutputHandle = fopen(GPIODirection, "rb+")) == NULL){
        printf("Unable to open direction handle\n");
        return 1;
    }
    
    strcpy(setValue,"out");
    fwrite(&setValue, sizeof(char), 3, myOutputHandle);
    fclose(myOutputHandle);
   
   
   

    while(1) {
        
        //read adc ch0
        snprintf(buf, sizeof(buf), SYSFS_ADC_DIR);
      fd = open(buf, O_RDONLY);
      read(fd,ch,4);
      close(fd);
      value= mi_atoi(ch, strlen(ch));
      value= (value*100)/2667;
      sleep(0.1);
      
      //read adc ch 6
        snprintf(buf, sizeof(buf), SYSFS_ADC_DIR2);
      fd2 = open(buf, O_RDONLY);
      read(fd2,ch2,4);
      close(fd2);
      value2= mi_atoi(ch2, strlen(ch));
   
      // if the solar power is high and the current from the motor is low, then shut down the fuel cell and turn the solar charger on, otherwise the fuel cell is always on.
      if (value > 80 && value2 < 1000){
          GPIOPin=60;
        
        sprintf(GPIOString, "%d", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
        
        if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
        }
       
        strcpy(setValue, GPIOString);
        fwrite(&setValue, sizeof(char), 2, myOutputHandle);
        fclose(myOutputHandle);
        
        // Set output to low
        if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
            printf("Unable to open value handle\n");
            return 1;
        
        }
        strcpy(setValue, "0"); // Set value low
        fwrite(&setValue, sizeof(char), 1, myOutputHandle);
        fclose(myOutputHandle);
        sleep(0.5); // wait for 1 sec
        
        GPIOPin=50;
        
        sprintf(GPIOString, "%d", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
        
        if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
        }
       
        strcpy(setValue, GPIOString);
        fwrite(&setValue, sizeof(char), 2, myOutputHandle);
        fclose(myOutputHandle);
       
        // Set output to high
        if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
            printf("Unable to open value handle\n");
            return 1;
        }
        
        strcpy(setValue, "1"); // Set value high
        fwrite(&setValue, sizeof(char), 1, myOutputHandle);
        fclose(myOutputHandle);
        sleep(0.5); // wait for 1 sec
    }else{
          GPIOPin=50;
        
        sprintf(GPIOString, "%d", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
        
        if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
        }
       
        strcpy(setValue, GPIOString);
        fwrite(&setValue, sizeof(char), 2, myOutputHandle);
        fclose(myOutputHandle);
        
        // Set output to low
        if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
            printf("Unable to open value handle\n");
            return 1;
        
        }
        strcpy(setValue, "0"); // Set value low
        fwrite(&setValue, sizeof(char), 1, myOutputHandle);
        fclose(myOutputHandle);
        sleep(0.5); // wait for 1 sec
        
        GPIOPin=60;
        
        sprintf(GPIOString, "%d", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%d/value", GPIOPin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%d/direction", GPIOPin);
        
        if ((myOutputHandle = fopen("/sys/class/gpio/export", "ab")) == NULL){
        printf("Unable to export GPIO pin\n");
        return 1;
        }
       
        strcpy(setValue, GPIOString);
        fwrite(&setValue, sizeof(char), 2, myOutputHandle);
        fclose(myOutputHandle);
       
        // Set output to high
        if ((myOutputHandle = fopen(GPIOValue, "rb+")) == NULL){
            printf("Unable to open value handle\n");
            return 1;
        }
        
        strcpy(setValue, "1"); // Set value high
        fwrite(&setValue, sizeof(char), 1, myOutputHandle);
        fclose(myOutputHandle);
        sleep(0.5); // wait for 1 sec
      }
      
                    printf("potencia solar: %d \n",value);
                    printf("corriente del motor %d \n",value2);
           
    }

    return 0;
}
Пример #2
0
int main(int argc, char * argv[]){
  /* argv[0] es el nombre del programa
   * argv[1] es el archivo de input
   * argv[2] es el archivo de output soluciones
   * argv[3] es el archivo de output isoterma (opcional)
   */

  std::ifstream input_file(argv[1]);


  double r_i, r_e;
  unsigned int m_mas_uno, n;
  double iso;
  unsigned int ninst;

  std::vector<std::vector<double> > temperaturas_interiores;
  std::vector<std::vector<double> > temperaturas_exteriores;

  input_file >> r_i >> r_e >> m_mas_uno >> n >> iso >> ninst;

  double aux;
  for(unsigned int i = 0; i<ninst; i++){
    std::vector<double> temp_interior;
    std::vector<double> temp_exterior;
    
    for(unsigned j = 0; j<n; j++){ // la cantidad de temperaturas externas
      input_file >> aux;           // es n, o sea, la cantidad de angulos
      temp_interior.push_back(aux);
    }
    for(unsigned j = 0; j<n; j++){ // lo mismo que arriba
      input_file >> aux;            
      temp_exterior.push_back(aux);
    }

    temperaturas_interiores.push_back(temp_interior);
    temperaturas_exteriores.push_back(temp_exterior);
  }

  input_file.close();


  // ACA YA ESTA TODO LISTO PARA SER USADO

  std::ofstream f_soluciones(argv[2], std::ofstream::out);

  int metodo = mi_atoi(argv[3]);

  std::ofstream f_isotermas;
  if(argc>4){
    f_isotermas.open(argv[4], std::ofstream::out);
  }
  
  Sistema s(r_i, r_e, m_mas_uno, n, temperaturas_interiores, temperaturas_exteriores);
 
	if(metodo == 0) s.solve(f_soluciones, ELIM_GAUSSIANA);
  else if(metodo == 1) s.solve(f_soluciones, FACTORIZACION_LU);
  else if(metodo == 2) s.solve(f_soluciones, ELIM_GAUSSIANA_BANDA);
  else if(metodo == 3) s.solve(f_soluciones, FACTORIZACION_LU_BANDA);
	
  s.isotermas(f_isotermas, iso);

	imprimir_vector(s.tiempos);
  f_soluciones.close();

  return 0;
}