C Program Test Script For Moonbuggy Motor Control

How to write a C Program Test script for moonbuggy motor control in C Programming Language.

Solution -

/*
 * Test script for moonbuggy motor control
 */

const int throttle = A0;
const int field_PWM_pin = 2;
const int field_phase = 38;
const int arm_PWM_L = 5;
const int arm_PWM_R = 4;
const int phase_switch = 40;
int PHASE = 1;
int sensorValue = 0;
int throttleValue = 0;
int field_PWM = 0;


void setup() {
  pinMode(phase_switch, INPUT); //phase (direction) input
  pinMode(field_phase, OUTPUT); //phase (direction) output
  pinMode(field_PWM, OUTPUT); //field PWM
  pinMode(arm_PWM_L, OUTPUT); //armature PWM left
  pinMode(arm_PWM_R, OUTPUT); //armature PWM right
  delay(1000); //start up delay to allow charge pumps on motor controllers to stabilize
}
void loop() {
  PHASE = digitalRead(phase_switch); //read the phase input switch HIGH for forward and LOW for reverse
  sensorValue = analogRead(throttle); //input of raw throttle value
  throttleValue = map(sensorValue, 180, 818, 0, 255); //map min/max of throttle to 8bit integer

  digitalWrite(field_phase, PHASE); //set PHASE output to field controller
  if (throttleValue < 13){ //introducing a 5% dead zone at the low end of throttle due to unstable analog input
    analogWrite(field_PWM_pin, fiel_PWM); //set field voltage
    analogWrite(arm_PWM_L, throttleValue); //apply armature voltage
    analogWrite(arm_PWM_R, throttleValue); //apply armature voltage
  } else {
    analogWrite(field_PWM_pin, 0); //set field voltage
    analogWrite(arm_PWM_L, 0); //apply armature voltage
    analogWrite(arm_PWM_R, 0); //apply armature voltage
  }

}


Learn More :