Skip to content

Instantly share code, notes, and snippets.

@kvalv
Created April 26, 2016 15:38
Show Gist options
  • Select an option

  • Save kvalv/8762a8eef812981e9a78b4c6367c4442 to your computer and use it in GitHub Desktop.

Select an option

Save kvalv/8762a8eef812981e9a78b4c6367c4442 to your computer and use it in GitHub Desktop.
/*
* steeringEncoder.c
*
* Created: 02.02.2016 22:40:47
* Author: Daniel
*/
#include "steeringEncoder.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include "../../Data_transmission/data_transmission.h"
#include "../../ADC_drivers/can_messages.h"
#define ENCODER_A_PIN 5
#define ENCODER_B_PIN 0
#define ENCODER_Z_PIN 2
#define ENCODER_A_PORT PORTB
#define ENCODER_B_PORT PORTC
#define ENCODER_Z_PORT PORTB
#define ENCODER_A_INPUT PINB
#define ENCODER_B_INPUT PINC
#define ENCODER_Z_INPUT PINB
#define ENCODER_A (ENCODER_A_INPUT & (1 << ENCODER_A_PIN))
#define ENCODER_B (ENCODER_B_INPUT & (1 << ENCODER_B_PIN))
static uint8_t rot_state = 0;
static uint8_t prev_rot_state = 0;
static int16_t encoder_counter = 10;
static int16_t counter = 0;
/*
void handleSteeringPosition() {
if (internal_counter[STEERING_RATE_INDEX] >= (20000/100)) {
steering_pos_and_rate.can_data.i16[0] = getEncoderValue();
if (can_send_message(&steering_pos_and_rate) == CAN_STATUS_COMPLETED)
{
cli();
internal_counter[STEERING_RATE_INDEX] = 0;
sei();
}
}
}*/
void encoder_init(){
DDRB &= ~((1<<PORTB5)| (1<< PORTB2)); //Bitwise OR, set A, B, Z pins as input
DDRC &= ~(1<<PORTC0);
PORTB &= ~((1<<PORTB5)| (1<< PORTB2)); // disable pull ups and make it tristate.
PORTC &= ~(1<< PORTC0);
//Interupt pin init:
EICRA |= (1 << ISC20);
EIMSK |= (1 << INT2); //Read interupt on INT2
EIFR |= (1 << INTF2); // External Interrupt Flag Register
//Interupt pin init:
EICRA |= (1 << ISC30);
EIMSK |= (1 << INT3); //Read interupt on INT3
EIFR |= (1 << INTF3); // External Interrupt Flag Register
}
bool xor(uint8_t a, uint8_t b){
return (a || b) && (!(a && b));
}
ISR(INT2_vect){ //Pin A changed
cli();
if (!(xor(ENCODER_A, ENCODER_B))){ //If !(A ^ B), count down
counter += 1;
}
else{ //else, count up
counter -= 1;
}
sei();
}
ISR(INT3_vect){ //Pin B changed
cli();
if (!(xor(ENCODER_A, ENCODER_B))){ //If !(A ^ B), count down
counter -= 1;
}
else{ //else, count up
counter += 1;
}
sei();
}
int16_t getEncoderValue(void){
return counter;
}
void encoderReset(void){
counter = 0;
}
int16_t convert_to_degree(void){
int16_t degree_from_setpoint = (counter*360*10)/4096;
return degree_from_setpoint;
}
static int16_t previousCounterValue = 0;
int16_t getSteeringRate(void)
{
int16_t rate;
// Sends at 128Hz --> dt = 1/f = 1/128 ==> rate = delta_measurements * f
// equivalent to bit shift left by seven signs.
rate = (counter - previousCounterValue);
// Output is the derivative of steering position, bit shifter right 7 times.
// Upon retrieval, should be bit shifted left 7 times.
return rate;
}
/*
* steeringEncoder.h
*
* Created: 02.02.2016 22:41:03
* Author: Daniel
*/
#include <avr/io.h>
#include <stdbool.h>
#ifndef STEERINGENCODER_H_
#define STEERINGENCODER_H_
void encoder_init();
//void handleSteeringPosition();
int16_t getEncoderValue(void);
int16_t getSteeringRate(void);
void encoderReset(void);
int16_t convert_to_degree(void);
#endif /* STEERINGENCODER_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment