Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!

ECEN 2440- Servo Snake

Robotic snake powered by the MSP432401R micro-controller, utilizing servo motors to emulate a snake-like movement with a sinusoidal pattern.

IntermediateWork in progress56
ECEN 2440- Servo Snake

Things used in this project

Hardware components

MSP-EXP432P401R SimpleLink MSP432 LaunchPad
Texas Instruments MSP-EXP432P401R SimpleLink MSP432 LaunchPad
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×5
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1
High Speed Single Comparator
Texas Instruments High Speed Single Comparator
×1
LSM6DSO
STMicroelectronics LSM6DSO
×1
Linear Regulator (Low Dropout)
Linear Regulator (Low Dropout)
×1
3.3V, 3.6A Step-Down Voltage Regulator D36V28F3
×1
Lumenier 500mAh 2s 50c Lipo Battery
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Board-To-Board Connector, 2.54 mm
Board-To-Board Connector, 2.54 mm
×40
2.54 mm 90 degree Male Header Pins
×5
2.54mm 90 degree Female Header Pins
×1
2.54mm Pin Header Shorting Cap
×5
Black Adhesive Velcro Dots (30078-AMS)
×1
Jumper wires (generic)
Jumper wires (generic)
×12
PLA Filament
×1
Toothpicks: 2 in and 4 in
×1
Small rubber bands
×1

Software apps and online services

Code Composer Studio
Texas Instruments Code Composer Studio

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

CAD - Head

The is the CAD version of the head. It was measured to hold the Ultrasonic sensor, magnetometer, reflectance sensor, and a servo.

Schematics

Ultrasonic Sensor Code Development - Breadboard Circuit

Within the image, you can see the complete circuit diagram/setup for the Ultrasonic sensor testing. Included is the complete wiring for the circuit with connections from the MSP432401R and the sensor itself.

Located within the MSP432 for this code to run are the following pin maps.

