U8glib slow simple test program on oled - c

I'm trying to use u8glib on an arduino ethernet, in the first phase i need to display a simple countdown so in a little pretty way. To test if the display can be managed in time.
I have to measure changes in order of milliseconds, likes 30ms, therefore i need that the countdown changes 1 units in about 50/100ms.
This is the oled http://www.seeedstudio.com/wiki/Grove_-_OLED_Display_1.12%22
Now im trying U8glib because has a lot of font integrated and it seems well documented.
But the big problem is: The countdown, without sleep obiuvsly, is too slow! In order of 1 change in 1second.
What i'm wrong? I need to change library?
Thanks
This is the code
#include "U8glib.h"
U8GLIB_SSD1327_96X96_GR u8g(U8G_I2C_OPT_NONE); // I2C
#define MAX_VAL 200
#define MAX_VAL1 250
#define MAX_VAL2 350
#define MAX_VAL3 450
#define MAX_VAL4 500
int current_max_val = MAX_VAL1;
int currentBB = current_max_val;
char bb_n[4] = {0,0,0,0};
int bb_level = 4;
static int last_bb_n = current_max_val;
void draw(void) {
int sizev = u8g.getStrWidth(bb_n);
u8g.drawStr( (48 - (sizev/2)),60,bb_n);
for(int i=0;i<bb_level;i++){
u8g.drawTriangle((i*10)+0,90, (i*10)+10,90, (i*10)+5,85);
}
last_bb_n = currentBB;
u8g.drawHLine(0,20, 96);
u8g.drawHLine(0,70, 96);
}
void setup(void) {
// flip screen, if required
//u8g.setRot180();
// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
u8g.setFont(u8g_font_gdb30);
}
void loop(void) {
// picture loop
u8g.firstPage();
count();
do {
draw();
} while( u8g.nextPage() );
}
void count(){
if(currentBB == 0){
currentBB = current_max_val;
}
itoa(currentBB,bb_n,10);
if(currentBB >= ((current_max_val/4)*3)){
bb_level = 4;
}else if((currentBB < ((current_max_val/4)*3)) && (currentBB >= (current_max_val/4)*2)){
bb_level = 3;
}else if((currentBB < ((current_max_val/4)*2)) && (currentBB >= (current_max_val/4))){
bb_level = 2;
}else if(currentBB < (current_max_val/4)){
bb_level = 1;
}else{
bb_level = 0;
}
currentBB--;

Some ideas for optimization:
Use U8GLIB_SSD1327_96X96_2X_GR
Make sizev a global variable and calculate its value in count()
Oliver

Related

PID Line follower with tank treads

I have made a (pretty bad) line follower.
Here is a sketch to roughly know the shape of the robot and location of the treads and sensors
[-] 0 0 [-] // 0 = color sensor
[-]------[-] // - = robot body
[-]------[-] // [-] = tank tread
[-] [-]
Here's what it does:
get Red, Green & Blue, make average of sensor 1 readings, do the same for 2
subtract to get value
this value will go through the PID part
steer with calculated steering
repeat (all of this is in a loop)
I use RGB and not reflected intensity (which is what is commonly used), because sometimes I need to detect if there's green color under the sensor (if there is, turn).
The real problem comes with the steering part. Unfortunately, it only accelerates a motor, meaning that in very tight turns we just lose the line.
Optimally, it should compensate a bit with the other motor (maybe going in the other direction?), but I am not sure how to calculate the speed of the motor, nor how to enforce this very strict line following policy.
Here is the code (I am also very grateful for any kind of tips on how to clean up the code! This is my first project in C :D ). I am not asking to read it all (it is pretty long), you could also just look at the steering function, and work your way back to rawFollowLine, this should hopefully shorten the code.
void rawFollowLine(int speed, float Kp, float Ki, float Kd){
_checkInit();
set_sensor_mode(sn_lx_color, "RGB-RAW");
set_sensor_mode(sn_rx_color, "RGB-RAW");
//printAllSensors();
int wasBlackCounter = 0;
int wasBlack = 0;
int lastBlack = 0;
for (int i = 0; i < 2000; i++)
{
if (isTerminating == 1)
{
killMotors(0);
break;
}
int greenCheck = rawGreenCheck(&wasBlack, &wasBlackCounter, &lastBlack);
if (wasBlack == 1){
wasBlackCounter++;
if (wasBlackCounter > 50){
wasBlackCounter = 0;
wasBlack = 0;
}
}
if (greenCheck == 1)
{
// lx is green
killMotors(1);
usleep(400 * 1000);
drive(200, 70);
waitIfMotorIsRunning();
killMotors(1);
pivotTurn(-90);
}
else if (greenCheck == 2)
{
// rx is green
killMotors(1);
usleep(400 * 1000);
drive(200, 70);
waitIfMotorIsRunning();
killMotors(1);
pivotTurn(90);
}
else if (greenCheck == 3)
{
// both rx and lx are green
killMotors(1);
turn(180);
}
else if (greenCheck == 5)
{
if(lastBlack == 2)
{
lastBlack = 0;
drive(100, -200);
//pivotTurn(50);
}
else if (lastBlack == 1)
{
lastBlack = 0;
drive(100, -200);
//pivotTurn(-50);
} else {
pidLineRaw(speed, Kp, Ki, Kd, &lastBlack);
}
}
else
{
pidLineRaw(speed, Kp, Ki, Kd, &lastBlack);
}
}
killMotors(1);
}
int rawGreenCheck(int *wasBlack, int *wasBlackCounter, int *lastBlack)
{
// Some documentation
// return nums:
// 3 = double green
// 2 = right green
// 1 = left green
// 0 = no green
int lx_red;
int lx_green;
int lx_blue;
int rx_red;
int rx_green;
int rx_blue;
get_sensor_value(0, sn_lx_color, &lx_red);
get_sensor_value(0, sn_rx_color, &rx_red);
get_sensor_value(1, sn_lx_color, &lx_green);
get_sensor_value(1, sn_rx_color, &rx_green);
get_sensor_value(2, sn_lx_color, &lx_blue);
get_sensor_value(2, sn_rx_color, &rx_blue);
//printf("rx_red %d\n", rx_red);
rx_red = (rx_red * rx_ratio_r);
rx_green = (rx_green * rx_ratio_g);
rx_blue = (rx_blue * rx_ratio_b);
//printf("rx_red (again) %d\n", rx_red);
if(
lx_red < 55 &&
lx_green > 90 &&
lx_blue < 55 &&
rx_red < 55 &&
rx_green > 90 &&
rx_blue < 55
)
{
// rx and lx see green
if (*wasBlack == 1)
{
// Apparently we crossed an intersection!
printf("Apparently we crossed an intersection!\n");
// We need to go straight.
*wasBlack = 0;
*wasBlackCounter = 0;
return 0;
}
else
{
return 3;
}
}
else if(lx_red < 55 && lx_green > 90 && lx_blue < 55)
{
// lx sees green
return 1;
}
else if(rx_red < 55 && rx_green > 90 && rx_blue < 55)
{
// rx sees green
return 2;
}
else if(rx_red < 50 && rx_green < 50 && rx_blue < 50 && lx_red < 50 && lx_green < 50 && lx_blue < 50)
{
// rx and lx see black
// this is needed if the intersection has the green tiles after the black line
printf("We are on the line? Is this an intersection?\n");
*wasBlack = 1;
return 0;
}
else if(lx_red < 55 && lx_green < 55 && lx_blue < 55)
{
// lx = right sees black
// this is needed if the intersection has the green tiles after the black line
//printf("We are on the line? Is this an intersection?\n");
killMotor(1, motor[R]);
rotateTillBlack(motor[L], sn_rx_color);
//printf("ASS2\n");
return 0;
}
else if(rx_red < 55 && rx_green < 55 && rx_blue < 55)
{
// rx = left sees black
killMotor(1, motor[L]);
rotateTillBlack(motor[R], sn_lx_color);
//printf("ASS1\n");
return 0;
}
//*lx_color_status = 0;
//*rx_color_status = 0;
*lastBlack = 0;
return 0;
}
void pidLineRaw(int speed, float Kp, float Ki, float Kd, int *lastBlack)
{
int red_lx_color;
int red_rx_color;
int green_lx_color;
int green_rx_color;
int blue_lx_color;
int blue_rx_color;
int lx_color;
int rx_color;
int last_error = 0;
int integral = 0;
int derivative = 0;
//float Kp = 0.1;
//float Ki = 0;
//float Kd = 0;
//set_sensor_mode(sn_lx_color, "COL-REFLECT");
//set_sensor_mode(sn_rx_color, "COL-REFLECT");
get_sensor_value(0, sn_lx_color, &red_lx_color);
get_sensor_value(0, sn_rx_color, &red_rx_color);
get_sensor_value(1, sn_lx_color, &green_lx_color);
get_sensor_value(1, sn_rx_color, &green_rx_color);
get_sensor_value(2, sn_lx_color, &blue_lx_color);
get_sensor_value(2, sn_rx_color, &blue_rx_color);
lx_color = (red_lx_color + green_lx_color+ blue_lx_color)/3;
rx_color = ( (red_rx_color*rx_ratio_r) + (green_rx_color*rx_ratio_g) + (blue_rx_color*rx_ratio_b))/3;
if(*lastBlack == 0)
{
int error = lx_color - rx_color;
integral = integral + error;
derivative = error - last_error;
last_error = error;
int steering_val = (error * Kp) + (integral * Ki) + (derivative * Kd);
// printf("error: %d\nsteering: %d\n",error, steering_val);
move_steering(-steering_val, speed, 1, 0);
} else if (*lastBlack == 1)
{
printf("lx_color_status\n");
move_steering(35, speed, 1, 0);
move_steering(-2, speed, 1, 0);
}
else if (*lastBlack == 2)
{
printf("rx_color_status\n");
move_steering(-35, speed, 1, 0);
move_steering(2, speed, 1, 0);
}
else
{
printf("HMMM: %d\n", *lastBlack);
exit(666);
}
}
static void _getSteeringSpeed(int speed, int *lx_speed, int *rx_speed, int steering)
{
if(steering > 100 || steering < -100)
{
printf("Yo wtf steering is %d\n", steering);
}
else
{
int speed_factor = (50 - abs(steering)) / 50;
*lx_speed = speed;
*rx_speed = speed;
if(steering >= 0)
{
*rx_speed = *rx_speed * speed_factor;
}
else
{
*lx_speed = *lx_speed * speed_factor;
}
}
}
Some parts are omitted, yes, they are not required to solve the problem.
I am also extremely sorry as there might be unused variables and such. I am working on refactoring the project, I'll update the post when I'm done.
So, summing everything up, I need to make sure that the steering part properly turns and follows the line. How do I do that? Is the code that I wrote even suitable? I'm guessing the steering itself might need some sort of feedback loop, to check if it's on the line?

Incorrect values being returned from an Array of Struct - Arduino

I am having a problem that I asked here but as the problem has changed somewhat, as has the code, I am starting a new question
I am trying to read and write values from an array of struct via the function
void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
The values I am getting from the Serial.print are not those stored in the array. Specifically the one for endLed, which should never be 0
Output from Serial
Breeze Module
Start 8 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 1 End 0
Breeze Module
Start 4 End 7
These are defined as
// Define each display's LED range
{
displayUnit[0].startLed=0;
displayUnit[0].endLed=3;
displayUnit[1].startLed=4;
displayUnit[1].endLed=7;
displayUnit[2].startLed=8;
displayUnit[2].endLed=11;
displayUnit[3].startLed=12;
displayUnit[3].endLed=15;
}
Also the program seems to freze after a while.
Any help as to why I am reading the wrong values would be appreciated.
Full Code
/* **********************************
Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//
/* **********************************
Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
void InitPixelStates(struct_displayType& mDisplayUnit)
{
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
leds[i]=BASE_COLOR;
}
}
void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
Serial.println("Breeze Module");
Serial.print("Start ");
Serial.print(mDisplayUnit.startLed);
Serial.print(" End " );
Serial.println(mDisplayUnit.endLed);
for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
Serial.print(" pixel state " );
Serial.println(mDisplayUnit.pixelState[i] );
Serial.println(GettingBrighter);
Serial.println(GettingDimmerAgain);
Serial.println(SteadyDim);
if ( mDisplayUnit.pixelState[i] == SteadyDim) {
// this pixels is currently: SteadyDim
// so we randomly consider making it start getting brighter
leds[i]=BASE_COLOR;
if ( random8() < breezeStrength) {
mDisplayUnit.pixelState[i] = GettingBrighter;
}
} else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
Serial.println("Brighter " + i);
// this pixels is currently: GettingBrighter
// so if it's at peak color, switch it to getting dimmer again
if ( leds[i] >= PEAK_COLOR ) {
mDisplayUnit.pixelState[i] = GettingDimmerAgain;
} else {
// otherwise, just keep brightening it:
leds[i] += DELTA_COLOR_UP;
}
} else { // getting dimmer again
Serial.println("Dimmer " + i);
// this pixels is currently: GettingDimmerAgain
// so if it's back to base color, switch it to steady dim
if ( leds[i] <= BASE_COLOR ) {
leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
mDisplayUnit.pixelState[i] = SteadyDim;
} else {
// otherwise, just keep dimming it down:
leds[i] -= DELTA_COLOR_DOWN;
}
}
}
}

7 Segment isn't working properly

I'm having some troubles about my 7 segment 2 digit counter. I'm using a PIC16F877A with 20Mhz crystal. I'm programming my pic using a replica PICKIT 3 and MPLAB IDE.
Circuit
When i write 02,03,04,04...06 segment displaying it like 88 but 2. Digit C and F segments are not bright as other segments.
02
When i write 01,07,10, everything is normal
My code:
#include <16F877A.h>
#use delay(clock=20m)
#define Dig2 PIN_A0
#define Dig1 PIN_A3
#define rfid PIN_A1
#define reset PIN_A2
#use fast_io(b)
#use fast_io(a)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
//static const int digit[10]= { 0b0111111, 0b0000110, 0b1011011, 0b1001111,0b1100110,0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1101111 };// anode
static const int digit[10]= { 0b1000000, 0b1111001, 0b0100100, 0b0110000,0b0011001,0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000 };//cathode
//SECOND CODE
void display(unsigned char value)
{
int onlar = value / 10;
int birler = value % 10;
output_low(Dig2);
output_high(Dig1);
output_b(digit[onlar]);
delay_ms(5);
output_low(Dig1);
output_high(Dig2);
output_b(digit[birler]);
delay_ms(5);
}
/* FIRST CODE
// Written by Michaƫl Roy
void display(unsigned char value)
{
static char tens = 0;
char dig = (tens) ? (value / 10) : (value % 10);
dig = digit[dig];
output_high((tens) ? Dig1 : Dig2);
output_b(dig);
output_low((tens) ? Dig2 : Dig1);
tens = !tens;
} */
void main()
{
char sayi = 0;
set_tris_b(0b10000000);
set_tris_a(0b11111010);
while(1)
{
display(sayi);
if(input(rfid) == 0)
{
sayi++;
while(input(rfid) == 0)
{
display(sayi);
}
if (sayi == 100)
{
sayi = 0 ;
}
}
if(input(reset) == 0)
{
delay_ms(3000);
if(input(reset) == 0)
{
sayi = 0;
}
}
}
}
How can I solve this?

Go to a line number in embedded C outside of the function

Is it possible to call a line number in embedded C that is outside of the function?
Simple goto label commands can't work because I am in the if statement loop: I want to restart the function once the error is greater than 200.
void followWall(void)
{
begin:
int desired = getADC(); //store the first ADC reading as the desired distance from the wall
int right = 200; //pre set all variables to be called in the drive function
int left = 200; //ints are needed here for bit masking later on to stop overflow
char rightH = 0;
char leftH = 0;
float k = 0.5; //constant to times the error to fine tune the response
drive(rightH,right,leftH,left); //drive the robot to the pre set variables
while( bumpFlag == 0 ) //while the robot has not been bumped or encounted a cliff
{
int actual = getADC(); //call a new ADC reading everytime at start of while as the current reading
rightH = 0;
leftH = 0; //set variables back to default to stop constant increase and decrease in variables
left = 200;
right = 200;
char error = abs(actual-desired); //calculate the error by minusing the actual distance to the desired
if(motorAngle < 180) //wall on left between 1st and 2nd quadrant
{
if(error > 200)
{
stop();
moveStepper(33,0);
goto begin;
}
if (actual > desired)
{
right -=((error)*k);
left += ((error)*k);
}
else if (actual < desired)
{
left -=((error)*k);
right +=((error)*k);
}
}
else if (motorAngle > 180)
{
if(error > 200)
{
stop();
moveStepper(33,1);
goto begin;
}
if (actual > desired)
{
left -=((error)*k);
right +=((error)*k);
}
else if (actual < desired)
{
right -=((error)*k);
left +=((error)*k);
}
}
drive(rightH,right,leftH,left); bumpSensor();
setLCDCursor(0x09);
writeLCDNumber(convert(actual)); //constantly write the converted AC value on the LCD
}
stop(); //stop the robot
}
You should restructure your code to avoid a GOTO. Another loop is needed. Something like this.
void followWall(void)
{
repeat = 0;
do // add this loop here
{
...
...
while( bumpFlag == 0 )
{
repeat = 0;
..
..
if(motorAngle < 180)
{
..
if(error > 200)
{
..
..
repeat = 1;
break;
}
}
else if (motorAngle > 180)
{
..
if(error > 200)
{
..
..
repeat = 1;
break;
}
}
}
} while (repeat == 1);
}

Atmel microprocessor and rotary encoder controlling speed of 7 segment display

I am trying to get a rotary encoder to control the speed of a 7 segment display counting from 0-9 with the Atmel (ATmega328P Xplained mini) microprocessor. My problem is that whenever I run the program the display just counts faster and faster until you can just see an "8", sometimes it seems that I can keep the speed down by turning the rotary encoder CCW and sometimes no effect at all. As I am not that experienced in programming and especially not this stuff I hope someone is capable and willing to help.
Here is my code:
#include <avr/io.h>
void Display (uint8_t x)
{
static uint8_t tabel[] =
{0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010,0b10000010,0b11111000,0b10000000,0b10010000};
PORTD = tabel[x];
}
int GetInput (void)
{
uint8_t x = PINC&1;
uint8_t y = (PINC>>1)&1;
if (x == 0 && y == 0) {return 0; }
else if (x == 1 && y == 0) {return 1;}
else if (x == 0 && y == 1) {return 2;}
else {return 3;}
}
int main(void)
{
DDRD = 0xFF; // set PortD as an output
DDRC = 0x00; // set PortC as an input
PORTB = 0x03; // Activate Pull-up resistors
float d = 9000;
int tick = 0;
int i = 0;
int input, state = 0; // initial state
int oldInput = 0;
while (1)
{
input = GetInput();
if (oldInput == 0 && input == 1)
{
d = (d * 1.1);
//slower
}else if (oldInput == 0 && input == 2)
{
d = (d * 0.9);
//faster
}else if (oldInput == 1 && input == 0)
{
d = (d * 0.9);
//faster
}else if (oldInput == 1 && input == 3)
{
d = (d * 1.1);
//slower
}else if (oldInput == 2 && input == 0)
{
d = (d * 1.1);
//slower
}else if (oldInput == 2 && input == 3)
{
d = (d * 0.9);
//faster
}else if (oldInput == 3 && input == 1)
{
d = (d * 0.9);
//faster
}else if (oldInput == 3 && input == 2)
{
d = (d * 1.1);
//slower
}
oldInput = input;
switch (state)
{
case 0: //ini
Display(0);
state = 1;
break;
case 1: //count
if (i == 9)
{
i = 0;
Display(i);
}
else
{
i++;
Display(i);
}
state = 2;
break;
case 2: // delay
if (tick < d)
{
state = 2;
tick++;
}
else
{
state = 1;
tick = 0;
}
break;
case 3: //reset / destroy
break;
}
}
}
First try changing the GetInput function to return a more useful value. Note that bit 0 and bit 1 of PINC already combine to form the integer that you're reconstructing.
int GetInput (void)
{
// array to convert grey scale bit patterns to direction indicators.
// Rows indexed by lastValue, columns indexed by thisValue, and the
// content is -1 for CCW, +1 for CW, 0 for no motion. Note that 0 is
// also used for an invalid transition (2 bits changed at once), but a
// different value could be used for fault detection.
static const int tableGreyToDirection[4][4] =
{
0 , -1, 1 , 0 , // lastValue==0
1 , 0 , 0 , -1, // lastValue==1
-1, 0 , 0 , 1 , // lastValue==2
0 , 1 , -1, 0 // lastValue==3
};
static uint8_t lastValue = 0; // A valid default starting value
uint8_t thisValue = (PINC & 0b00000011); // Use the bottom two bits as a value from 0..3
int result = tableGreyToDirection[lastValue][thisValue];
lastValue = thisValue;
return result;
}
You can then simplify the test in the loop greatly.
while (1)
{
// Check the direction of the encoder: -1 = CCW, +1 = CW, anything else = no motion.
input = GetInput();
if(0 < input)
{
// Motion is CW, so increment the delay (within reasonable bounds).
if(8900 > d) d += 100;
}
else if(0 > input)
{
// Motion is CCW, so decrement the delay (within reasonable bounds).
if(100 < d) d -= 100;
}
// Keep the rest as it is...
}
It would be advisable to change d to be a uint16_t and tidy it up a little. Further tips include using #define to provide readable names for constants. E.g. in my table of directions you could use:
#define ENCODER_CW 1
#define ENCODER_CCW -1
#define ENCODER_NEITHER 0
...
static const int tableGreyToDirection[4][4] =
{
ENCODER_NEITHER, ENCODER_CCW, ENCODER_CW, ENCODER_NEITHER, // lastValue==0
...
I'm sure you can fill it out yourself.
I checked your SW, but I can't find big issue instantly.
You'd better check below part.
If you didn't touch the encoder but speed is faster and faster
: do you have a scope to check the encoder input port whether noise is input from the port or not.
If two input port is stable, please check your value also stable
: old input and new input value should be same
: check by log or output toggle unused port when the value is changed. you can debug your own code.
You'd better add amount tick value than multiply directly to prevent d value becomes 0.
your CPU has to run as fast as detect port status change in main loop. - I think it is possible if this code is all of your system.

Resources