Skip to content

Commit 0a5cd6f

Browse files
authored
Merge pull request #66 from openUC2/mergemaster
Mergemaster
2 parents cd0f648 + 9ad8682 commit 0a5cd6f

3 files changed

Lines changed: 170 additions & 1 deletion

File tree

ESP32/AS5311-PWM/AS5311-PWM.ino

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "Arduino.h"
2+
#include "driver/mcpwm.h"
3+
4+
5+
#include "Arduino.h"
6+
#include "driver/mcpwm.h"
7+
#include "driver/gpio.h"
8+
9+
#define GPIO_CAP0_IN GPIO_NUM_26
10+
#define GPIO_CAP1_IN GPIO_NUM_27
11+
#define GPIO_TRIGGER GPIO_NUM_14 // New GPIO pin for triggering the counter
12+
13+
14+
struct CaptureData {
15+
uint32_t time_rising_edge;
16+
uint32_t time_falling_edge;
17+
uint32_t period;
18+
bool last_was_rising;
19+
};
20+
21+
CaptureData cap_data[2];
22+
volatile float latest_duty_cycle[2] = {0.0f, 0.0f}; // Updated by the MCPWM capture ISR
23+
volatile int counter = 0;
24+
25+
void IRAM_ATTR capture_handler(void *arg) {
26+
uint32_t cap_unit = (uint32_t)arg;
27+
uint32_t curr_time = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit);
28+
29+
if (mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit) == MCPWM_POS_EDGE) {
30+
if (cap_data[cap_unit].last_was_rising) {
31+
cap_data[cap_unit].period = curr_time - cap_data[cap_unit].time_rising_edge;
32+
}
33+
cap_data[cap_unit].time_rising_edge = curr_time;
34+
cap_data[cap_unit].last_was_rising = true;
35+
} else {
36+
cap_data[cap_unit].time_falling_edge = curr_time;
37+
cap_data[cap_unit].last_was_rising = false;
38+
}
39+
40+
if (cap_data[cap_unit].period != 0) {
41+
uint32_t high_time = cap_data[cap_unit].time_falling_edge - cap_data[cap_unit].time_rising_edge;
42+
latest_duty_cycle[cap_unit] = ((float)high_time / (float)cap_data[cap_unit].period) * 100.0f;
43+
Serial.printf("CAP%d Duty cycle: %0.2f%%\n", cap_unit, latest_duty_cycle[cap_unit]);
44+
}
45+
}
46+
47+
void IRAM_ATTR trigger_handler(void* arg) {
48+
for (int i = 0; i < 2; i++) {
49+
if (latest_duty_cycle[i] > 50.0f) {
50+
counter++;
51+
} else {
52+
counter--;
53+
}
54+
}
55+
Serial.printf("Counter (triggered): %d\n", counter);
56+
}
57+
58+
void setup() {
59+
Serial.begin(115200);
60+
Serial.println("Testing MCPWM capture on two pins...");
61+
62+
// Initialize MCPWM capture
63+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN);
64+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_1, GPIO_CAP1_IN);
65+
66+
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_BOTH_EDGE, 0);
67+
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP1, MCPWM_BOTH_EDGE, 0);
68+
69+
intr_handle_t cap0_intr_handle = NULL;
70+
intr_handle_t cap1_intr_handle = NULL;
71+
72+
mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP0, ESP_INTR_FLAG_IRAM, &cap0_intr_handle);
73+
mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP1, ESP_INTR_FLAG_IRAM, &cap1_intr_handle);
74+
75+
// Initialize the GPIO_TRIGGER pin for external interrupt
76+
gpio_config_t io_conf;
77+
io_conf.intr_type = GPIO_INTR_POSEDGE; // Trigger on rising edge
78+
io_conf.mode = GPIO_MODE_INPUT;
79+
io_conf.pin_bit_mask = (1ULL << GPIO_TRIGGER);
80+
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
81+
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
82+
gpio_config(&io_conf);
83+
84+
// Register ISR for the trigger pin
85+
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
86+
gpio_isr_handler_add(GPIO_TRIGGER, trigger_handler, NULL);
87+
88+
// Clear capture data
89+
memset(cap_data, 0, sizeof(cap_data));
90+
}
91+
92+
void loop() {
93+
delay(1000);
94+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <OneWire.h>
2+
#include <DallasTemperature.h>
3+
#include <Arduino.h>
4+
5+
// GPIO where the DS18B20 is connected to
6+
const int oneWireBus = GPIO_NUM_25;
7+
8+
9+
// Setup a oneWire instance to communicate with any OneWire devices
10+
OneWire oneWire(oneWireBus);
11+
12+
// Pass our oneWire reference to Dallas Temperature sensor
13+
DallasTemperature sensors(&oneWire);
14+
15+
// PID constants
16+
const float Kp = 2.0;
17+
const float Ki = 0.5;
18+
const float Kd = 1.0;
19+
20+
// PID variables
21+
float previousError = 0;
22+
float integral = 0;
23+
24+
// Desired temperature
25+
const float setpoint = 36.0; // for example, 50°C
26+
27+
// LEDC variables
28+
const int freq = 5000;
29+
const int ledChannel = 0;
30+
const int resolution = 8;
31+
const int heaterPin = GPIO_NUM_12; // PWM pin connected to the heater
32+
33+
// Get the current temperature
34+
// Here you'll want to replace this with your actual temperature reading logic
35+
float getTemperature() {
36+
// Dummy implementation, replace with your sensor's logic
37+
sensors.requestTemperatures();
38+
return sensors.getTempCByIndex(0);
39+
}
40+
41+
void setup() {
42+
Serial.begin(115200);
43+
sensors.begin();
44+
// Setup LEDC for heater control
45+
ledcSetup(ledChannel, freq, resolution);
46+
ledcAttachPin(heaterPin, ledChannel);
47+
}
48+
49+
void loop() {
50+
51+
float currentTemperature = getTemperature();
52+
float error = setpoint - currentTemperature;
53+
54+
integral += error;
55+
float derivative = error - previousError;
56+
57+
// Calculate PID output
58+
float output = Kp * error + Ki * integral + Kd * derivative;
59+
60+
// Ensure output is within the range [0, 255] for the 8-bit PWM resolution
61+
output = constrain(output, 0, 255);
62+
63+
// Set the PWM signal
64+
ledcWrite(ledChannel, (int)output);
65+
66+
// Update the previous error
67+
previousError = error;
68+
69+
// Print values for debugging (optional)
70+
Serial.print("Current Temperature: "); Serial.print(currentTemperature);
71+
Serial.print(" °C | PWM Output: "); Serial.println(output);
72+
73+
delay(1000);
74+
}

uc2rest/mserial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def __init__(self, port, baudrate, timeout=1):
355355
self.manufacturer = "UC2Mock"
356356
self.BAUDRATES = -1
357357

358-
def isOpen(Self):
358+
def isOpen(self):
359359
return self.is_open
360360

361361
def open(self):
@@ -371,6 +371,7 @@ def readline(self, timeout=1):
371371
return b''
372372
data = self.data_buffer
373373
self.data_buffer = self.data_buffer
374+
time.sleep(.05)
374375
return bytes(data)
375376

376377
def read(self, num_bytes):

0 commit comments

Comments
 (0)