Vcc -> MSP432 5V Supply
Gnd -> MSP432 GND
Echo -> PIN 2.6
(Used with a voltage divider of 3 equal resistance Resistors in series. Drawing voltage after 1st Resistor.
Trip -> PIN 2.7

Sensor's trigger distance is configured within the code, allowing for setting ranges, minimum values and maximum values for detection. As is the case with an Ultrasonic sensor, calculations are made converting from the speed of sound.

PCB Layout

PCB Schematics

Code

Firmware zip file

C/C++
Press PS1 to begin the snake's movement, then place a wall 15cm in front to trigger the ultrasonic sensor and initiate a right turn. The amplitude and right and left offset values can be adjusted for more aggressive sinusoidal movement and sharper turning, respectively.
No preview (download only).

Ultrasonic Sensor Code Development - Code

C/C++
Ultrasonic sensor testing code is driven based on the provided circuit diagram. Connecting the Ultrasonic sensor to the MSP432, allowable ranges for detection are made.

Within the code, timers are set up for driving and triggering the sensor while an ECHO pin reads in elapsed ticks derived from the setup timer. From here, a distance is made, starting on line 146, where ticks captured is converted into elapsed ticks and from there multiplied by the speed of sound to convert the time taken in ticks to a distance.

Note: Code measures distance in centimeters and recordings can only happen as fast as the timer allows for interrupts to be processed Current range is set for 15 to 10 cm, meaning that when the sensor detects a distance between these ranges a placeholder state change will occur allowing code to be easily integrated into other projects.
/**
 * main.c
 *
 * TIMER A2 generates a 10Hz pulse train to initialize the Ultrasonic sensor
 * TIMER A0 is used for input capture to measure the distance
 */

#include "msp.h"
#define TICKS ((uint16_t)0xFFFF)
#define TICKS2 ((uint16_t)0x6DDD)



float TickLength = 1.333;   // uS
float SpeedOfSound = 0.034; // cm per uS
int c = 4;

volatile int covDebug = 0;

volatile uint32_t CaptureValues [2] = {0};
volatile uint32_t ElapsedTicks = 0;
volatile float ElapsedTime = 0;
volatile float Distance = 0;

void timerA03_stop(void){
    TIMER_A0->CTL &= TIMER_A_CTL_MC__STOP;
}

void timerA04_stop(void){
    TIMER_A0->CTL &= TIMER_A_CTL_MC__STOP;
}

void timerA04_start(void){
    TIMER_A0->CTL |= TIMER_A_CTL_MC__UPDOWN;
    TIMER_A0->CCR[0]    = TICKS2;
    TIMER_A0->CCR[4]    = TICKS2 - c;
}

void timerA04_config(void){

    // trig on sensor - P2.7

    TIMER_A0->CTL       |= TIMER_A_CTL_CLR;
    TIMER_A0->CTL       |= TIMER_A_CTL_SSEL__SMCLK;
    TIMER_A0->CTL       |= TIMER_A_CTL_CLR;
    TIMER_A0->CTL       |= TIMER_A_CTL_ID_2;         // sets timer ID to 2 ---- division by 4
    TIMER_A0->CCTL[4]   |= TIMER_A_CCTLN_OUTMOD_7;   // resets Output
    TIMER_A0->CCTL[4]   &= TIMER_A_CCTLN_OUTMOD_4;   // sets output to toggle

}

void timerA03_config(void){

    // configuring clock

    TIMER_A0->CTL       |= TIMER_A_CTL_CLR;          // clears TA0
    TIMER_A0->CTL       |= TIMER_A_CTL_SSEL__SMCLK;  // use SMCLK
    TIMER_A0->CTL       |= TIMER_A_CTL_ID_2;         // sets timer ID to 2 ---- division by 4

    //configuring interrupts
    TIMER_A0->CCTL[3]   |= TIMER_A_CCTLN_CCIE;       // enable interrupt for  echo on sensor - P2.6

    TIMER_A0->CCTL[3]   |= TIMER_A_CCTLN_CM__BOTH;   // set as capture input on rising and falling edge
    TIMER_A0->CCTL[3]   |= TIMER_A_CCTLN_CAP;        // set as capture mode

    TIMER_A0->CCTL[3]   &= ~TIMER_A_CCTLN_CCIFG;     //clear interrupt flag

}

void timerA03_start(void){
    TIMER_A0->CTL |= TIMER_A_CTL_MC__UP;

    TIMER_A0->CCR[0] = TICKS;
}

void config_NVIC(void){
    __NVIC_EnableIRQ(TA0_N_IRQn); //enables timer A interrupt
}

void gpio_config(void){
    P2->DIR     |= BIT7;             // trig gpio configuration
    P2->OUT     &= ~(BIT7);

    P2->SEL0    |= BIT7;
    P2->SEL1    &= ~(BIT7);

    P2->DIR     &= ~BIT6;            // configure P2.6 to input
    P2->OUT     &= ~BIT6;            // configure pull down on P2.6
    P2->REN     |=  BIT6;            // enable pull down

    P2->SEL0    |=   BIT6;           // enable primary module function on P2.6 (TIMER_A0 CCR3)
    P2->SEL1    &=  ~BIT6;
}

void TA0_N_IRQHandler(void){
    __NVIC_DisableIRQ(TA0_N_IRQn); // disable since we're in the interrupt

    if(TIMER_A0->CCTL[3] & TIMER_A_CCTLN_CCIFG)
    {

        if(TIMER_A0->CCTL[3] & TIMER_A_CCTLN_CCI)
        {
            CaptureValues[0] = TIMER_A0->CCR[3];
        }
        else
        {
            CaptureValues[1] = TIMER_A0->CCR[3];

            if(CaptureValues[0] < CaptureValues[1])
            {
                ElapsedTicks = CaptureValues[1] - CaptureValues[0]; //find elapsed ticks
            }
            else
            {
                ElapsedTicks = (CaptureValues[1] + TICKS) - CaptureValues[0];
            }
        }
        // clear the interrupt source flag
        TIMER_A0->CCTL[3] &= ~TIMER_A_CCTLN_CCIFG;
    }

    __NVIC_EnableIRQ(TA0_N_IRQn); // enable interrupt since we are about to exit handler
}

//change

void main(void)
{
    WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;     // stop watchdog timer

    volatile uint32_t state = 1;    // test driven development statement

    timerA04_stop();
    timerA04_config();
    timerA04_start();

    timerA03_stop();
    timerA03_config();
    timerA03_start();
    gpio_config();
    config_NVIC();

    while(state == 1)
    {

        ElapsedTime = ElapsedTicks * TickLength;        //convert ticks to time
        Distance = ElapsedTime * SpeedOfSound / 2;      //centimeters
        if(Distance <= 15 && Distance >= 14
                )
        {

            state = 2;

        }

    }
    while(state == 2)
    {

        while(1);

    }

}

Credits

Case Chrisbacher
1 project • 1 follower
Contact
Skye Reese
1 project • 1 follower
Contact
Garrett GANE0483
1 project • 0 followers
Contact
Alexis Muniz
1 project • 0 followers
Contact
Ricardo Ramirez Bunsow
1 project • 0 followers
Contact
Khalid Shahba
1 project • 0 followers
Contact
Arielle Blum
1 project • 1 follower
Contact
Nicholas Haratsaris
1 project • 0 followers
Contact
Joshua Bay
1 project • 0 followers
Contact
Jiamiao He
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.