summaryrefslogtreecommitdiff
path: root/pcr-controller/pcr-controller.c
blob: ac064e09d4da1527d0584b17285548b2574cda6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  OpenPCR Teensy Controller Code
 *
 *
 *  Copyright (C) 2013 Bernhard Tittelbach <xro@realraum.at>
*   uses avr-utils, anyio & co by Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of spreadspace avr utils.
 *
 *  spreadspace avr utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  spreadspace avr utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
 */


#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <stdio.h>
#include <stdlib.h>

#include "util.h"
#include "led.h"
#include "anyio.h"

#include "onewire.h"
#include "ds1820.h"

#include "pwm.h"
#include "pid_control.h"

#define PIN_HIGH(PORT, PIN) PORT |= (1 << PIN)
#define PIN_LOW(PORT, PIN) PORT &= ~(1 << PIN)
#define PINMODE_OUTPUT PIN_HIGH  //just use DDR instead of PORT
#define PINMODE_INPUT PIN_LOW  //just use DDR instead of PORT

#define OP_SETBIT |=
#define OP_CLEARBIT &= ~
#define OP_CHECK &
#define PIN_SW(PORTDDRREG, PIN, OP) PORTDDRREG OP (1 << PIN)

#define HIGHv OP_SETBIT
#define LOWv OP_CLEARBIT

#define PUMP_PIN PINB3
#define PELTIER_INA  PINF7
#define PELTIER_INB  PINB6
#define PELETIER_PWM_EN PINB5
#define TOPHEAT_PIN PIND7

uint8_t num_temp_sensors_ = 0;
uint16_t raw_temp_ = 0;

void queryAndSaveTemperature(uint8_t bit_resolution)
{
    uint8_t sensor_index = 0;

    if (num_temp_sensors_ == 0)
    {
        num_temp_sensors_ = ds1820_discover();
    }

    for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++)
    {
        ds1820_set_resolution(sensor_index, bit_resolution);
        ds1820_start_measuring(sensor_index);
    }

    ds1820_wait_conversion_time(bit_resolution);

    for (sensor_index=0; sensor_index < num_temp_sensors_; sensor_index++)
    {
        raw_temp_ = ds1820_read_temperature(sensor_index);
        if (raw_temp_ != DS1820_ERROR)
        {
            break; //we need only one successfully read value
        }
    }
}

void printRawTemp(int16_t raw_temp)
{
  printf("%d.%02d", raw_temp / 16, 100 * (raw_temp % 16) / 16);
}

void printTemperature(void)
{
  if (num_temp_sensors_ == 0)
  {
    printf("ERROR: No DS1820 sensors on 1wire bus, thus no temperature\r\n");
    return;
  }
  if (raw_temp_ == DS1820_ERROR)
  {
      printf("ERROR talking to DS18b20, no valid temperature!\r\n");
  } else {
      printf("Temp: ");
      printRawTemp(raw_temp_);
      printf("\r\n");
  }
}

void readIntoBuffer(char *buffer, uint8_t buflen)
{
  while (anyio_bytes_received() == 0);
  int ReceivedByte=0;
  do {
    ReceivedByte = fgetc(stdin);
    if (ReceivedByte != EOF)
    {
      *buffer = (char) ReceivedByte;
      buffer++;
      buflen --;
    }
  } while (ReceivedByte != '\n' && ReceivedByte != '\r' && buflen > 1);
  *buffer = 0;
}

int16_t readNumber(void)
{
  char buffer[20];
  readIntoBuffer(buffer, 20);
  return atoi(buffer);
}

void setPeltierCoolingDirectionPower(int16_t value)
{
  if (value > 255)
    value = 255;
  if (value < -255)
    value = -255;
  
  if (value >= 0)
  {
    PIN_HIGH(PORTF, PELTIER_INA);
    PIN_LOW(PORTB, PELTIER_INB);
    pwm_set((uint8_t) value);
  } else {
    PIN_LOW(PORTF, PELTIER_INA);
    PIN_HIGH(PORTB, PELTIER_INB);
    pwm_set((uint8_t) (-1 * value)); 
  }
}

void handle_cmd(uint8_t cmd)
{
  switch(cmd) {
  case ' ':
  case '\n':
  case '\r':
    return;
  case 'R':
  case 'r': reset2bootloader(); break;
  case 's': printTemperature(); return;
  case 'L': led_toggle(); break;
  case 't':
    printf("TargetTemp: ");
    printRawTemp(pid_getTargetValue());
    printf("\r\n");
    return;
  case 'p': 
  case 'i':
  case 'd':
    pid_printVars();
    return;
  case 'T': pid_setTargetValue(readNumber()); break;
  case 'P': pid_setP(readNumber()); break;
  case 'I': pid_setI(readNumber()); break;
  case 'D': pid_setD(readNumber()); break;
  case 'A': PIN_HIGH(PORTB, PUMP_PIN); break;
  case 'a': PIN_LOW(PORTB, PUMP_PIN); break;  
  case 'B': PIN_HIGH(PORTD, TOPHEAT_PIN); break;
  case 'b': PIN_LOW(PORTD, TOPHEAT_PIN); break;  
  default: printf("ERROR\r\n"); return;
  }
  printf("OK\r\n");
}

int main(void)
{
  /* Disable watchdog if enabled by bootloader/fuses */
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  cpu_init();
  led_init();
  anyio_init(115200, 0);
  sei();

  led_off();
  owi_init(PINC7, &PINC);
  PINMODE_OUTPUT(DDRB, PUMP_PIN);
  PIN_LOW(PORTB, PUMP_PIN);
  PINMODE_OUTPUT(DDRB, PELETIER_PWM_EN);
  PINMODE_OUTPUT(DDRB, PELTIER_INB);
  PINMODE_OUTPUT(DDRF, PELTIER_INA);
  PINMODE_OUTPUT(DDRD, TOPHEAT_PIN);
  
  pwm_init();
  
  pid_loadFromEEPROM();

  num_temp_sensors_ = ds1820_discover();
  
  for(;;) 
  {
    int16_t BytesReceived = anyio_bytes_received();
    while(BytesReceived > 0)
    {
      int ReceivedByte = fgetc(stdin);
      if (ReceivedByte != EOF)
      {
        handle_cmd(ReceivedByte);
      }
      BytesReceived--;
    }
    
    queryAndSaveTemperature(11);
    
    // PID control
    // FIXME: if we do USB Input / Output (input especially) we delay PID controll too mauch
    //              that's bad, since the routing requires that it be called at exact intervalls
    //              maybe we should use a interrupt routine

    setPeltierCoolingDirectionPower(pid_calc(raw_temp_));
    
    anyio_task();
  }
}