<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 砂田沙耶</title>
    <description>The latest articles on DEV Community by 砂田沙耶 (@_ff41734170f7cc70ac79).</description>
    <link>https://dev.to/_ff41734170f7cc70ac79</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2817435%2F0583adc5-35da-4fd3-b9b9-eefe77b0482c.png</url>
      <title>DEV Community: 砂田沙耶</title>
      <link>https://dev.to/_ff41734170f7cc70ac79</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_ff41734170f7cc70ac79"/>
    <language>en</language>
    <item>
      <title>Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 4 : Color Detection Accuracy</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Sun, 30 Mar 2025 23:30:25 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9lbhja7a0dcrwknmvln8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9lbhja7a0dcrwknmvln8.png" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
When participating in a robotics contest using LEGO SPIKE Prime, choosing the right programming environment can impact performance. In this experiment, we tested how accurately different programming environments detect colors while the robot is in motion.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tested Programming Environments
&lt;/h2&gt;

&lt;p&gt;I compared the following four environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word Blocks (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (Pybricks) → &lt;a href="https://pybricks.com/" rel="noopener noreferrer"&gt;More info&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C Language (spike-rt) → &lt;a href="https://github.com/spike-rt/spike-rt" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Robot Configuration
&lt;/h2&gt;

&lt;p&gt;For the test, I used a car-type robot with the following setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Left wheel motor: Port A&lt;/li&gt;
&lt;li&gt;Right wheel motor: Port B&lt;/li&gt;
&lt;li&gt;Side-facing color sensor: Port C&lt;/li&gt;
&lt;li&gt;Downward-facing color sensor: Port D
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftp0h1fxnvbs771pmefov.PNG" alt=" "&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Test Method
&lt;/h2&gt;

&lt;p&gt;To compare the environments, I conducted the following test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The side-facing color sensor detects various block colors while the robot moves.&lt;/li&gt;
&lt;li&gt;The downward-facing color sensor detects black and stops the robot.&lt;/li&gt;
&lt;li&gt;Each environment was tested five times, and the average results were compared.&lt;/li&gt;
&lt;li&gt;Programs were optimized for each environment while keeping the movement speed nearly the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Word Blocks (SPIKE App 3)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyncrghocs1bbpt8hl6r0.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyncrghocs1bbpt8hl6r0.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
Python (SPIKE App 3)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Set up all devices
LineSensor = port.D
ColorSensor = port.C

colors = []
color_now = None

# Color reading task
async def color_task():
    global color_now
    while True:
        color_now = color_sensor.color(ColorSensor)
        await runloop.sleep_ms(1)

async def main():
    global colors
    global color_now

    motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=900)

    while color_sensor.reflection(LineSensor) &amp;gt; 50:
        if len(colors) == 0 or color_now != colors[-1]:
            colors.append(color_now)
        await runloop.sleep_ms(1)

    motor_pair.stop(motor_pair.PAIR_1, stop=motor.BRAKE)

    print(colors)

runloop.run(color_task(), main())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python (Pybricks)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Set up all devices.
hub = PrimeHub()
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, Direction.CLOCKWISE)
line_sensor = ColorSensor(Port.D)
color_sensor = ColorSensor(Port.C)

# Variables for color detection
color_now = None
colors = []

# Color reading task
async def color_task():
    global color_now
    while True:
        color_now = await color_sensor.color(surface=True)
        await wait(1)

async def run():
    global colors
    global color_now

    # start moving
    left_motor.run(900)
    right_motor.run(900)

    while await line_sensor.reflection() &amp;gt; 50:
        if len(colors) == 0 or color_now != colors[-1]:
            colors.append(color_now)
        await wait(1)

    left_motor.brake()
    right_motor.brake()

    print(colors)

async def main():
    await multitask(color_task(), run())

run_task(main())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C Language (spike-rt)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pup_motor_t *motorA;             
pup_motor_t *motorB;             
pup_device_t *ColorSensor;       
pup_device_t *LineSensor;

