<commit_before>#include "DualEncoderDriver.h"

#include "constants.h"

#include <Arduino.h>

static constexpr float DISTANCE = MICROS_PER_SECOND * WHEEL_PERIMETER / IMPULSES_PER_ROUND;

Encoder::Encoder() :
	last_micros(0), impulse_counter(0),
	direction(0), speed(0), acceleration(0) {}

void Encoder::update(int8_t b)
{
	direction = b;
	++impulse_counter;
}

void Encoder::update()
{
	uint32_t current_micros = micros();

	float last_speed = speed;
	speed = DISTANCE * impulse_counter / (current_micros - last_micros);
	if(direction == 0) speed = -speed;

	acceleration = (speed - last_speed) / (current_micros - last_micros);

	last_micros = current_micros;
	impulse_counter = 0;
}

Encoder leftEncoder, rightEncoder;

void leftHandler()
{
	int b = digitalRead(LEFT_B_PIN);
	leftEncoder.update(b);
}

void rightHandler()
{
	int b = digitalRead(RIGHT_B_PIN);
	rightEncoder.update(b);
}

void initDualEncoders()
{
	pinMode(LEFT_A_PIN, INPUT);
	pinMode(LEFT_B_PIN, INPUT);
	pinMode(RIGHT_B_PIN, INPUT);
	pinMode(RIGHT_B_PIN, INPUT);

	attachInterrupt(digitalPinToInterrupt(LEFT_A_PIN), leftHandler, RISING);
	attachInterrupt(digitalPinToInterrupt(RIGHT_A_PIN), rightHandler, RISING);
}<commit_msg>Bug fix<commit_after>#include "DualEncoderDriver.h"

#include "constants.h"

#include <Arduino.h>

static constexpr float DISTANCE = MICROS_PER_SECOND * WHEEL_PERIMETER / IMPULSES_PER_ROUND;

Encoder::Encoder() :
	last_micros(0), impulse_counter(0),
	direction(0), speed(0), acceleration(0) {}

void Encoder::update(int8_t b)
{
	direction = b;
	++impulse_counter;
}

void Encoder::update()
{
	uint32_t current_micros = micros();

	float last_speed = speed;
	speed = DISTANCE * impulse_counter / (current_micros - last_micros);
	if(direction == 0) speed = -speed;

	acceleration = (speed - last_speed) / (current_micros - last_micros);

	last_micros = current_micros;
	impulse_counter = 0;
}

Encoder leftEncoder, rightEncoder;

void leftHandler()
{
	int8_t b = digitalRead(LEFT_B_PIN);
	leftEncoder.update(b);
}

void rightHandler()
{
	int8_t b = digitalRead(RIGHT_B_PIN);
	rightEncoder.update(b);
}

void initDualEncoders()
{
	pinMode(LEFT_A_PIN, INPUT);
	pinMode(LEFT_B_PIN, INPUT);
	pinMode(RIGHT_A_PIN, INPUT);
	pinMode(RIGHT_B_PIN, INPUT);

	attachInterrupt(digitalPinToInterrupt(LEFT_A_PIN), leftHandler, RISING);
	attachInterrupt(digitalPinToInterrupt(RIGHT_A_PIN), rightHandler, RISING);
}
{'fraction_non_alphanumeric': 0.12531328320802004, 'fraction_numerical': 0.009189640768588136, 'mean_word_length': 6.437888198757764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119220', 'n_tokens_mistral': 943, 'n_tokens_neox': 908, 'n_words': 165}