Demo entry 6656004

main

   

Submitted by anonymous on Oct 28, 2017 at 16:58
Language: C. Code size: 6.1 kB.

//
//  @FILE: main.c
//  @PROJECT: Mars Rover
//
//  Created by preston sundar on 15/03/17.
//  Copyright (c) 2017 Preston Sundar. All rights reserved.
//


#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "Preston'sUART.h"
#include <util/atomic.h>
#include <string.h>
#include <stdlib.h>
#include "motors.h"
#include "Preston'sSimpletwi.h"
#include "Preston'sHMC5883l.h"
#include "Preston'spca9685.h"

//NOTE: safe pwm value for motor in range 247 to 255, otherwise heating issue

void process_command(void);
void copy_command(void);
// The inputted commands are never going to be
// more than 8 chars long.
// volatile so the ISR can alter them
volatile unsigned char data_in[8];    //+2 chars as the newline feed chars take up two charaters-> \n
unsigned char command_in[8];

volatile unsigned char data_count;
volatile unsigned char command_ready;

char converisonBuffer[5];
char pwmStr[3], servposStr[3];

int pwmLInt = 0;
int pwmRInt = 0;
signed int servoVal = 0;

void initIO(void) {
    uart_init();
    DDRB |= 1 << PB0;
    sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
    motors_init();
    twi_INIT();
    pca9685_init(0x00, 50);
}

int main(void) {
	initIO();
    
    
	while (1) {
        
        if (command_ready == 1) {
            copy_command();
            command_ready = 0;
            process_command();
            
        }
        
        PORTB ^= 1 << PB0;
        _delay_ms(20);

        
        
        
	}
	return 0; // never reached
}

ISR(USART_RXC_vect){                //interrupt routine for the UART input
    data_in[data_count] = UDR;

    if (data_in[data_count] == '\n') {
        command_ready = 1;
        // Reset to 0, ready to go again
        data_count = 0;
    } else {
        data_count++;
    }
}



void process_command(void){         //function to process the recieved command
    
    switch (command_in[0]) {
        case 'f':
            if (command_in[1] == '|') {
                pwmStr[0] = command_in[2];
                pwmStr[1] = command_in[3];
                pwmStr[2] = command_in[4];
                
                pwmLInt = atoi(pwmStr);
                
                forward(pwmLInt, pwmLInt);
                //uart_transmit_string(pwmStr); // debug
            
            }
            
            break;
        case 'b':
            if (command_in[1] == '|') {
                pwmStr[0] = command_in[2];
                pwmStr[1] = command_in[3];
                pwmStr[2] = command_in[4];
                
                pwmLInt = atoi(pwmStr);
 
                reverse(pwmLInt, pwmLInt);
                //uart_transmit_string(pwmStr); //debug
            }
            break;

        case 'l':
            if ((command_in[1] == '|')) {
                pwmStr[0] = command_in[2];
                pwmStr[1] = command_in[3];
                pwmStr[2] = command_in[4];
                
                pwmLInt = atoi(pwmStr);
                
                pwmStr[0] = command_in[5];
                pwmStr[1] = command_in[6];
                pwmStr[2] = command_in[7];
                
                pwmRInt = atoi(pwmStr);
                
                left(pwmRInt, pwmLInt);
                
            }
            break;
        case 'r':
            if ((command_in[1] == '|')) {
                pwmStr[0] = command_in[2];
                pwmStr[1] = command_in[3];
                pwmStr[2] = command_in[4];
                
                pwmLInt = atoi(pwmStr);
                
                pwmStr[0] = command_in[5];
                pwmStr[1] = command_in[6];
                pwmStr[2] = command_in[7];
                
                pwmRInt = atoi(pwmStr);
                
                right(pwmRInt, pwmLInt);
                
            }
            break;
        case 'B':
            if ((command_in[1] == '?')) {
                itoa(HMC5883l_getHeadingDegrees(), converisonBuffer, 10);
                uart_transmit_string("B:");
                uart_transmit_string(converisonBuffer);
                uart_transmit_string("\n");
            }
            break;
        case 'x':
            if (command_in[1] == '!') {
                motors_break();
            }
        case 'p':                                   //servo control
            if (command_in[1] == '|') {
                servposStr[0] = command_in[2];
                servposStr[1] = command_in[3];
                servposStr[2] = command_in[4];
                
                servoVal = atoi(servposStr) - 90;
                
                pca9685_servo(0, servoVal);
                //uart_transmit_string(servoVal); //debug
            }
            break;
            
        case 't':
            if (command_in[1] == '|') {
                servposStr[0] = command_in[2];
                servposStr[1] = command_in[3];
                servposStr[2] = command_in[4];
                
                servoVal = atoi(servposStr) - 90;
                
                pca9685_servo(1, servoVal);
                //uart_transmit_string(servoVal); //debug
            }
            break;
        case 'G':
            if (command_in[1] == '|') {
                servposStr[0] = command_in[2];
                servposStr[1] = command_in[3];
                servposStr[2] = command_in[4];
                
                servoVal = atoi(servposStr) - 90;
                
                pca9685_servo(2, servoVal);
                //uart_transmit_string(servoVal); //debug
            }
            break;

        default:
            break;
    }

}

void copy_command(void){            //copy buffer to more "permanent" buffer
       
        ATOMIC_BLOCK(ATOMIC_FORCEON) {  //don't allow interrupts to fire
            
            memcpy(command_in, data_in, 8);
            memset(data_in, 0, 8);
            
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).