Skip to content

Teensy 3.5, low fps with 2400 ws2812b's #418

Closed

Description

@tehuster

Hi fastled guru's,

I'm not sure if this is the right place to drop this question so excuse me if it's not.

I'm currently working on a game involving:

  • 2400 ws2812b leds in 4 strings of 10m
  • hooked up on a teensy 3.5. port 2,3,4,5.
  • This is powered by a beefy power supply branched out at the start of the strip and after 5m.
  • I'm using FastLED 3.1 i think..

The problem is the low fps, @ 4x50 led's it's silkey smooth, but @ 4x250 or higher it's already very slow. @ 4x600 it's like 1 fps or slower.

I've searched the internet far and wide and i came across multiple reason what could be the problem:

  • not enough SRAM, however i thought the teensy 3.5 has plenty for this amount of leds.
  • fastled does not support teensy 3.5 thus not enabling the full power of the mighty teensy! (or something)
  • quoting @focalintent:

It takes 30µs to write out a single led's worth of data, so the absolute maximum number of rgb led updates per second that you can do is about 33,000 (or, if you're doing 8-way parallel output 260,000ish). Of course, that's assuming spending 100% of your time writing led data.

so the bottleneck is in the led itself?

  • I need to power the ledstrip more often, every 1m or so.
  • My code has to many floating point calculation (i don't think so because if i disable them my fps is still low) (i will put in my code)

As you might have guessed my knowledge is not comprehensive enough yet to debug this problem.

So could you help me out by figuring out what my bottleneck is and what i need to do/buy to fix this problem. All the tips are welcome i wanna learn as much as possible, also code optimalisation is very welcome. Thanks!

(code is work is progress)

#include <FastLED.h>;

//////////////////////////////////////// DEFINES

#define BUTTONPINP1LEFTY 27
#define BUTTONPINP1RIGHTY 26
#define BUTTONPINP2LEFTY 9
#define BUTTONPINP2RIGHTY 8
#define BUTTONPINP3LEFTY 11
#define BUTTONPINP3RIGHTY 10
#define BUTTONPINP4LEFTY 25
#define BUTTONPINP4RIGHTY 24

#define LED1 5
#define LED2 4
#define LED3 3
#define LED4 2

#define LEFTY 0
#define RIGHTY 1
#define LASTLEFTY 2
#define LASTRIGHTY 3

#define VELOCITY 0
#define FLOATPOS 1
#define SPEED 2
#define ACCELERATION 3

#define LANE 0
#define POS 1
#define OLDPOS 2

///////////////////////////////////////// redOrbInfo

int redOrbAmount = 8;
float redOrbMov[8][2]; // 0 VELOCITY  1 FLOATPOS
int redOrbPos[8][2];   // 0 LANE      1 POS
int direction = 1;
const int orbminspeed= 15;
const int orbmaxspeed= 30;

const int NUM_LEDS = 50;

////////////////////////////////////////  playerInfo

const int playerButtonPins[4][2] = {
    {BUTTONPINP1LEFTY, BUTTONPINP1RIGHTY},
    {BUTTONPINP2LEFTY, BUTTONPINP2RIGHTY},
    {BUTTONPINP3LEFTY, BUTTONPINP3RIGHTY},
    {BUTTONPINP4LEFTY, BUTTONPINP4RIGHTY}};

int playerAmount = 4;
boolean playerButtonState[4][4]; // 0 LEFTY      1 RIGHTY    2 LASTLEFTY   3 LASTRIGHTY
float playerMov[4][4];           // 0 VELOCITY  1 FLOATPOS 2 SPEED   3 ACCELERATION
int playerPos[4][3];             // 0 LANE      1 POS      2 OLDPOS
float friction = 0.99;           // Not needed when fixed speed..

float knockback = -1;
float defaultSpeed = 0.25;
float defaultAcceleration = 0.025; //can be used for variable speed
float boost = 0.05;

/////////////////////////////////////////  updateInfo

unsigned long previousMillis = 0;
const int interval = 5;

/////////////////////////////////////////  ledInfo
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
CRGB leds4[NUM_LEDS];

/////////////////////////////////////////////////////////////////// SETUP

void setup()
{
    delay(3000);
    //Serial.begin(9600);
    setupLEDS();
    setupInput();
    createPlayers();
    createOrbs();
}

/////////////////////////////////////////////////////////////////// LOOP

void loop()
{
    FastLED.clear();
    //readInput();  //Can also be added to gameLoop
    updateGame();
    FastLED.show();
}

/////////////////////////////////////////////////////////////////// gameLoop

void updateGame()
{
    unsigned long currentMillis = micros();
    if (currentMillis - previousMillis >= interval)
    {
        gameLoop();
        previousMillis = currentMillis;
    }
}

void gameLoop() //MOVE DETECT RE-MOVE?
{
    for (int redOrbsX = 0; redOrbsX < redOrbAmount; redOrbsX++)
    {
        moveOrb(redOrbsX);
        for (int redOrbsY = 0; redOrbsY < redOrbAmount; redOrbsY++)
        {
            hitDetectionOrb(redOrbsX, redOrbsY);
        }
        updateOrb(redOrbsX);
    }
    for (int playerX = 0; playerX < playerAmount; playerX++)
    {
        movePlayer(playerX);
        for (int playerY = 0; playerY < playerAmount; playerY++)
        {
            hitDetectionPlayer(playerX, playerY);
        }
        for (int redOrbs = 0; redOrbs < redOrbAmount; redOrbs++)
        {
            hitDetectionPlayerOrb(playerX, redOrbs);
        }
        updatePlayer(playerX);
    }
}

/////////////////////////////////////////////////////////////////// INPUT

void setupInput()
{
    for (int i = 0; i < playerAmount; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            pinMode(playerButtonPins[i][j], INPUT);
        }
    }
}

