Laser control - SergeGit/rc-tank-platform GitHub Wiki

Hardware

The following states are defined for laser control:

  • OFF
  • ON

image

Laser LED

Voltage drop = 3.5V (3V)
Imax = 25 mA
Source voltage = +5V
Resistor = 100 Ohm

Control is via the Arduino. This will only be On / Off signal. The voltage can best be controlled via a transistor.

image

Laser control

1. Pin Assignments for Laser

  • Laser to use pin A3 (analog pin 3) on the Arduino Nano
  • Kept as digital output despite being on analog pin (the Arduino allows this)
// Weapon system pins
#define CANNON_PIN 30
#define LASER_PIN A3     // Changed from 32 to A3
#define IR_LIGHT_PIN 34

Initialize the light pins

  // Initialize weapon system pins
  pinMode(CANNON_PIN, OUTPUT);
  pinMode(LASER_PIN, OUTPUT);
  pinMode(IR_LIGHT_PIN, OUTPUT);
  pinMode(CANNON_READY_PIN, INPUT_PULLUP);

2. Command Handling

Laser:

void handleLaserToggle(byte data) {
  // data: 0=off, 1=on
  digitalWrite(LASER_PIN, data ? HIGH : LOW);
  Serial.print("Laser: ");
  Serial.println(data ? "ON" : "OFF");
}

3. Status Reporting

  • Added light states to the response data for GET_ALL_SENSORS command
  • Updated the status byte to include bits for front and rear light status

Using the Updated Light Commands

From the Raspberry Pi side, you can now send these commands to control the lights:

# For front lights
i2c.send_command(Command.LASER_TOGGLE, 0)  # Turn off
i2c.send_command(Command.LASER_TOGGLE, 1)  # Turn on

Code

Laser_control.ino

/*******************************************************************************
   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTY AND SUPPORT
   IS APPLICABLE TO THIS SOFTWARE IN ANY FORM. CYTRON TECHNOLOGIES SHALL NOT,
   IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL
   DAMAGES, FOR ANY REASON WHATSOEVER.
 ********************************************************************************
   DESCRIPTION:
   This code controls a laser via a ULN2001 Darlington transistor array.

   CONNECTIONS (Arduino nano v3 - Atmega328P, old bootloader):

   Arduino A3   - Digital output Laser control - ULN2001AN/(7B) via 2k2Ω 
 *******************************************************************************/

// Define pins
const int laserPin = A3;   // Darlington controlled via A3

// Parameters


// Function to set up Laser control 
void setup_Laser_ctrl() {
  // pinout laser control
  pinMode(laserPin, OUTPUT);   // Set the Laser pin as an output
}


// Functions
void laserOff(){
  //turn off Laser
  digitalWrite(laserPin, LOW);
}

void laserOn(){
  //turn on LED  
  digitalWrite(laserPin, HIGH);
}


// Function to control based on FSM
void laser_ctrl() {
  switch (state){
    case LaserState::ON:
      laserOn();
      break;

    case LaserState::OFF:
      laserOff();
      break;    
  }
}

Test code

Test_main.ino

/*******************************************************************************
   THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTY AND SUPPORT
   IS APPLICABLE TO THIS SOFTWARE IN ANY FORM. CYTRON TECHNOLOGIES SHALL NOT,
   IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL
   DAMAGES, FOR ANY REASON WHATSOEVER.
 ********************************************************************************
   DESCRIPTION:
   This is the test code for testing the Laser control function.
   The laser is controlled via a state machine.

   CONNECTIONS (Arduino nano v3 - Atmega328P, old bootloader):
 *******************************************************************************/

// Finite State Machine (FSM)
enum LaserState {
  ON,
  OFF
};
LaserState state = LaserState::OFF;  // Initial state


// Keyboard 0-9 movement for laser control
#define LASER_ON '5'
#define LASER_OFF '6'


// Initialisation
void setup() {
  Serial.begin(9600);
  setup_Laser_ctrl();             // Setting up light control 
}


void loop() {
  char byte = 0;
  // press q to cancel and exit
  while (byte != 'z') {
    Serial.readBytes(&byte, 1);

    // LASER CONTROL VIA KEYBOARD CONTROL  
    // -----------------------------------
    // press 5 for Laser On
    if (byte == LASER_ON) {
      //Set Laser On
      Serial.print("Command: Laser On \n");
      state = LaserState::ON;
      laser_ctrl();
    }

    // press 6 Laser Off
    if (byte == LASER_OFF) {
      //Set Laser Off
      Serial.print("Command: Laser Off \n");
      state = LaserState::OFF;
      laser_ctrl();
    }

  }
  Serial.print("Done \n");
  Serial.end();
}