char colors[30]; // Array to store detected colors
char color_now;

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);
  LineSensor = pup_color_sensor_get_device(PBIO_PORT_ID_D);
  ColorSensor= pup_color_sensor_get_device(PBIO_PORT_ID_C);

  int8_t i = 0;

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

  sta_cyc(CYC_HDR); // Start color_task

  // Start moving
  pup_motor_set_speed(motorA,900);
  pup_motor_set_speed(motorB,900);

  // Add detected colors to the array
  while (pup_color_sensor_reflection(LineSensor) &amp;gt; 50 ) {
    if (i == 0 || color_now != colors[i-1]) {
      if (i &amp;lt; sizeof(colors) - 1) {
        colors[i] = color_now;
        i++;
      }
    }
  }

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

  stp_cyc(CYC_HDR); // Stop color_task

  // Output the colors detected
  int8_t j;
  for (j = 0; j &amp;lt; 30; j++) {
    syslog(LOG_NOTICE, "%d : %c", j, colors[j]);
  }
}

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The robot and block placement during the test:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5l7nsbqasexf4ljoxmn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5l7nsbqasexf4ljoxmn.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expected color detection sequence:&lt;br&gt;
None → Red → None → Green → None → Green → Red → None → Yellow → Blue → None → Green → None → Red → None → Blue → None&lt;br&gt;
  &lt;iframe src="https://www.youtube.com/embed/Ea6X1Y07XXs"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results: Which Environment Was Most Accurate?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnxpoh2gkv4sasme2leb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnxpoh2gkv4sasme2leb.PNG" alt=" "&gt;&lt;/a&gt;&lt;br&gt;
The environment with the least misdetections was C Language (spike-rt):&lt;br&gt;
1.2% - C Language (spike-rt)&lt;br&gt;
2.5% - Python (Pybricks)&lt;br&gt;
3.22% - Word Block (SPIKE App3)&lt;br&gt;
4.27% - Python (SPIKE App3)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observed Trends&lt;/strong&gt;&lt;br&gt;
Word Block &amp;amp; Python (SPIKE App3):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More likely to misdetect colors when changing to/from green (often detecting light blue or black by mistake).&lt;/li&gt;
&lt;li&gt;More likely to misdetect white when changing from yellow.&lt;/li&gt;
&lt;li&gt;More likely to misdetect light blue when changing from blue.&lt;/li&gt;
&lt;li&gt;Seemed to detect background colors instead of "None" when the object’s color was ambiguous or distant.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pybricks &amp;amp; C (spike-rt):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More likely to misdetect yellow when changing from green.&lt;/li&gt;
&lt;li&gt;More likely to misdetect white when changing from yellow.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Want to Try C Programming on LEGO SPIKE Prime?
&lt;/h2&gt;

&lt;p&gt;If you’re interested in trying C on SPIKE Prime, there are beginner-friendly learning materials available. As of March 2025, a trial version is also accessible—&lt;a href="https://afrel.stores.jp/?category_id=672db77de533d013abda0a7e" rel="noopener noreferrer"&gt;give it a try!&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Related Articles&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3"&gt;Testing LEGO SPIKE Prime with C: Line Follower Speed &amp;amp; Stability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73"&gt;Introducing SPIKE-RT: the C Language Software Platform for LEGO SPIKE Prime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 3&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>lego</category>
      <category>spikeprime</category>
    </item>
    <item>
      <title>Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 3</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Mon, 24 Mar 2025 00:09:39 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Does the Programming Environment Affect Rotation Accuracy? A 90° Test Using the Gyro Sensor&lt;/strong&gt;&lt;br&gt;
When using LEGO SPIKE Prime in robotics competitions, how much does the choice of programming environment affect rotation accuracy?&lt;br&gt;
To investigate this, I conducted an experiment comparing different programming environments.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tested Programming Environments
&lt;/h2&gt;

&lt;p&gt;I compared the following four environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word Blocks (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (Pybricks) → &lt;a href="https://pybricks.com/" rel="noopener noreferrer"&gt;More info&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C Language (spike-rt) → &lt;a href="https://github.com/spike-rt/spike-rt" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Robot Configuration
&lt;/h2&gt;

&lt;p&gt;For the test, I used a car-type robot with the following setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Left motor: Port A&lt;/li&gt;
&lt;li&gt;Right motor: Port B&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6339guuwv1yogllkw6aj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6339guuwv1yogllkw6aj.png" alt="Image description" width="800" height="609"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Test Method
&lt;/h2&gt;

&lt;p&gt;To compare the environments, I conducted the following test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the hub’s gyro sensor to rotate the robot 90°&lt;/li&gt;
&lt;li&gt;Measure the difference between the target angle (90°) and the actual rotation&lt;/li&gt;
&lt;li&gt;Perform 10 trials for each environment and calculate the average error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Program Code:&lt;/strong&gt;&lt;br&gt;
Word Blocks&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2rn83aitkkzo4wzho5bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2rn83aitkkzo4wzho5bc.png" alt="Image description" width="673" height="839"&gt;&lt;/a&gt;&lt;br&gt;
Python (SPIKE App 3)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import motor, motor_pair, time
from hub import motion_sensor, port, button

motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
motion_sensor.up_face()

for i in range(10):
    # Wait for the left button to be pressed
    while not button.pressed(button.LEFT):
        pass

    while button.pressed(button.LEFT):
        pass   

    # Reset yaw
    motion_sensor.reset_yaw(0)
    time.sleep_ms(50)   # Wait for yaw angle to reset

    start = motion_sensor.tilt_angles()[0]*0.1

    # Turn right
    while (abs(motion_sensor.tilt_angles()[0]*0.1) &amp;lt; 90):
        motor_pair.move_tank(motor_pair.PAIR_1, 300, -300)

    # Stop turning
    motor_pair.stop(motor_pair.PAIR_1,stop=motor.BRAKE)

    time.sleep_ms(500)
    stop = abs(motion_sensor.tilt_angles()[0]*0.1)
    print("start:", start, "stop:", stop)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python (Pybricks)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch

hub = PrimeHub()
left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, Direction.CLOCKWISE)