void readInput()
{
    laneChangeInput();
}

void laneChangeInput()
{
    for (int i = 0; i < playerAmount; i++) //0 LEFTY  1 RIGHTY 2 LASTLEFTY 3 LASTRIGHTY
    {
        playerButtonState[i][LEFTY] = digitalRead(playerButtonPins[i][LEFTY]);
        playerButtonState[i][RIGHTY] = digitalRead(playerButtonPins[i][RIGHTY]);

        if (playerButtonState[i][LEFTY] != playerButtonState[i][LASTLEFTY])
        {
            if (playerButtonState[i][LEFTY] == HIGH)
            {
                changePlayerLane(i, LEFTY);
            }
        }
        playerButtonState[i][LASTLEFTY] = playerButtonState[i][LEFTY];
        if (playerButtonState[i][RIGHTY] != playerButtonState[i][LASTRIGHTY])
        {
            if (playerButtonState[i][RIGHTY] == HIGH && playerPos[i][LANE] != 3)
            {
                changePlayerLane(i, RIGHTY);
            }
        }
        playerButtonState[i][LASTRIGHTY] = playerButtonState[i][RIGHTY];
    }
}

/////////////////////////////////////////////////////////////////// MOVEMENT

void moveOrb(int orbIndex)
{
    redOrbMov[orbIndex][FLOATPOS] += redOrbMov[orbIndex][VELOCITY];
    if (redOrbMov[orbIndex][FLOATPOS] > NUM_LEDS)
    {
        redOrbMov[orbIndex][FLOATPOS] = 0.0;
    };
    redOrbPos[orbIndex][POS] = (int)(redOrbMov[orbIndex][FLOATPOS] + 0.5);
}

void movePlayer(int index) // not perfect, debug and rework
{
    playerPos[index][OLDPOS] = playerPos[index][POS];

    if (playerMov[index][VELOCITY] < playerMov[index][SPEED])
    {
        playerMov[index][VELOCITY] += playerMov[index][ACCELERATION]; //Can use fixed acceleration to save memory
    }
    playerMov[index][VELOCITY] = playerMov[index][VELOCITY] * friction;
    playerMov[index][FLOATPOS] += playerMov[index][VELOCITY];
    if (playerMov[index][FLOATPOS] > NUM_LEDS)
    {
        playerMov[index][FLOATPOS] -= NUM_LEDS;
        //playerMov[index][FLOATPOS] = 0.0;
    }
    else if (playerMov[index][FLOATPOS] < 0.0)
    {
        playerMov[index][FLOATPOS] += NUM_LEDS;
    }
    playerPos[index][POS] = (int)(playerMov[index][FLOATPOS] + 0.5);
}

void changePlayerLane(int player, int direction) // fcking moe <- check again please
{
    if (direction == RIGHTY && playerPos[player][LANE] != 3)
    {
        playerPos[player][LANE]++;
    }
    else if (direction == LEFTY && playerPos[player][LANE] != 0)
    {
        playerPos[player][LANE]--;
    }
};

int isLaneEmpty(int player, int direction) //fcking moe <- check again please
{
    for (int i = 0; i < playerAmount; i++)
    {
        if (player != i)
        {
            if ((playerPos[player][LANE] + 1) != playerPos[i][LANE])
            {
                return 4;
            }else{
                return i;
            }
        }
    }
}

void changeOrbLane(int orb)
{
    if (redOrbPos[orb][LANE] == 0)
    {
        redOrbPos[orb][LANE]++;
    }
    else if (redOrbPos[orb][LANE] == 3)
    {
        redOrbPos[orb][LANE]--;
    }
    else
    {
        direction = direction * -1;
        redOrbPos[orb][LANE] += direction;
    }
};

/////////////////////////////////////////////////////////////////// HIT DETECTION

