uint16_t accelerometer_read(accel_t accel) { if (accel == ACCEL_X) return analog_read(ANALOG9); if (accel == ACCEL_Y) return analog_read(ANALOG10); if (accel == ACCEL_Z) return analog_read(ANALOG4); return 0; }
int term(){ // valeur doights de commande value_apontador_finger=analog_read(i); value_middle_finger=analog_read(i); // if(value_apontador_finger==255) { } if() { cmd[nb_champ_seq]=1; nb_champ_seq++;} // if(value_middle_finger==255) { } }
/* Read and average the ADC input signal */ static double read_temp(uint8_t sensor_number) { int32_t raw = 4095; // initialize raw with value equal to lowest temperature. double celsius = 0; uint8_t i; if (sensor_number == EXTRUDER_0){ raw = analog_read(EXTRUDER_0_SENSOR_ADC_CHANNEL); }else if (sensor_number == HEATED_BED_0) { raw = analog_read(HEATED_BED_0_SENSOR_ADC_CHANNEL); /* int32_t bed_temp_buf[5]; for(int32_t i = 0; i < 5; i++) { bed_temp_buf[i] = analog_read(HEATED_BED_0_SENSOR_ADC_CHANNEL); } raw = getMedianValue(bed_temp_buf); */ } // filter the ADC values with simple IIR adc_filtered[sensor_number] = ((adc_filtered[sensor_number] * 15) + raw) / 16; raw = adc_filtered[sensor_number]; /* Go and use the temperature table to math the temperature value... */ if (raw < temptable[0][sensor_number]) /* Limit the smaller value... */ { celsius = temptable[0][2]; } else if (raw >= temptable[NUMTEMPS-1][sensor_number]) /* Limit the higher value... */ { celsius = temptable[NUMTEMPS-1][2]; } else { for (i=1; i<NUMTEMPS; i++) { if (raw < temptable[i][sensor_number]) { celsius = temptable[i-1][2] + (raw - temptable[i-1][sensor_number]) * (temptable[i][2] - temptable[i-1][2]) / (temptable[i][sensor_number] - temptable[i-1][sensor_number]); break; } } } return celsius; }
char getLines() { char result = 1; if(analog_read(TLLINE)<THRESHOLD) result *= 2; if(analog_read(TRLINE)<THRESHOLD) result *= 3; if(analog_read(BLLINE)<THRESHOLD) result *= 5; if(analog_read(BRLINE)<THRESHOLD) result *= 7; return result; }
int main() { // set up the 3pi initialize(); int last_proximity = 0; const int base_speed = 200; const int set_point = 100; // what is the set point for? // This is the "main loop" - it will run forever. while(1) { // In case it gets stuck: for 1 second every 15 seconds back up if (get_ms() % 15000 > 14000) { back_up(); continue; } // If something is directly in front turn to the right in place int front_proximity = analog_read(5); if (front_proximity > 200) { turn_in_place(); continue; } int proximity = analog_read(1); // 0 (far away) - 650 (close) int proportional = proximity - set_point; int derivative = proximity - last_proximity; // Proportional-Derivative Control Signal int pd = proportional / 3 + derivative * 20; int left_set = base_speed + pd; int right_set = base_speed - pd; set_motors(left_set, right_set); if (TIME_TO_DISPLAY) { clear(); lcd_goto_xy(0,0); print_long(proximity); lcd_goto_xy(5,0); print_long(pd); lcd_goto_xy(0,1); print_long(left_set); lcd_goto_xy(4,1); print_long(right_set); } last_proximity = proximity; // remember last proximity for derivative } // This part of the code is never reached. while(1) { set_motors(0,0) } }
int main(void) { /*init all the things*/ motors_init(PHASE_CORRECT, PRESCALE_8); uart_init(BAUD_CALC(9600)); sei(); adc_init(); wdt_init(WDTO_500MS); /********************/ go(0, 0); uart_putc(ACK); over_current_a = 0; over_current_b = 0; while (1) { current_a = analog_read(C_SENS_A); current_b = analog_read(C_SENS_B); req_t = get_request_type(); if (req_t == CURRENT_REQ) { write_current(current_a, current_b); } else if (req_t == MOTORS_SET) { read_motors(&speed_a, &speed_b); wdt_reset(); } //manage current sens #ifdef CURRENT_LIMIT if ((current_a < CURRENT_LIMIT) && (current_b < CURRENT_LIMIT)) { go(speed_a, speed_b); over_current_a = over_current_b = 0; } else { //overcurrent go(0, 0); uart_putc('!'); over_current_a = current_a - CURRENT_LIMIT; over_current_b = current_b - CURRENT_LIMIT; } #endif } return (0); }
void meta_ocupada(){ unsigned int dist = 0; unsigned int todas = 0; for (int x = 0; x < 5; x++){ if (x > 0 && x < 4) { delay_ms(40); } dist = analog_read(7); todas += dist; } if (todas >= 1000) { return true; } else { return false; } //return false; }
int potentiometer_read(analogvalue_t *value) { analogvalue_t newvalue; enum { CHANNEL = 0, N_READINGS = 3 }; int raw = 0; for (int i = 0; i < N_READINGS; i++) { analog_read(CHANNEL, &newvalue); raw += newvalue.raw; } newvalue.raw = raw / N_READINGS; if (newvalue.raw > 1020) newvalue.raw = 1020; if ((newvalue.raw > value->prevraw && newvalue.raw - value->prevraw > 3) || (newvalue.raw < value->prevraw && value->prevraw - newvalue.raw > 3)) { if (newvalue.t - value->t > 50) { value->prevraw = value->raw; value->raw = newvalue.raw; value->v = newvalue.raw / 4; value->t = newvalue.t; } } return value->v; }
/* Read and average the ADC input signal */ static uint16_t read_temp(uint8_t sensor_number) { int32_t raw = 0; int16_t celsius = 0; uint8_t i; if (sensor_number == EXTRUDER_0) { raw = analog_read(EXTRUDER_0_SENSOR_ADC_CHANNEL); } else if (sensor_number == HEATED_BED_0) { raw = analog_read(HEATED_BED_0_SENSOR_ADC_CHANNEL); } // filter the ADC values with simple IIR adc_filtered[sensor_number] = ((adc_filtered[sensor_number] * 7) + raw) / 8; raw = adc_filtered[sensor_number]; /* Go and use the temperature table to math the temperature value... */ if (raw > temptable[0][sensor_number]) /* Limit the smaller value... */ { celsius = temptable[0][2]; } else if (raw < temptable[NUMTEMPS-1][sensor_number]) /* Limit the higher value... */ { celsius = temptable[NUMTEMPS-1][2]; } else { for (i=1; i<NUMTEMPS; i++) { if (raw > temptable[i][sensor_number]) { celsius = temptable[i-1][2] + (raw - temptable[i-1][sensor_number]) * (temptable[i][2] - temptable[i-1][2]) / (temptable[i][sensor_number] - temptable[i-1][sensor_number]); break; } } } uart_writestr("\nTemp: "); uart_send_32_Hex(celsius); return celsius; }
void thermistor_read(const temp_sensor_t *sensor, temp_sensor_runtime_t *runtime){ uint8_t j, table_num; uint16_t temp = 0; sensor_thermistor_userdata *userdata = sensor->userdata; //Read current temperature temp = analog_read(userdata->pin); // for thermistors the thermistor table number is in the additional field table_num = userdata->table_num; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point //#ifndef EXTRUDER //if (DEBUG_PID && (debug_flags & DEBUG_PID)) // sersendf_P(PSTR("pin:%d Raw ADC:%d table entry: %d"),userdata->pin,temp,j); //#endif // Linear interpolating temperature value // y = ((x - x₀)y₁ + (x₁-x)y₀ ) / (x₁ - x₀) // y = temp // x = ADC reading // x₀= temptable[j-1][0] // x₁= temptable[j][0] // y₀= temptable[j-1][1] // y₁= temptable[j][1] // y = // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); //#ifndef EXTRUDER //if (DEBUG_PID && (debug_flags & DEBUG_PID)) // sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); //#endif return; } } //Clamp for overflows if (j == NUMTEMPS) temp = temptable[table_num][NUMTEMPS-1][1]; runtime->next_read_time = 0; runtime->last_read_temp = temp; }
uint16_t read_temp(uint8_t sensor_number) { uint16_t raw = 0, celsius = 0; uint8_t i; if (sensor_number == EXTRUDER_0) { raw = analog_read(EXTRUDER_0_SENSOR_ADC_CHANNEL); } else if (sensor_number == HEATED_BED_0) { raw = analog_read(HEATED_BED_0_SENSOR_ADC_CHANNEL); } /* Go and use the temperature table to math the temperature value... */ if (raw < temptable[0][sensor_number]) /* Limit the smaller value... */ { celsius = temptable[0][2]; } else if (raw >= temptable[NUMTEMPS-1][sensor_number]) /* Limit the higher value... */ { celsius = temptable[NUMTEMPS-1][2]; } else { for (i=1; i<NUMTEMPS; i++) { if (raw < temptable[i][sensor_number]) { celsius = temptable[i-1][2] + (raw - temptable[i-1][sensor_number]) * (temptable[i][2] - temptable[i-1][2]) / (temptable[i][sensor_number] - temptable[i-1][sensor_number]); break; } } } return celsius; }
int main(void) { setup(); while(1) { x = analog_read(&PB1); uart1.printf("hex = %05d\r\n", x); x = analog_read_voltage(&PB1); uart1.printf("val = %04dmv\r\n", x); uart1.printf("==============\r\n", x); delay_ms(1000); } }
void main(void) { slave_id(); port_configure(); //ini(); analog_config(); while(true) { analog_read(); // Le posicao corrente if (buffer_adc>250) // Limite mecanico superior { stop_engine(); limit_up=0; limit_down=1; } else if (buffer_adc<10) // Limite mecanico inferior { stop_engine(); limit_down=0; limit_up=1; } else if ((buffer_adc==buffer_pos)) // Para motor em posicao { stop_engine(); } else if ((buffer_adc>buffer_pos) && limit_down!=0) { move_down(); limit_up=1; //liberta limite cima } else if ((buffer_adc<buffer_pos) && limit_up!=0) { move_up(); limit_down=1; //liberta limite baixo } } }
/* Read and average the R2C2 ADC input signal */ static double read_R2C2_temp(void) { double celsius = 0; int32_t adc_r2c2_buf[9]; for(int32_t i = 0; i < 9; i++) { adc_r2c2_buf[i] = analog_read(R2C2_TEMP_SENSOR_ADC_CHANNEL); } adc_r2c2_raw = getMedianValue(adc_r2c2_buf); adc_filtered_r2c2 = adc_filtered_r2c2*0.9 + adc_r2c2_raw*0.1; double volts = (double) adc_filtered_r2c2*(3.3/4096); celsius = (volts - 0.5)*100; return celsius; }
int main() { init_sensors(); int SET_POINT_VALUE = get_int_from_user("SetPnt=?", 20, 5); int END_POINT_VALUE = get_int_from_user("EndPnt=?", 145, 1); wait_with_message("Press B"); count_down(2); int front = 0; int left = 0; int right = 0; int balance = 0; int left_speed = 0; int right_speed = 0; int set_point = 0; while(1) { left = analog_read(6); right = analog_read(5); front = analog_read(7); balance = 0; if (left > 20 || right > 20) { balance = right - left - 20; } if(front < SET_POINT_VALUE) { left_speed = 110 - (1.0 * 0.54838709677419354838709677419355 * front); right_speed = 110 - (1.0 * 0.54838709677419354838709677419355 * front); } else { while(left_speed > 25 || right_speed > 25) { left_speed--; right_speed--; set_motors(left_speed + balance,right_speed - balance); play_frequency(200, 50, 14); } } if (set_point == 0 && front > END_POINT_VALUE) { set_point = 1; set_motors(25,25); } else { set_motors(left_speed + balance,right_speed - balance); } if (set_point == 1 && front < END_POINT_VALUE) { break; } } halt(); clear(); print("f="); print_long(front); // end while(1); }
/// called every 10ms from clock.c - check all temp sensors that are ready for checking void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { if (temp_sensors_runtime[i].next_read_time) { temp_sensors_runtime[i].next_read_time--; } else { uint16_t temp = 0; //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX6675 case TT_MAX6675: #ifdef PRR PRR &= ~MASK(PRSPI); #elif defined PRR0 PRR0 &= ~MASK(PRSPI); #endif #ifdef NAL_REPRAP /* This section is compatible with the mendel original implementation of the MAX6675. * Not using the SPI as the MISO line is used to control our heater. */ WRITE(MAX6675_CS, 0); // Enable device // Read in 16 bits from the MAX6675 for (uint8_t i=0; i<16; i++){ WRITE(MAX6675_SCK,1); // Set Clock to HIGH temp <<= 1; // shift left by one // Read bit and add it to our variable temp += (READ(MAX6675_SO) != 0 ); WRITE(MAX6675_SCK,0); // Set Clock to LOW } WRITE(MAX6675_CS, 1); //Disable Device #else SPCR = MASK(MSTR) | MASK(SPE) | MASK(SPR0); // enable TT_MAX6675 WRITE(SS, 0); // ensure 100ns delay - a bit extra is fine delay(1); // read MSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp = SPDR; temp <<= 8; // read LSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp |= SPDR; // disable TT_MAX6675 WRITE(SS, 1); #endif temp_sensors_runtime[i].temp_flags = 0; if ((temp & 0x8002) == 0) { // got "device id" temp_sensors_runtime[i].temp_flags |= PRESENT; if (temp & 4) { // thermocouple open temp_sensors_runtime[i].temp_flags |= TCOPEN; } else { temp = temp >> 3; } } // this number depends on how frequently temp_sensor_tick is called. the MAX6675 can give a reading every 0.22s, so set this to about 250ms temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_MAX6675 */ #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { uint8_t j, table_num; //Read current temperature temp = analog_read(temp_sensors[i].temp_pin); // check for open circuit if (temp ==1023){ temp = 0 ; // this should convert to max temperature and ensure the heaters are turned off temp_sensors_runtime[i].temp_flags |= TCOPEN; } // for thermistors the thermistor table number is in the additional field table_num = temp_sensors[i].additional; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point #ifndef EXTRUDER if (debug_flags & DEBUG_PID) sersendf_P(PSTR("pin:%d Raw ADC:%d table entry: %d"),temp_sensors[i].temp_pin,temp,j); #endif // Linear interpolating temperature value // y = ((x - x₀)y₁ + (x₁-x)y₀ ) / (x₁ - x₀) // y = temp // x = ADC reading // x₀= temptable[j-1][0] // x₁= temptable[j][0] // y₀= temptable[j-1][1] // y₁= temptable[j][1] // y = // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); #ifndef EXTRUDER if (debug_flags & DEBUG_PID) sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); #endif break; } } #ifndef EXTRUDER if (debug_flags & DEBUG_PID) sersendf_P(PSTR(" Sensor:%d\n"),i); #endif //Clamp for overflows if (j == NUMTEMPS) temp = temptable[table_num][NUMTEMPS-1][1]; temp_sensors_runtime[i].next_read_time = 0; } while (0); break; #endif /* TEMP_THERMISTOR */ #ifdef TEMP_AD595 case TT_AD595: temp = analog_read(temp_sensors[i].temp_pin); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point temp = (temp * 500L) >> 8; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_AD595 */ #ifdef TEMP_PT100 case TT_PT100: #warning TODO: PT100 code break #endif /* TEMP_PT100 */ #ifdef TEMP_INTERCOM case TT_INTERCOM: temp = read_temperature(temp_sensors[i].temp_pin); temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_INTERCOM */ #ifdef TEMP_DUMMY case TT_DUMMY: temp = temp_sensors_runtime[i].last_read_temp; if (temp_sensors_runtime[i].target_temp > temp) temp++; else if (temp_sensors_runtime[i].target_temp < temp) temp--; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_DUMMY */ default: /* prevent compiler warning */ break; } temp_sensors_runtime[i].last_read_temp = temp; if (labs((int16_t)(temp - temp_sensors_runtime[i].target_temp)) < (TEMP_HYSTERESIS*4)) { if (temp_sensors_runtime[i].temp_residency < (TEMP_RESIDENCY_TIME*100)) temp_sensors_runtime[i].temp_residency++; } else { temp_sensors_runtime[i].temp_residency = 0; } if (temp_sensors[i].heater < NUM_HEATERS) { heater_tick(temp_sensors[i].heater, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); } } }
void test_analog() { // test that set/get mode works set_analog_mode(MODE_8_BIT); printf("\nGet8BIT"); assert(MODE_8_BIT == get_analog_mode()); set_analog_mode(MODE_10_BIT); printf("\nGet10BIT"); assert(MODE_10_BIT == get_analog_mode()); // read the trimpot in 10 bit mode and compare it to 8 bit mode int x1 = analog_read(7); set_analog_mode(MODE_8_BIT); delay_ms(1); // required for readings to stabilize int x2 = analog_read(7); printf("\n8BIT10BIT %d %d",x1,x2); assert( abs((x1>>2) - x2) < 10 ); // make sure that the average reading is more stable than individual readings set_analog_mode(MODE_10_BIT); unsigned char i; int min = 1023, max = 0, avg_min = 1023, avg_max = 0; for(i=0;i<10;i++) { int x1 = analog_read(7); int x2 = analog_read_average(7,256); if(x1 > max) max = x1; if(x1 < min) min = x1; if(x2 > avg_max) avg_max = x2; if(x2 < avg_min) avg_min = x2; printf("\nAvgComp %03x %03x", x1, x2); assert( abs(x1-x2) < 10); } printf("\nAB%03x%03x%03x%03x",max,min,avg_max,avg_min); assert( max - min >= avg_max - avg_min); // check that temp C and F return appropriate values in 10bit mode set_analog_mode(MODE_10_BIT); x1 = analog_read_average(6,100); int expect_temp_f = (((int)(analog_read_average_millivolts(TEMP_SENSOR, 20)) * 12) - 634) / 13; int expect_temp_c = (((int)(analog_read_average_millivolts(TEMP_SENSOR, 20) * 20)) - 7982) / 39; int temp_f = read_temperature_f(); int temp_c = read_temperature_c(); printf("\nTF10 %d %d", expect_temp_f, temp_f); assert( expect_temp_f/5 == temp_f/5 ); printf("\nTC10 %d %d", expect_temp_c, temp_c); assert( expect_temp_c/5 == temp_c/5 ); // try temp in 8bit mode set_analog_mode(MODE_8_BIT); delay_ms(1); // required for readings to stabilize? temp_f = read_temperature_f(); temp_c = read_temperature_c(); printf("\nTF8 %d %d", expect_temp_f, temp_f); assert( (expect_temp_f - temp_f) <= 20 ); printf("\nTC8 %d %d", expect_temp_c, temp_c); assert( abs(expect_temp_c - temp_c) <= 20 ); // test background conversion set_analog_mode(MODE_10_BIT); delay_ms(1); // required for readings to stabilize x1 = analog_read_average(6,100); start_analog_conversion(6); while(analog_is_converting()) printf("\nConvert"); x2 = analog_conversion_result(); printf("%d %d", x1, x2); assert( abs(x1 - x2) < 10 ); // make sure to_millivolts works in 8 and 10 bit mode set_analog_mode(MODE_10_BIT); x1 = 5000; x2 = to_millivolts(1023); printf("\nmV1 %d %d",x1,x2); assert( x1 == x2 ); x1 = 2498; x2 = to_millivolts(511); printf("\nmV2 %d %d",x1,x2); assert( x1 == x2 ); x1 = 0; x2 = to_millivolts(0); printf("\nmV3 %d %d",x1,x2); assert( x1 == x2 ); set_analog_mode(MODE_8_BIT); x1 = 5000; x2 = to_millivolts(255); printf("\nmV4 %d %d",x1,x2); assert( x1 == x2 ); x1 = 2490; x2 = to_millivolts(127); printf("\nmV5 %d %d",x1,x2); assert( x1 == x2 ); x1 = 0; x2 = to_millivolts(0); printf("\nmV6 %d %d",x1,x2); assert( x1 == x2 ); }
/// called every 10ms from clock.c - check all temp sensors that are ready for checking void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { if (temp_sensors_runtime[i].next_read_time) { temp_sensors_runtime[i].next_read_time--; } else { uint16_t temp = 0; //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX6675 case TT_MAX6675: #ifdef PRR PRR &= ~MASK(PRSPI); #elif defined PRR0 PRR0 &= ~MASK(PRSPI); #endif SPCR = MASK(MSTR) | MASK(SPE) | MASK(SPR0); // enable TT_MAX6675 WRITE(SS, 0); // No delay required, see // https://github.com/triffid/Teacup_Firmware/issues/22 // read MSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp = SPDR; temp <<= 8; // read LSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp |= SPDR; // disable TT_MAX6675 WRITE(SS, 1); temp_sensors_runtime[i].temp_flags = 0; if ((temp & 0x8002) == 0) { // got "device id" temp_sensors_runtime[i].temp_flags |= PRESENT; if (temp & 4) { // thermocouple open temp_sensors_runtime[i].temp_flags |= TCOPEN; } else { temp = temp >> 3; } } // this number depends on how frequently temp_sensor_tick is called. the MAX6675 can give a reading every 0.22s, so set this to about 250ms temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_MAX6675 */ #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { uint8_t j, table_num; //Read current temperature temp = analog_read(temp_sensors[i].temp_pin); // for thermistors the thermistor table number is in the additional field table_num = temp_sensors[i].additional; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR("pin:%d Raw ADC:%d table entry: %d"),temp_sensors[i].temp_pin,temp,j); #endif // Linear interpolating temperature value // y = ((x - x₀)y₁ + (x₁-x)y₀ ) / (x₁ - x₀) // y = temp // x = ADC reading // x₀= temptable[j-1][0] // x₁= temptable[j][0] // y₀= temptable[j-1][1] // y₁= temptable[j][1] // y = // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); #endif break; } } #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR(" Sensor:%d\n"),i); #endif //Clamp for overflows if (j == NUMTEMPS) temp = temptable[table_num][NUMTEMPS-1][1]; temp_sensors_runtime[i].next_read_time = 0; } while (0); break; #endif /* TEMP_THERMISTOR */ #ifdef TEMP_AD595 case TT_AD595: temp = analog_read(temp_sensors[i].temp_pin); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point temp = (temp * 500L) >> 8; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_AD595 */ #ifdef TEMP_PT100 case TT_PT100: #warning TODO: PT100 code break #endif /* TEMP_PT100 */ #ifdef TEMP_INTERCOM case TT_INTERCOM: temp = read_temperature(temp_sensors[i].temp_pin); temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_INTERCOM */ #ifdef TEMP_NONE case TT_NONE: temp_sensors_runtime[i].last_read_temp = temp_sensors_runtime[i].target_temp; // for get_temp() temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_NONE */ #ifdef TEMP_DUMMY case TT_DUMMY: temp = temp_sensors_runtime[i].last_read_temp; if (temp_sensors_runtime[i].target_temp > temp) temp++; else if (temp_sensors_runtime[i].target_temp < temp) temp--; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_DUMMY */ default: /* prevent compiler warning */ break; } temp_sensors_runtime[i].last_read_temp = temp; } if (labs((int16_t)(temp_sensors_runtime[i].last_read_temp - temp_sensors_runtime[i].target_temp)) < (TEMP_HYSTERESIS*4)) { if (temp_sensors_runtime[i].temp_residency < (TEMP_RESIDENCY_TIME*100)) temp_sensors_runtime[i].temp_residency++; } else { temp_sensors_runtime[i].temp_residency = 0; } if (temp_sensors[i].heater < NUM_HEATERS) { heater_tick(temp_sensors[i].heater, temp_sensors[i].temp_type, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); } }
if (temp & 4) { // thermocouple open temp_flags |= TEMP_FLAG_TCOPEN; } else { current_temp = temp >> 3; return current_temp; } } #endif /* TEMP_MAX6675 */ #ifdef TEMP_THERMISTOR uint8_t i; //Read current temperature temp = analog_read(TEMP_PIN_CHANNEL); //Calculate real temperature based on lookup table for (i = 1; i < NUMTEMPS; i++) { if (pgm_read_word(&(temptable[i][0])) > temp) { // multiply by 4 because internal temp is stored as 14.2 fixed point temp = pgm_read_word(&(temptable[i][1])) + (pgm_read_word(&(temptable[i][0])) - temp) * 4 * (pgm_read_word(&(temptable[i-1][1])) - pgm_read_word(&(temptable[i][1]))) / (pgm_read_word(&(temptable[i][0])) - pgm_read_word(&(temptable[i-1][0]))); break; } } //Clamp for overflows if (i == NUMTEMPS) temp = temptable[NUMTEMPS-1][1]; return temp;
void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { if (temp_sensors_runtime[i].next_read_time) { temp_sensors_runtime[i].next_read_time--; } else { uint16_t temp = 0; //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX6675 case TT_MAX6675: #ifdef PRR PRR &= ~MASK(PRSPI); #elif defined PRR0 PRR0 &= ~MASK(PRSPI); #endif SPCR = MASK(MSTR) | MASK(SPE) | MASK(SPR0); // enable TT_MAX6675 WRITE(SS, 0); // ensure 100ns delay - a bit extra is fine delay(1); // read MSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp = SPDR; temp <<= 8; // read LSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp |= SPDR; // disable TT_MAX6675 WRITE(SS, 1); temp_sensors_runtime[i].temp_flags = 0; if ((temp & 0x8002) == 0) { // got "device id" temp_sensors_runtime[i].temp_flags |= PRESENT; if (temp & 4) { // thermocouple open temp_sensors_runtime[i].temp_flags |= TCOPEN; } else { temp = temp >> 3; } } // this number depends on how frequently temp_sensor_tick is called. the MAX6675 can give a reading every 0.22s, so set this to about 250ms temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_MAX6675 */ #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { uint8_t j; //Read current temperature temp = analog_read(temp_sensors[i].temp_pin); //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { if (pgm_read_word(&(temptable[j][0])) > temp) { // multiply by 4 because internal temp is stored as 14.2 fixed point temp = pgm_read_word(&(temptable[j][1])) * 4 + (temp - pgm_read_word(&(temptable[j-1][0]))) * 4 * (pgm_read_word(&(temptable[j][1])) - pgm_read_word(&(temptable[j-1][1]))) / (pgm_read_word(&(temptable[j][0])) - pgm_read_word(&(temptable[j-1][0]))); break; } } //Clamp for overflows if (j == NUMTEMPS) temp = temptable[NUMTEMPS-1][1] * 4; temp_sensors_runtime[i].next_read_time = 0; } while (0); break; #endif /* TEMP_THERMISTOR */ #ifdef TEMP_AD595 case TT_AD595: temp = analog_read(temp_pin); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point temp = (temp * 500L) >> 8; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_AD595 */ #ifdef TEMP_PT100 case TT_PT100: #warning TODO: PT100 code break #endif /* TEMP_PT100 */ #ifdef TEMP_INTERCOM case TT_INTERCOM: temp = get_read_cmd() << 2; start_send(); temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_INTERCOM */ #ifdef TEMP_DUMMY case TT_DUMMY: temp = temp_sensors_runtime[i].last_read_temp; if (temp_sensors_runtime[i].target_temp > temp) temp++; else if (temp_sensors_runtime[i].target_temp < temp) temp--; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_DUMMY */ } temp_sensors_runtime[i].last_read_temp = temp; if (labs(temp - temp_sensors_runtime[i].target_temp) < TEMP_HYSTERESIS) { if (temp_sensors_runtime[i].temp_residency < TEMP_RESIDENCY_TIME) temp_sensors_runtime[i].temp_residency++; } else { temp_sensors_runtime[i].temp_residency = 0; } if (temp_sensors[i].heater_index < NUM_HEATERS) { heater_tick(temp_sensors[i].heater_index, i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); } } }
int main() { // wait wait_with_message("Press B"); // init and calibrate light sensors unsigned int sensors[5]; pololu_3pi_init_disable_emitter_pin(2000); // (2000 for the timeout corresponds to 2000*0.4 us = 0.8 ms on our 20 MHz processor) wait_for_button_release(BUTTON_B); delay_ms(1000); for(int counter=0;counter<80;counter++) { if(counter < 20 || counter >= 60) set_motors(40,-40); else set_motors(-40,40); calibrate_line_sensors(IR_EMITTERS_ON); delay_ms(20); } set_motors(0,0); // init IR sensors set_analog_mode(MODE_8_BIT); DDRC &= ~(1<< PORTC5); PORTC &= ~(1<< PORTC5); // wait wait_with_message("Press B"); delay_ms(1000); int left_speed = 110; int right_speed = 110; int set_point = 0; while(1) { // check light sensors for our boundary unsigned int position = read_line(sensors,IR_EMITTERS_ON); if(position > 5 && position < 1000) { turn_right(55,65); } else if(position > 1000 && position < 1800) { turn_right(55,95); } else if(position > 1800 && position < 3000) { turn_left(55,95); } else if(position > 3000 && position < 3995) { turn_left(55,65); } // check IR sensors int left = analog_read(6); int right = analog_read(5); int front = analog_read(7); /*if((get_ms() % 300) == 0) { clear(); lcd_goto_xy(0,0); print_long(left); lcd_goto_xy(0,1); print_long(right); }*/ int balance = 0; if (left > 20 || right > 20) { balance = right - left - 20; } if (set_point == 0 && front > 162) { set_point = 1; set_motors(25,25); } else { set_motors(left_speed + balance,right_speed - balance); } } halt(); clear(); //print("f="); //print_long(front); // end while(1); }
/// called every 10ms from clock.c - check all temp sensors that are ready for checking void temp_sensor_tick() { temp_sensor_t i = 0; for (; i < NUM_TEMP_SENSORS; i++) { if (temp_sensors_runtime[i].next_read_time) { temp_sensors_runtime[i].next_read_time--; } else { uint16_t temp = 0; #ifdef TEMP_MAX31855 uint32_t value = 0; struct max31855_plat_data *max31855; #endif //time to deal with this temp sensor switch(temp_sensors[i].temp_type) { #ifdef TEMP_MAX31855 case TT_MAX31855: #ifdef __arm__ max31855 = (struct max31855_plat_data*)temp_sensors[i].plat_data; spiAcquireBus(max31855->bus); spiStart(max31855->bus, max31855->config); spiSelect(max31855->bus); spiReceive(max31855->bus, 4, &value); spiUnselect(max31855->bus); spiReleaseBus(max31855->bus); value = SWAP_UINT32(value); temp_sensors_runtime[i].temp_flags = 0; if (value & (1 << 16)) { #ifdef HALT_ON_TERMOCOUPLE_FAILURE emergency_stop(); #endif } else { temp = value >> 18; if (temp & (1 << 13)) temp = 0; } #else temp = 0; #endif break; #endif #ifdef TEMP_MAX6675 case TT_MAX6675: #ifdef PRR PRR &= ~MASK(PRSPI); #elif defined PRR0 PRR0 &= ~MASK(PRSPI); #endif SPCR = MASK(MSTR) | MASK(SPE) | MASK(SPR0); // enable TT_MAX6675 WRITE(SS, 0); // No delay required, see // https://github.com/triffid/Teacup_Firmware/issues/22 // read MSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp = SPDR; temp <<= 8; // read LSB SPDR = 0; for (;(SPSR & MASK(SPIF)) == 0;); temp |= SPDR; // disable TT_MAX6675 WRITE(SS, 1); temp_sensors_runtime[i].temp_flags = 0; if ((temp & 0x8002) == 0) { // got "device id" temp_sensors_runtime[i].temp_flags |= PRESENT; if (temp & 4) { // thermocouple open temp_sensors_runtime[i].temp_flags |= TCOPEN; } else { temp = temp >> 3; } } // this number depends on how frequently temp_sensor_tick is called. the MAX6675 can give a reading every 0.22s, so set this to about 250ms temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_MAX6675 */ #ifdef TEMP_THERMISTOR case TT_THERMISTOR: do { uint8_t j, table_num; //Read current temperature temp = analog_read(i); // for thermistors the thermistor table number is in the additional field table_num = temp_sensors[i].additional; //Calculate real temperature based on lookup table for (j = 1; j < NUMTEMPS; j++) { if (pgm_read_word(&(temptable[table_num][j][0])) > temp) { // Thermistor table is already in 14.2 fixed point #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR("pin:%d Raw ADC:%d table entry: %d"),temp_sensors[i].temp_pin,temp,j); #endif // Linear interpolating temperature value // y = ((x - x₀)y₁ + (x₁-x)y₀ ) / (x₁ - x₀) // y = temp // x = ADC reading // x₀= temptable[j-1][0] // x₁= temptable[j][0] // y₀= temptable[j-1][1] // y₁= temptable[j][1] // y = // Wikipedia's example linear interpolation formula. temp = ( // ((x - x₀)y₁ ((uint32_t)temp - pgm_read_word(&(temptable[table_num][j-1][0]))) * pgm_read_word(&(temptable[table_num][j][1])) // + + // (x₁-x) (pgm_read_word(&(temptable[table_num][j][0])) - (uint32_t)temp) // y₀ ) * pgm_read_word(&(temptable[table_num][j-1][1]))) // / / // (x₁ - x₀) (pgm_read_word(&(temptable[table_num][j][0])) - pgm_read_word(&(temptable[table_num][j-1][0]))); #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR(" temp:%d.%d"),temp/4,(temp%4)*25); #endif break; } } #ifndef EXTRUDER if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR(" Sensor:%d\n"),i); #endif //Clamp for overflows if (j == NUMTEMPS) temp = temptable[table_num][NUMTEMPS-1][1]; temp_sensors_runtime[i].next_read_time = 0; } while (0); break; #endif /* TEMP_THERMISTOR */ #ifdef TEMP_AD595 case TT_AD595: temp = analog_read(i); // convert // >>8 instead of >>10 because internal temp is stored as 14.2 fixed point temp = (temp * 500L) >> 8; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_AD595 */ #ifdef TEMP_PT100 case TT_PT100: #warning TODO: PT100 code break #endif /* TEMP_PT100 */ #ifdef TEMP_INTERCOM case TT_INTERCOM: temp = read_temperature(temp_sensors[i].temp_pin); temp_sensors_runtime[i].next_read_time = 25; break; #endif /* TEMP_INTERCOM */ #ifdef TEMP_DUMMY case TT_DUMMY: temp = temp_sensors_runtime[i].last_read_temp; if (temp_sensors_runtime[i].target_temp > temp) temp++; else if (temp_sensors_runtime[i].target_temp < temp) temp--; temp_sensors_runtime[i].next_read_time = 0; break; #endif /* TEMP_DUMMY */ default: /* prevent compiler warning */ break; } /* Exponentially Weighted Moving Average alpha constant for smoothing noisy sensors. Instrument Engineer's Handbook, 4th ed, Vol 2 p126 says values of 0.05 to 0.1 for TEMP_EWMA are typical. */ #ifndef TEMP_EWMA #define TEMP_EWMA 1.0 #endif #define EWMA_SCALE 1024L #define EWMA_ALPHA ((long) (TEMP_EWMA * EWMA_SCALE)) temp_sensors_runtime[i].last_read_temp = (uint16_t) ((EWMA_ALPHA * temp + (EWMA_SCALE-EWMA_ALPHA) * temp_sensors_runtime[i].last_read_temp ) / EWMA_SCALE); } if (labs((int16_t)(temp_sensors_runtime[i].last_read_temp - temp_sensors_runtime[i].target_temp)) < (TEMP_HYSTERESIS*4)) { if (temp_sensors_runtime[i].temp_residency < (TEMP_RESIDENCY_TIME*120)) temp_sensors_runtime[i].temp_residency++; } else { // Deal with flakey sensors which occasionally report a wrong value // by setting residency back, but not entirely to zero. if (temp_sensors_runtime[i].temp_residency > 10) temp_sensors_runtime[i].temp_residency -= 10; else temp_sensors_runtime[i].temp_residency = 0; } if (temp_sensors[i].heater < NUM_HEATERS) { heater_tick(temp_sensors[i].heater, temp_sensors[i].temp_type, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].target_temp); } if (DEBUG_PID && (debug_flags & DEBUG_PID)) sersendf_P(PSTR("DU temp: {%d %d %d.%d}"), i, temp_sensors_runtime[i].last_read_temp, temp_sensors_runtime[i].last_read_temp / 4, (temp_sensors_runtime[i].last_read_temp & 0x03) * 25); }
int umain() { uint16_t i,n = 36; uint16_t port=23; uint16_t km,kc; uint16_t xd[36]; uint16_t yd[36]; //happylib_init(); // start printf("\nIRDistCal Press Go"); go_click(); // get number of samples to read while (!go_press()) { printf("\nUse frob to # ofsamples: %2d",n); switch (frob_read_range(0,2)) { case 0: n= 9; break; case 1: n=18; break; case 2: n=36; break; } pause (40); } // wait for go release while (go_press()); // fill distance array for (i=0;i<n;i++) { xd[i] = 10 + 2*i*(36/n); } // get port number while (!go_press()) { port = frob_read_range(8,23); printf("\nUse frob to select port: %2d",port); pause (40); } // wait for go release while (go_press()); // read samples for (i=0;i<n;i++) { while (!go_press()) { yd[i] = analog_read(port); printf("\nSample @ %2dcm =%4d",xd[i],yd[i]); } // wait for go release while (go_press()); } // calculate & print irdist_fit(xd,yd,n,&km,&kc); printf("\nOK: M: %5d C: %d, press Go",km,kc); go_click(); // save /* Disabled until confdb is working printf("\nGo to Save calibStop to quit"); while (1) { if (go_press()) { go_click(); confdb_save_integer(CONF_HLIB_IRDIST_M,km); confdb_save_integer(CONF_HLIB_IRDIST_C,kc); break; } if (stop_press()) { stop_press(); break; } } */ printf("\ncalibration done"); // do nothing forever while (1); return 0; }
// Returns the distance in centimeters from a pin. unsigned int long_range_distance(unsigned char pin) { unsigned int distVoltage = analog_read(pin); unsigned int distValue = convertToDistance(distVoltage); }
void get_inputs(uint16_t *events) { uint32_t vcc = analog_vcc(); uint32_t brake_value = analog_read(&vcc, 3); uint32_t pedal1_value = analog_read(&vcc, 5); uint32_t pedal2_value = analog_read(&vcc, 6); //uint32_t controller_throttle1 = analog_read(&vcc, 4); //uint32_t controller_throttle2 = analog_read(&vcc, 5); int pedal2_in_pedal1 = ((long)pedal2_value * PEDAL_SLOPE_NOMIN) / PEDAL_SLOPE_DENOM + PEDAL_INTERCEPT; int pedal_difference = (int)pedal1_value - (int)pedal2_in_pedal1; if(pedal_difference < 0) pedal_difference = -pedal_difference; /* int throttle1_transform = THRTTL_SLOPE * controller_throttle1 + THRTTL_INTERCEPT; int throttle2_transform = THRTTL_SLOPE * controller_throttle2 + THRTTL_INTERCEPT; */ // HV_UP if(!(PINA&(1<<1))) *events |= (1<<HV_UP); // DRIVE_UP if(!(PINA&(1<<2)) && PIND&(1<<5) && PIND&(1<<6) && brake_value > BRAKE_FIFTEEN_PERCENT) *events |= (1<<DRIVE_UP); // NEUTRAL_UP if(!(PINA&(1<<3))) *events |= (1<<NEUTRAL_UP); // PRECHARGE_DONE if(precharging_done) *events |= (1<<PRECHARGE_DONE); // CHARGE_UP if(PIND&(1<<4)) *events |= (1<<CHARGE_UP); // CHARGE_DOWN if(!(PIND&(1<<4))) *events |= (1<<CHARGE_DOWN); // SOFT_FAULT_SIG if(pedal_difference > PEDAL1_TEN_PERCENT) *events |= (1<<SOFT_FAULT_SIG); // Pedal sensor comparison if(pedal1_value < PEDAL1_DROPOUT || pedal2_value < PEDAL2_DROPOUT) *events |= (1<<SOFT_FAULT_SIG); // Pedal sensor disconnect if(brake_value > BRAKE_MIN_MARGIN && pedal1_value > PEDAL1_TWENTYFIVE_PERCENT) *events |= (1<<SOFT_FAULT_SIG); // Pedal and brake /* if(pedal1_value < controller_throttle1 || pedal1_value > controller_throttle2) *events |= (1<<SOFT_FAULT_SIG); // Pedal vs controller's throttle */ // SOFT_FAULT_REMEDIED if(pedal_difference <= PEDAL1_TEN_PERCENT && pedal1_value < PEDAL_MIN_MARGIN && pedal1_value >= PEDAL1_DROPOUT && pedal2_value >= PEDAL2_DROPOUT) *events |= (1<<SOFT_FAULT_REMEDIED); // HARD_FAULT_SIG uint8_t portc_triggers = !(PINC&(1<<0) && PINC&(1<<1) && PINC&(1<<2) && PINC&(1<<3)); uint8_t portd_triggers = !(PIND&(1<<7)); uint8_t porte_triggers = !(PINE&(1<<0) && PINE&(1<<1) && PINE&(1<<6) && PINE&(1<<7)); if(portc_triggers || portd_triggers || porte_triggers) *events |= (1<<HARD_FAULT_SIG); // GLV Voltage Low /* if(!(PINF&(1<<0))) glv_sys_voltage_low = 1; else glv_sys_voltage_low = 0; */ } // get_inputs()