for i in range(10):
    # Wait until button is pressed
    while not any(hub.buttons.pressed()):
        wait(10)
    # Wait for all buttons to be released.
    while any(hub.buttons.pressed()):
        wait(10)

    while not hub.imu.ready():
        wait(10)

    # Reset the IMU
    hub.imu.reset_heading(0)
    wait(500)
    start_heading = hub.imu.heading()

    # Turn right
    while abs(start_heading - hub.imu.heading()) &amp;lt; 90:
        left_motor.run(300)
        right_motor.run(-300)

    # Stop turning
    left_motor.brake()
    right_motor.brake()

    wait(500)
    stop_heading = round(hub.imu.heading(), 1)
    print("start:", start_heading, "stop:", stop_heading)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C Language (spike-rt)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;t_syslog.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;kernel.h&amp;gt;

#include &amp;lt;spike/hub/system.h&amp;gt;

#include &amp;lt;gyro_turn.h&amp;gt;

#include "spike/pup/motor.h"
#include "spike/pup/colorsensor.h"
#include "spike/pup/forcesensor.h"
#include "spike/pup/ultrasonicsensor.h"

#include "spike/hub/battery.h"
#include "spike/hub/button.h"
#include "spike/hub/display.h"
#include "spike/hub/imu.h"
#include "spike/hub/light.h"
#include "spike/hub/speaker.h"

#include &amp;lt;pbio/color.h&amp;gt;

#include "kernel_cfg.h"
#include "syssvc/serial.h"
#include "math.h"

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

// 0:roll(x) 1:pitch(y) 2:yaw(z)
float ang_v[3] = {0};
float imu_offset[3] = {0};

float start_value; // Gyro value at start
float stop_value; // Gyro value at stop

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);

  imu_setup(imu_offset); // Offset calibration

  int8_t i;
  for (i = 0; i &amp;lt; 10; i++) {
    // Wait for left button to be pressed
    hub_button_t pressed;
    while(!(pressed&amp;amp;HUB_BUTTON_LEFT)){
      hub_button_is_pressed(&amp;amp;pressed);
      hub_light_on_color(PBIO_COLOR_GREEN);
    }
    // Wait for the left button to be released
    while (pressed &amp;amp; HUB_BUTTON_LEFT) {
      hub_button_is_pressed(&amp;amp;pressed);
      hub_light_on_color(PBIO_COLOR_BLUE);
    }

    dly_tsk(500*1000);
    sta_cyc(CYC_HDR); // Start gyro sensor monitoring
    hub_imu_init(); // Initializing the IMU

    // Reset yaw
    ang_v[2] = 0;
    start_value = ang_v[2]; 

    // Turn right
    while (fabs(ang_v[2]) &amp;lt; 90) {
        pup_motor_set_power(motorA, 30);
        pup_motor_set_power(motorB, -30);
    }

    // Stop turning
    pup_motor_brake(motorA);
    pup_motor_brake(motorB);

    dly_tsk(500000);
    stop_value = ang_v[2];
    syslog(LOG_NOTICE,  "start: %d.%02d stop: %d.%02d",  
      (int)start_value, (int)((start_value - (int)start_value) * 100), 
      (int)stop_value, (int)(fabs(stop_value - (int)stop_value) * 100));
}
  stp_cyc(CYC_HDR); // Stop gyro sensor monitoring
}

void gyro_monitor(intptr_t exinf)
{
  float ang_raw[3]; // IMU angular acceleration
  hub_imu_get_angular_velocity(ang_raw);

  // Offset Correction
  ang_v[0] += (ang_raw[0] - imu_offset[0]) * 0.001;
  ang_v[1] += (ang_raw[1] - imu_offset[1]) * 0.001;
  ang_v[2] += (ang_raw[2] - imu_offset[2]) * 0.001;
}

