Cubelets API Documentation 2.0
C API
Loading...
Searching...
No Matches
drive.c File Reference

Detailed Description

Bi-Directional

#include "cubelet.h"
void setup()
{
}
void loop()
{
block_value = weighted_average();
unsigned int motor_speed;
if (block_value < 128){ //reverse
set_drive_direction(BACKWARD);
motor_speed = 2 * (127 - block_value);
}
else { //forward
set_drive_direction(FORWARD);
motor_speed = 2 * (block_value - 128);
}
set_drive(motor_speed);
}
uint8_t weighted_average(void)
Calculates a block value based on the on the weighted average of neighbors block values.
void setup()
Function ran just a single time. Used for setting up variables or timers.
Definition bargraph.c:3
void loop()
The loop() function gets called repeatedly while a Cubelet is powered on.
Definition bargraph.c:8
#define FORWARD
Definition motor.h:14
#define BACKWARD
Definition motor.h:19

Easer

#include "cubelet.h"
//Implements a BUF_SIZE value buffer to utilize a boxcar filter of 400ms window.
#define BUF_SIZE 8 //number of block values to remember for the boxcar filter
uint8_t block_values[BUF_SIZE] = {0};
uint8_t counter = 0;
void setup()
{
}
void loop()
{
//add the current value into the buffer and update the counter
block_values[counter] = weighted_average();
counter++;
if (counter >= BUF_SIZE) counter = 0;
//computes current running average
int current_running_average = 0;
for (unsigned int i=0; i<BUF_SIZE; i++){
current_running_average += block_values[i];
}
current_running_average /= BUF_SIZE;
set_drive(current_running_average);
wait(50); //updates approximately 20 times per second
}
void wait(uint16_t delay)
Function to delay execution for a specified amount of time.

Quiver

#include "cubelet.h"
#include <stdbool.h>
bool motor_forward = true;
void setup()
{
}
void loop()
{
block_value = weighted_average();
if (motor_forward) {
set_drive_direction(FORWARD);
motor_forward = false;
}
else {
set_drive_direction(BACKWARD);
motor_forward = true;
}
set_drive(block_value);
wait(100+3*(255-block_value)); //wait between approximately 100 and 850 ms to switch directions again, depending on block value
}

Reverse

#include "cubelet.h"
void setup()
{
set_drive_direction(BACKWARD);
}
void loop()
{
block_value = weighted_average();
set_drive(block_value);
}

Scurry

#include "cubelet.h"
#include <stdbool.h>
bool is_moving = false;
void setup()
{
}
void loop()
{
unsigned int current_weighted_average = weighted_average();
if (is_moving){
//probability of stopping is 0% for full weighted average and almost 100% for zero weighted average
if (rand()%255 > current_weighted_average){
is_moving = false;
}
}
else {
//probability of starting is 0% for zero weighted average and almost 100% for full weighted average
if (rand()%255 < current_weighted_average){
is_moving = true;
}
}
if (is_moving) set_drive(255);
else set_drive(0);
wait(100);
}