LEGO SPIKE Prime Triceratops: C Language Roars to Life!

by AfrelEdTech in Circuits > Robots

23 Views, 0 Favorites, 0 Comments

LEGO SPIKE Prime Triceratops: C Language Roars to Life!

サムネイル用.jpg

Can a robot dinosaur walk on its own and find food?

Yes it can—with a little help from C programming and LEGO SPIKE Prime!

This time, we created a Triceratops robot that walks and stops when it finds its favorite treat. Let’s give it life with code!

Supplies

  1. 1 PC with Windows 10 or 11 operating system.
  2. SPIKE Prime

Robot Setup

cover.jpg

Our Triceratops is built with:

  1. Legs: Two motors for left and right movement
  2. Nose (Smell Sensor): A color sensor to detect food (red blocks!)


What It Can Do

Walking Legs

Each leg moves one step at a time, with perfect timing between left and right.

We used multitasking (cyclic handlers) to control both legs together so the robot walks smoothly like a real creature.


Smell Sensor

The color sensor acts like a nose!

When the robot detects the color red, it stops—just like it found some delicious food!

Pro Tips!

  1. The secret to smooth walking? Precise timing and coordination between legs!
  2. We used multitasking in C to make both motors work at the same time.
  3. And don’t forget the mechanical build—getting it to walk straight takes some clever design!


.c

pup_motor_t *motorA;
pup_motor_t *motorB;
pup_device_t *ColorSensor;
pup_device_t *ForceSensor;
pup_device_t *UltraSonicSensor;

char color_now;

int move_180degrees(pup_motor_t *motor) {
if (pup_motor_get_count(motor) > -170) {
pup_motor_set_speed(motor,400);
} else {
pup_motor_stop(motor);
return 1;
}
return 0;
}

void Main(intptr_t exinf)
{
motorA = pup_motor_init(PBIO_PORT_ID_A, PUP_DIRECTION_COUNTERCLOCKWISE);
motorB = pup_motor_init(PBIO_PORT_ID_B, PUP_DIRECTION_CLOCKWISE);
ColorSensor= pup_color_sensor_get_device(PBIO_PORT_ID_C);
pup_motor_reset_count(motorA);
pup_motor_reset_count(motorB);

// Wait for left button to be pressed
hub_button_t pressed;
while(!(pressed&HUB_BUTTON_LEFT)){
hub_button_is_pressed(&pressed);
hub_light_on_color(PBIO_COLOR_GREEN);
}

dly_tsk(3000000);

sta_cyc(CYC_HDR); // Start color_task

int compFlg_A = 0;
int compFlg_B = 0;

while(color_now != 'r') {
compFlg_A = move_180degrees(motorA);
compFlg_B = move_180degrees(motorB);
if(compFlg_A && compFlg_B) {
pup_motor_reset_count(motorA);
pup_motor_reset_count(motorB);
}
}

// Stop moving
pup_motor_stop(motorA);
pup_motor_stop(motorB);

// Exit the program
exit(0);
}

// Read color every 1ms
void color_task(intptr_t exinf)
{
color_now = pup_color_sensor_color_name(ColorSensor, true);
}

.cfg

INCLUDE("tecsgen.cfg");

#include "Triceratops.h"
CRE_TSK(MAIN_TASK, { TA_ACT, 0, Main, MAIN_PRIORITY + 1, STACK_SIZE, NULL });
CRE_TSK(CYCHDR_TASK, { TA_NULL, 0, color_task, MAIN_PRIORITY, STACK_SIZE, NULL });
CRE_CYC(CYC_HDR, { TA_NULL, { TNFY_ACTTSK, CYCHDR_TASK }, 1*1000, 0 });

Let’s Make Dinosaurs Walk!

LEGO SPIKE Prime Triceratops: C Language Roars to Life!

It’s so much fun to bring robots to life with just a few lines of C code.

Why not build your own robot dinosaur and teach it to walk, search, or even roar?

Let your imagination go wild!

【Free Trial】Start C Language Programming With Afrel's Educational Materials

Are you intrigued by the idea of programming your SPIKE™ Prime with C? Did you know that Afrel is a key information provider for SPIKE™ Prime?


To meet the demand of those who want to "learn more about SPIKE™ Prime " and "try programming in C," Afrel has developed and sells educational materials for learning C language programming with SPIKE™ Prime.


Stepping up from Python to C is an excellent opportunity to further enhance your programming skills. Learning C will give you a deeper understanding of how computers work and enable more advanced control.


Afrel's C language materials are designed for beginners, ensuring a smooth learning experience. Why not take this chance to discover the joy of controlling SPIKE™ Prime with C and expand your programming horizons?


If you're ready to tackle C language programming, click the link below for more details.


Learn more about SPIKE™ Prime C Language Programming Materials here