void imu_setup(float offset[3]){
  dly_tsk(3*1000*1000);

  hub_light_on_color(PBIO_COLOR_ORANGE);

  hub_imu_init(); // Initializing IMU

  float ang_raw[3]; // Raw IMU angular velocity

  // Offset calibration
  for(int i=0; i&amp;lt;1000; i++){
    hub_imu_get_angular_velocity(ang_raw);
    offset[0] += ang_raw[0];
    offset[1] += ang_raw[1];
    offset[2] += ang_raw[2];
    dly_tsk(1*1000);
  }

  offset[0] /= 1000;
  offset[1] /= 1000;
  offset[2] /= 1000;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vZKALC3aw3I"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Results: Which Environment Was Most Accurate?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fek3zpxbbcs2rviildgoi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fek3zpxbbcs2rviildgoi.PNG" alt="Image description" width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are the average rotation errors (smaller is better):&lt;br&gt;
1️⃣ 6.4° - C Language (spike-rt) 🏆&lt;br&gt;
2️⃣ 8.6° - Python (Pybricks)&lt;br&gt;
3️⃣ 9.6° - Python (SPIKE App 3)&lt;br&gt;
4️⃣ 17.1° - Word Blocks (SPIKE App 3)&lt;br&gt;
Additionally, I measured the error fluctuation range (stability):&lt;br&gt;
1️⃣ 0.8° - C Language (spike-rt) 🏆&lt;br&gt;
2️⃣ 0.9° - Word Blocks (SPIKE App 3)&lt;br&gt;
3️⃣ 1.0° - Python (Pybricks)&lt;br&gt;
4️⃣ 1.2° - Python (SPIKE App 3)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C Language (spike-rt) had the most accurate rotation (smallest error)&lt;/li&gt;
&lt;li&gt;C Language (spike-rt) also had the most stable results (smallest variation)
________________________________________&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Want to Try C Programming on LEGO SPIKE Prime?
&lt;/h2&gt;

&lt;p&gt;If you’re interested in trying C on SPIKE Prime, there are beginner-friendly learning materials available. As of March 2025, a trial version is also accessible—&lt;a href="https://afrel.stores.jp/?category_id=672db77de533d013abda0a7e" rel="noopener noreferrer"&gt;give it a try!&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Related Articles
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3"&gt;Testing LEGO SPIKE Prime with C: Line Follower Speed &amp;amp; Stability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73"&gt;Introducing SPIKE-RT: the C Language Software Platform for LEGO SPIKE Prime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔹 More tests are planned, including further evaluations for robotics competitions. Stay tuned for future updates! 🚀&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Comparing LEGO SPIKE Prime Programming : Which Is Best for Robotics Competitions? - 2</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Mon, 17 Mar 2025 00:11:17 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Does the Programming Environment Affect Precision? A 360° Rotation Test&lt;/strong&gt;&lt;br&gt;
When using LEGO SPIKE Prime in robotics competitions, does the choice of programming environment impact movement precision?&lt;br&gt;
To investigate this, I conducted an experiment comparing different programming environments.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tested Programming Environments
&lt;/h2&gt;

&lt;p&gt;I compared the following four environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word Blocks (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (Pybricks) → &lt;a href="https://pybricks.com/" rel="noopener noreferrer"&gt;More info&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C Language (spike-rt) → &lt;a href="https://github.com/spike-rt/spike-rt" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Robot Configuration
&lt;/h2&gt;

&lt;p&gt;For the test, I used a car-type robot with the following setup:&lt;br&gt;
•Left motor: Port A&lt;br&gt;
•Right motor: Port B&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw0hkpu65lxk2x4lthuq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvw0hkpu65lxk2x4lthuq.png" alt="Image description" width="800" height="609"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Test Method
&lt;/h2&gt;

&lt;p&gt;To compare the environments, I conducted the following test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Command the robot to rotate 360° using a specified motor angle&lt;/li&gt;
&lt;li&gt;Measure the difference between the target and actual rotation angles&lt;/li&gt;
&lt;li&gt;Perform 10 trials for each environment and calculate the average error&lt;/li&gt;
&lt;li&gt;The same logic was used for all environments
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Uy_Zyjhbu6k"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Program Code (for all environments):&lt;/strong&gt;&lt;br&gt;
Word Blocks (SPIKE App3)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhsoj9qcjj4e2knmu7xk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhsoj9qcjj4e2knmu7xk.png" alt="Image description" width="752" height="805"&gt;&lt;/a&gt;&lt;br&gt;
Python (SPIKE App3)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tread = 8
d_tire = 5.6
goal = 360;

for i in range(10):
motor.reset_relative_position(port.A, 0)
start_angle = motor.relative_position(port.A)
motor.run(port.A, -300)
motor.run(port.B, -300)

while tread/d_tire*goal &amp;gt; -motor.relative_position(port.A):
pass

motor.stop(port.A, stop=motor.BRAKE)
motor.stop(port.B, stop=motor.BRAKE)

time.sleep_ms(500)
print("error:", abs(motor.relative_position(port.A)-start_angle) - tread/d_tire*goal )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python (Pybricks)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hub = PrimeHub()
motorA = Motor(Port.A, Direction.COUNTERCLOCKWISE)
motorB = Motor(Port.B, Direction.CLOCKWISE)

tread = 8
d_tire = 5.6
goal = 360

for i in range(10):
start_angle = motorA.angle()

motorA.run(300)
motorB.run(-300)

while tread/d_tire*goal &amp;gt; motorA.angle() - start_angle:
pass

motorA.brake()
motorB.brake()

wait(500)
print("error", motorA.angle()-start_angle - tread/d_tire*goal )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C Language (spike-rt)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void Main(intptr_t exinf)
{
dly_tsk(5000000);

uint8_t tread = 8;
float d_tire = 5.6;
uint32_t goal = 360;

motorA = pup_motor_init(PBIO_PORT_ID_A, PUP_DIRECTION_COUNTERCLOCKWISE);
motorB = pup_motor_init(PBIO_PORT_ID_B, PUP_DIRECTION_CLOCKWISE);

int8_t i;
for (i = 0; i &amp;lt; 10; i++){
pup_motor_reset_count(motorA);

pup_motor_set_speed(motorA, 300);
pup_motor_set_speed(motorB, -300);

while(tread/d_tire*goal &amp;gt; pup_motor_get_count(motorA));

pup_motor_brake(motorA);
pup_motor_brake(motorB);

dly_tsk(500000);

float error = pup_motor_get_count(motorA) - (tread / d_tire * goal);
syslog(LOG_NOTICE, "error: %d.%05d", (int)error, (int)((error - (int)error) * 100000));
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Results: Which Environment Was Most Precise?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi7ux3gfonh3eslrr4kzh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi7ux3gfonh3eslrr4kzh.PNG" alt="Image description" width="800" height="573"&gt;&lt;/a&gt;&lt;br&gt;
Here are the average rotation errors (smaller is better):&lt;br&gt;
1️⃣ 12° - C Language (spike-rt) 🏆&lt;br&gt;
2️⃣ 13° - Python (Pybricks)&lt;br&gt;
2️⃣ 13° - Python (SPIKE App 3)&lt;br&gt;
4️⃣ 14° - Word Blocks (SPIKE App 3)&lt;br&gt;
Additionally, I measured the error fluctuation range (consistency):&lt;br&gt;
1️⃣ 2° - Python (SPIKE App 3) 🏆&lt;br&gt;
2️⃣ 3° - C Language (spike-rt)&lt;br&gt;
2️⃣ 3° - Word Blocks (SPIKE App 3)&lt;br&gt;
4️⃣ 5° - Python (Pybricks)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways:&lt;/strong&gt;&lt;br&gt;
C Language (spike-rt) had the most accurate rotation (smallest error)&lt;br&gt;
Python (SPIKE App 3) had the most stable results (smallest variation)&lt;/p&gt;




&lt;h2&gt;
  
  
  Want to Try C Programming on LEGO SPIKE Prime?
&lt;/h2&gt;

&lt;p&gt;If you’re interested in trying C on SPIKE Prime, there are beginner-friendly learning materials available. As of March 2025, a trial version is also accessible—&lt;a href="https://afrel.stores.jp/?category_id=672db77de533d013abda0a7e" rel="noopener noreferrer"&gt;give it a try!&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  To See More
&lt;/h2&gt;

&lt;p&gt;If you're interested in more LEGO SPIKE Prime experiments with C language, check out this related article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3"&gt;Testing LEGO SPIKE Prime with C: Line Follower Speed &amp;amp; Stability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73"&gt;Introducing SPIKE-RT: the C Language Software Platform for LEGO SPIKE Prime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0"&gt;Comparing LEGO SPIKE Prime Programming : Which is Best for Robotics Competitions? - 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More tests are planned, including further evaluations for robotics competitions. Stay tuned for future updates! 🚀&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Comparing LEGO SPIKE Prime Programming : Which is Best for Robotics Competitions? - 1</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Mon, 10 Mar 2025 05:37:08 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Does Programming Environment Affect Performance? A Comparison for Robotics Competitions&lt;/strong&gt;&lt;br&gt;
When using LEGO SPIKE Prime for a robotics competition, does the programming environment impact performance? I conducted an experiment to compare different programming environments and see how they affect robot movement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tested Programming Environments&lt;/strong&gt;&lt;br&gt;
I compared the following four environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word Blocks (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (SPIKE App 3) → &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;Download here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Python (Pybricks) → &lt;a href="https://pybricks.com/" rel="noopener noreferrer"&gt;More info&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C Language (spike-rt) → &lt;a href="https://github.com/spike-rt/spike-rt" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Test Method&lt;/strong&gt;&lt;br&gt;
To compare the environments, I ran the following test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The robot moves straight and stops when the color sensor detects red&lt;/li&gt;
&lt;li&gt;Travel distance before detection: ~30 cm&lt;/li&gt;
&lt;li&gt;Speed: ~900 deg/s&lt;/li&gt;
&lt;li&gt;Measure the stopping distance (braking distance) from the moment red is detected until the robot stops completely&lt;/li&gt;
&lt;li&gt;Conduct five trials for each environment and calculate the average stopping distance&lt;/li&gt;
&lt;li&gt;The same logic was used for all environments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Program Code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word Blocks (SPIKE App 3)
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54x1yp2aecgz09wx6xlu.PNG" alt="Image description" width="319" height="419"&gt;
&lt;/li&gt;
&lt;li&gt;Python (SPIKE App 3)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from hub import port
import motor,color_sensor,time
import color

motor.run(port.A, -900)
motor.run(port.B, 900)

while color_sensor.color(port.C) != color.RED:
    pass

motor.stop(port.A,stop=motor.BRAKE)
motor.stop(port.B,stop=motor.BRAKE)

time.sleep_ms(1000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Python (Pybricks)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor
from pybricks.parameters import Color, Port, Stop
from pybricks.tools import wait

# Initialize the sensor.
sensor = ColorSensor(Port.C)

# Initialize the motor.
left_motor = Motor(Port.A)
right_motor = Motor(Port.B)

wait(500)

left_motor.run(-900)
right_motor.run(900)
while sensor.color() != Color.RED:
    pass
left_motor.brake()
right_motor.brake()

wait(1000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;C Language (spike-rt)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;kernel.h&amp;gt;

#include &amp;lt;spike/hub/system.h&amp;gt;

#include &amp;lt;stopAtRed.h&amp;gt;

#include "spike/pup/motor.h"
#include "spike/pup/colorsensor.h"

#include &amp;lt;pbio/color.h&amp;gt;

pup_motor_t *motorA;             // Variable for using motor A
pup_motor_t *motorB;             // Variable for using motor B
pup_device_t *ColorSensor;       // Variable for using color sensor

void Main(intptr_t exinf)
{
  // Wait 3 seconds
  dly_tsk(1000000);

  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_set_speed(motorA, 900);
  pup_motor_set_speed(motorB, 900);

  while(pup_color_sensor_color_name(ColorSensor, true) != 'r');

  pup_motor_brake(motorA);
  pup_motor_brake(motorB);

  dly_tsk(1000000);
  // End the program
  exit(0);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Robot Configuration&lt;/strong&gt;&lt;br&gt;
I used the following robot setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Left motor: Port A&lt;/li&gt;
&lt;li&gt;Right motor: Port B&lt;/li&gt;
&lt;li&gt;Color sensor: Port C
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2vr4cnwblvsokn6nbp7.PNG" alt="Image description" width="" height=""&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Results: Which Environment Performed Best?&lt;/strong&gt;&lt;br&gt;
Here are the average stopping distances (shorter is better):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;24mm - C Language (spike-rt) 🏆&lt;/li&gt;
&lt;li&gt;26.6mm - Python (Pybricks)&lt;/li&gt;
&lt;li&gt;27mm - Word Blocks (SPIKE App 3)&lt;/li&gt;
&lt;li&gt;27.2mm - Python (SPIKE App 3)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The C language environment (spike-rt) had the shortest stopping distance, meaning it responded the fastest.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UU_kxfumZp4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Want to Try C Programming on LEGO SPIKE Prime?&lt;/strong&gt;&lt;br&gt;
If you’re interested in trying C on SPIKE Prime, there are beginner-friendly learning materials available. As of March 2025, a trial version is also accessible—&lt;a href="https://afrel.stores.jp/?category_id=672db77de533d013abda0a7e" rel="noopener noreferrer"&gt;give it a try!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To see more&lt;/strong&gt;&lt;br&gt;
If you're interested in more LEGO SPIKE Prime experiments with C language, check out this related article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73"&gt;Introducing SPIKE-RT: The C Language Software Platform for LEGO SPIKE Prime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3"&gt;Testing LEGO SPIKE Prime with C: Line Follower Speed &amp;amp; Stability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe"&gt;Comparing LEGO SPIKE Prime Programming: Which Is Best for Robotics Competitions? - 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More tests are planned, including further evaluations for robotics competitions. Stay tuned for future updates! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introducing SPIKE-RT: The C Language Software Platform for LEGO SPIKE Prime</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Mon, 03 Mar 2025 01:05:50 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73</guid>
      <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;br&gt;
This article introduces SPIKE-RT, a software platform that enables C language programming for LEGO SPIKE Prime. Unlike existing platforms that primarily use MicroPython, SPIKE-RT provides a real-time operating system (RTOS) environment based on TOPPERS/ASP3, allowing for real-time programming and efficient hardware control. This makes it a valuable option for developers who require high performance and precise control in their SPIKE Prime projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is SPIKE-RT?&lt;/strong&gt;&lt;br&gt;
SPIKE-RT is a real-time operating system (RTOS) based software platform designed for LEGO SPIKE Prime, developed as the successor to EV3RT. It is built on TOPPERS/ASP3 RTOS, providing a C language programming environment under the MIT license. SPIKE-RT aims to enable real-time programming and enhance control applications' reliability and performance on LEGO SPIKE Prime.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F819kcsxtgccm1wv0xf4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F819kcsxtgccm1wv0xf4l.png" alt="Image description" width="760" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is TOPPERS/ASP3?&lt;/strong&gt;&lt;br&gt;
TOPPERS/ASP3 is an open-source RTOS targeting embedded systems that require high reliability, safety, and real-time performance. It extends the μITRON 4.0 specification and supports preemptive multitasking with priority-based scheduling. Its architecture is designed to maintain real-time properties while facilitating complex application development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparison with Existing Software Platforms&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LEGO Education SPIKE App: The official platform that provides a programming environment using MicroPython and Scratch. It appears to be based on a MicroPython runtime.&lt;/li&gt;
&lt;li&gt;Pybricks: An open-source platform for LEGO devices that supports programming using MicroPython.
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fckfpk4okte54akhzuqwf.png" alt="Image description" width="760" height="460"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are based on the MicroPython runtime. Compared to SPIKE-RT, the following issues can be considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limitations in Language Extensibility: Existing software assets written in C cannot be reused.&lt;/li&gt;
&lt;li&gt;Constraints in Real-Time Performance: Issues such as garbage collection can affect real-time behavior.
SPIKE-RT enables real-time programming with C, allowing the reuse of existing C software assets and supporting the development of applications with real-time requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Device Support&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  SPIKE-RT supports a wide range of devices in the LEGO SPIKE Prime ecosystem:
&lt;/h2&gt;

&lt;p&gt;Hub Internal Devices: Matrix LED, Single LED, Buttons, Accelerometer/Gyroscope (IMU), and Speaker. While external flash memory is not supported.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PUP (Powered Up) Devices: Color Sensor, Distance Sensor, Force Sensor, and Motors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;User Interface of SPIKE-RT&lt;/strong&gt;&lt;br&gt;
API Support: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TOPPERS/ASP3 API for real-time control&lt;/li&gt;
&lt;li&gt;C Standard Library (Newlib)&lt;/li&gt;
&lt;li&gt;SPIKE API for controlling LEGO SPIKE Prime devices
Communication Interfaces: &lt;/li&gt;
&lt;li&gt;Serial communication through PUP Port F (USART), USB Virtual COM Port, and Bluetooth Serial, all accessible via standard C I/O functions (fprintf()/fscanf()).
Development Environment: &lt;/li&gt;
&lt;li&gt;Compiler: GCC&lt;/li&gt;
&lt;li&gt;Build System: GNU Make&lt;/li&gt;
&lt;li&gt;Programming Method: USB DFU
SPIKE-RT's user interface provides a flexible and familiar development environment for C programmers, supporting real-time device control and communication.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;To see more&lt;/strong&gt;&lt;br&gt;
If you want to see a demonstration of SPIKE-RT in action, please refer to the following.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3"&gt;Testing LEGO SPIKE Prime with C: Line Follower Speed &amp;amp; Stability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0"&gt;Comparing LEGO SPIKE Prime Programming : Which is Best for Robotics Competitions? - 1&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br&gt;
This article is based on information from &lt;a href="https://github.com/spike-rt/materials/tree/main/2023-03-11_OSC2023OnlineSpring" rel="noopener noreferrer"&gt;https://github.com/spike-rt/materials/tree/main/2023-03-11_OSC2023OnlineSpring&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>lego</category>
      <category>c</category>
    </item>
    <item>
      <title>Testing LEGO SPIKE Prime with C: Line Follower Speed &amp; Stability</title>
      <dc:creator>砂田沙耶</dc:creator>
      <pubDate>Wed, 05 Feb 2025 04:17:48 +0000</pubDate>
      <link>https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3</link>
      <guid>https://dev.to/_ff41734170f7cc70ac79/testing-lego-spike-prime-with-c-line-follower-speed-stability-cp3</guid>
      <description>&lt;p&gt;I tried controlling the LEGO SPIKE Prime using C language to evaluate its usability, processing speed, and potential for robot competitions. To test its performance, I implemented a Line Follower algorithm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fasedsb61b1q74pwolq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fasedsb61b1q74pwolq.jpg" alt="Image description" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Key Experiments
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
1.Comparing the Line Follower performance using SPIKE App’s Word Blocks vs. C language&lt;br&gt;
2.Maximizing Line Follower speed using C language with PD control&lt;br&gt;
**&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Comparing SPIKE App Word Blocks vs. C Language
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
I created a Line Follower program using both &lt;a href="https://education.lego.com/en-us/downloads/spike-app/software/" rel="noopener noreferrer"&gt;SPIKE App&lt;/a&gt; (Word Blocks) and C language, keeping the control logic the same.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The C language version provided more stable driving performance compared to the SPIKE App.&lt;/li&gt;
&lt;li&gt;Watch the comparison video here: [&lt;a href="https://youtu.be/_JQyYQwK8VY" rel="noopener noreferrer"&gt;Video Link&lt;/a&gt;]&lt;/li&gt;
&lt;li&gt;Below is the source code for both versions:
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffhiacyptbfi5we4pj264.jpeg" alt="Image description" width="" height=""&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jx4xrqanou5beo3t4jz.jpeg" alt="Image description" width="643" height="746"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Improving Line Follower Speed with C
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
I tested how much I could increase the Line Follower speed using C language.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Watch the actual test video: [&lt;a href="https://youtu.be/agrynJIESm4" rel="noopener noreferrer"&gt;Video Link&lt;/a&gt;]&lt;/li&gt;
&lt;li&gt;Without extensive tuning, I achieved speeds of approximately 820 deg/s.&lt;/li&gt;
&lt;li&gt;Adding a second color sensor or optimizing the robot assembly could further improve stability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Development Environment &amp;amp; Resources
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
For C programming, I used this development environment: [&lt;a href="https://github.com/spike-rt/spike-rt" rel="noopener noreferrer"&gt;spike-rt&lt;/a&gt;]&lt;br&gt;
If you want to try it, there is a &lt;a href="https://afrel.stores.jp/?category_id=672db77de533d013abda0a7e" rel="noopener noreferrer"&gt;beginner-friendly tutorial&lt;/a&gt; available, including a trial (as of February 2025).&lt;/p&gt;

&lt;p&gt;🔹 Download the program used in this article: [&lt;a href="https://drive.google.com/file/d/1qzKc1vD3q1tA_p45kFDABMdW5jR_QjLy/view?usp=drive_link" rel="noopener noreferrer"&gt;Download Link&lt;/a&gt;]&lt;/p&gt;

&lt;h2&gt;
  
  
  To see more
&lt;/h2&gt;

&lt;p&gt;If you want to see a demonstration of SPIKE-RT in action, please refer to the following.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/introducing-spike-rt-the-c-language-software-platform-for-lego-spike-prime-4b73"&gt;Introducing SPIKE-RT: the C Language Software Platform for LEGO SPIKE Prime&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-1-29l0"&gt;Comparing LEGO SPIKE Prime Programming : Which is Best for Robotics Competitions? - 1&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-2-3pb1"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-3-20h1"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/_ff41734170f7cc70ac79/comparing-lego-spike-prime-programming-which-is-best-for-robotics-competitions-4-color-48pe"&gt;Comparing LEGO SPIKE Prime Programming: Which is Best for Robotics Competitions? - 4&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Future Tests &amp;amp; Robot Competition Applications&lt;br&gt;
I plan to conduct further tests on the usefulness of C language for robot competitions and will share my findings in upcoming articles. Stay tuned!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>lego</category>
      <category>c</category>
    </item>
  </channel>
</rss>
