C Code
#include <hidef.h> /* common defines and macros */
#include <mc9s12e128.h> /* derivative information */
#include <S12E128bits.h>
#include <stdio.h>
#include "XBee.h"
#include "helm_display.h"
#include "ADS12.H"
#include "helm_input.h"
// # defines for Specials
#define SPECIAL1_HI BIT0HI
#define SPECIAL1_LO BIT0LO
#define SPECIAL2_HI BIT1HI
#define SPECIAL2_LO BIT1LO
// ----------------------------------------
// --- Functions
// ----------------------------------------
// Initialization _________________________________________
void InitInputPorts(void)
{
// initialize all ports to analog, because, well, fuck it
if (ADS12_Init("AAAAAAAA") == ADS12_OK)
{
printf("A/D Initialized Successfully\r\n");
}
else
{
printf("A/D Failed to Initialize\r\n");
}
// Initialize special inputs E0 = special 1, E1 = special 2
//DDRE &= (SPECIAL1_LO & SPECIAL2_LO);
DDRE = 0x00;
}
// A/D Reads_______________________________________________
char Report_Special1(void)
{
return (PORTE & BIT7HI);
}
char Report_Special2(void)
{
return (PORTE & BIT1HI);
}
int Report_Voice(void)
{
return ADS12_ReadADPin(4); //was 0 // AD3 was broken
}
int Report_FSR(void)
{
return ADS12_ReadADPin(2); //was 1
}
int Report_Roll(void)
{
return ADS12_ReadADPin(1);
}
int Report_Pitch(void)
{
return ADS12_ReadADPin(0);
}
// Interpret A/D ___________________________________________
char Set_Speed(void)
{
static unsigned int voice_in;
static char direction;
static unsigned int temp_v;
voice_in = Report_Voice();
// printf("Voice IN = %d\r\b", voice_in);
// check for invalid values
if( (voice_in < VOICE_MIN))
{
// printf("Voice level below minimum. Voice: %d, Min: %d\r\n", voice_in, VOICE_MIN);
// printf("Defaulting to a STOP (8)\r\n");
return 8; //stop
}
// check direction
direction = Check_Reverse();
if (direction == FORWARD)
{
if( (voice_in > VOICE_MAX))
{
// printf("Voice level exceeds maximum. Voice: %d, Max: %d\r\n", voice_in, VOICE_MAX);
// printf("Defaulting to FULL SPEED AHEAD (F)\r\n");
return 15; //full ahead
}
// (Vin - Vmin) / (Vmax - Vmin) = % of throttle scale
// * F - 8 = translates to the direction scale
// + 8 put it into the appropriate positive range
temp_v = ((voice_in - VOICE_MIN) * 7) / (VOICE_MAX - VOICE_MIN);
return (temp_v + 8);
} else if (direction == REVERSE)
{
if( (voice_in > VOICE_MAX))
{
// printf("Voice level exceeds maximum. Voice: %d, Max: %d\r\n", voice_in, VOICE_MAX);
// printf("Defaulting to FULL SPEED REVERSE (F)\r\n");
return 0; //full reverse
}
// same, but there are 8 options ont he bottom side
// (0x08 - 0x00)
// and the scale goes from 0 to 8
temp_v = 8-((voice_in - VOICE_MIN) * 8) / (VOICE_MAX - VOICE_MIN);
return (temp_v);
}
}
char Set_Direction(void)
{
static unsigned int roll_in;
static unsigned int temp_d;
roll_in = Report_Roll();
// printf("Roll IN = %d\r\n", roll_in);
// check for invalid values
if (roll_in > ROLL_MAX)
{
// printf("ROLL exceeds maximum value. Roll: %d, Max: %d\r\n", roll_in, ROLL_MAX);
// printf("Defaulting to FULL LEFT (0)\r\n");
return 0;
}
if (roll_in < ROLL_MIN)
{
// printf("ROLL is below minimum value. Roll: %d, Min: %d\r\n", roll_in, ROLL_MIN);
// printf("Defaulting to FULL RIGHT (F)\r\n");
return 15;
}
// check for dead zone
if( (roll_in <= (ROLL_ZERO + ROLL_DEAD_ZONE))&&(roll_in >= (ROLL_ZERO - ROLL_DEAD_ZONE)))
{
return 8; // 8 is dead ahead
}
// scale linearly in two spaces outside of the dead zone
if(roll_in > ROLL_ZERO + ROLL_DEAD_ZONE)
return (15-((roll_in - (ROLL_ZERO + ROLL_DEAD_ZONE))*7/(ROLL_MAX - (ROLL_ZERO + ROLL_DEAD_ZONE))+8));
//return ((roll_in - ROLL_MIN) * (15) / (ROLL_MAX - ROLL_MIN));
return 15-((8-((ROLL_ZERO - ROLL_DEAD_ZONE) - roll_in)*8/((ROLL_ZERO - ROLL_DEAD_ZONE)-ROLL_MIN)));
}
char Set_PumpSpeed(void)
{
static int FSR_in;
static int temp_FSR;
FSR_in = Report_FSR();
temp_FSR = (((FSR_in - FSR_MIN) * 15)/(FSR_MAX - FSR_MIN));
if (temp_FSR > 15) return 15;
return temp_FSR;
}
char Check_Reverse(void)
{
static int pitch;
pitch = Report_Pitch();
// printf("Pitch is: %d\r\n", pitch);
if (pitch > REVERSE_THRESHOLD)
{
// printf("Forward (Check_Reverse)\r\n");
return FORWARD;
} else
{
// printf("Reverse (Check_Reverse)\r\n");
return REVERSE;
}
}