void hitDetectionOrb(int orbX, int orbY)
{
    if (orbX != orbY)
    {
        if (redOrbPos[orbX][LANE] == redOrbPos[orbY][LANE] && redOrbPos[orbY][POS] == (redOrbPos[orbX][POS] - 1))
        {
            if (redOrbMov[orbX][VELOCITY] > redOrbMov[orbY][VELOCITY])
            {
                redOrbPos[orbX][POS]++; //but why?
            }
            else
            {
                changeOrbLane(orbY); //please debug this
            }
        }
    }
}

void hitDetectionPlayer(int pX, int pY)
{
    if (pX != pY)
    {
        if (playerPos[pX][OLDPOS] <= playerPos[pY][POS] && playerPos[pX][POS] > playerPos[pY][POS])
        {
            if (playerPos[pX][LANE] == playerPos[pY][LANE])
            {
                playerMov[pX][VELOCITY] += knockback;
                playerMov[pX][SPEED] = defaultSpeed;
                playerMov[pY][SPEED] = defaultSpeed;
                movePlayer(pX); //?
            }
        }
    }
}

void hitDetectionPlayerOrb(int player, int orb) //Add detection from behind.
{
    if (playerPos[player][OLDPOS] <= redOrbPos[orb][POS] && playerPos[player][POS] > redOrbPos[orb][POS])
    {
        if (playerPos[player][LANE] != redOrbPos[orb][LANE])
        {
            playerMov[player][SPEED] += boost;
        }
        else
        {
            playerMov[player][VELOCITY] += knockback;
            playerMov[player][SPEED] = defaultSpeed;
            movePlayer(player); //?
        }
    }
}

/////////////////////////////////////////////////////////////////// UPDATE LEDS

void updatePlayer(int index)
{
    int currentColor;
    switch (index)
    {
    case 0:
        currentColor = CRGB::Blue;
        break;
    case 1:
        currentColor = CRGB::Purple;
        break;
    case 2:
        currentColor = CRGB::Green;
        break;
    case 3:
        currentColor = CRGB::Yellow;
        break;
    }

    switch (playerPos[index][LANE])
    {
    case 0:
        leds1[playerPos[index][POS]] = currentColor;
        break;
    case 1:
        leds2[playerPos[index][POS]] = currentColor;
        break;
    case 2:
        leds3[playerPos[index][POS]] = currentColor;
        break;
    case 3:
        leds4[playerPos[index][POS]] = currentColor;
        break;
    }
}

void updateOrb(int index)
{
    switch (redOrbPos[index][LANE])
    {
    case 0:
        leds1[redOrbPos[index][POS]] = CRGB::Red;
        break;
    case 1:
        leds2[redOrbPos[index][POS]] = CRGB::Red;
        break;
    case 2:
        leds3[redOrbPos[index][POS]] = CRGB::Red;
        break;
    case 3:
        leds4[redOrbPos[index][POS]] = CRGB::Red;
        break;
    }
}

/////////////////////////////////////////////////////////////////// CREATE

void createPlayers()
{
    for (int i = 0; i < playerAmount; i++)
    {
        playerMov[i][VELOCITY] = 0.0;
        playerMov[i][FLOATPOS] = 0.0;
        playerMov[i][SPEED] = defaultSpeed;
        playerMov[i][ACCELERATION] = defaultAcceleration;
        playerPos[i][LANE] = i;
        playerPos[i][POS] = 0;
        playerPos[i][OLDPOS] = 0;
    }
}

void createOrbs()
{
    for (int i = 0; i < redOrbAmount; i++)
    {
        int lane = random(0, 4);
        float posFloat = random(0, NUM_LEDS);
        float velocity = random(orbminspeed, orbmaxspeed);
        velocity = velocity / 100;
        redOrbPos[i][LANE] = lane;
        redOrbMov[i][VELOCITY] = velocity;
        redOrbMov[i][FLOATPOS] = posFloat;
    }
}

/////////////////////////////////////////////////////////////////// SETUP

void setupLEDS()
{
    FastLED.addLeds<NEOPIXEL, LED1>(leds1, NUM_LEDS);
    FastLED.addLeds<NEOPIXEL, LED2>(leds2, NUM_LEDS);
    FastLED.addLeds<NEOPIXEL, LED3>(leds3, NUM_LEDS);
    FastLED.addLeds<NEOPIXEL, LED4>(leds4, NUM_LEDS);
}

Activity

  1. focalintent commented on Mar 3, 2017

    @focalintent
    Contributor

    It's pretty straightforward math.

    Every time you call FastLED.show it will take 72ms (30us per led for 2400 leds) to write out the led data - which means no more than 13fps. With only 200 leds it only takes 6ms, which in theory could get you well over 150fps.

    I don't know if the person who did the teensy 3.5/3.6 port did the parallel output support but that would pay least let you get 40-50fps.

    I want to keep issues limited to bug report, not general discussion/help - and prefer the much larger g+ community (http://fastled.io/+) for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions