Author Topic: newby C programming question  (Read 2744 times)

0 Members and 1 Guest are viewing this topic.

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
newby C programming question
« on: February 05, 2011, 09:49:33 PM »
I finally got my LCD, PS2 (for the most part) and Axon II running but im having trouble getting the joysticks to return their values.

here is the code snipit that takes the right joy stick x and is supposed to display the value on the LCD.  However, it displays a constant "2" and for stick right Y it displays "3", so my guess it that i'm not getting the pot value but its assignment.  I know it must be some stupid code error.  And yes, i've enabled the joysticks.........
Code: [Select]
sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);
displayGoto(&LCD,0,1);
rprintf("%u",PS2_STICK_RIGHT_X);

as always, any help would be great!

here is the entire code that does everything correctly except display the joysticks
Code: [Select]
#include "hardware.h"
#include <stdio.h>
#include <math.h>


// Initialise the hardware
void appInitHardware(void) {
initHardware();


delay_ms(500);
}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){



return 0;

}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
int loop=1;
int i=0;
//int x;
// -------- Start Sony PS2-------

// -------- Start Sparkfun serLCD-------
//turn on LCD
LED_on(&led);
delay_ms(500);
rprintfInit( displayGetWriter(&LCD));
    displayBacklight(&LCD,TRUE);
delay_ms(1000);

displayGoto(&LCD,0,1);
rprintf("  LCD Initialized");

displayGoto(&LCD,0,2);
rprintf("     PS2 Ready");

delay_ms(1500);

displayClear(&LCD);

// -------- End   Sparkfun serLCD-------

sonyPS2_calibrate(&PS2,27);

//-------------main loop----------------------
while(loop==1){
// Read the Sony PS2 and store the result in PS2
if(sonyPS2_read(&PS2)){


// Test if SELECT button pressed
if(sonyPS2_buttonPressed(&PS2,PS2_BTN_SELECT)){

// -------- Start Sparkfun serLCD-------
//turn on LCD
LED_on(&led);
delay_ms(500);
// Initialise the LCD
//displayGoto(&LCD,0,1);
rprintfInit( displayGetWriter(&LCD));


   displayBacklight(&LCD,TRUE);
delay_ms(1000);

displayGoto(&LCD,0,1);
rprintf("  LCD Initialized");

displayGoto(&LCD,0,2);
rprintf("     PS2 Ready");

delay_ms(1500);

displayClear(&LCD);
}
// -------- End   Sparkfun serLCD-------

//----------PS2 buttons------------------------
if(sonyPS2_buttonPressed(&PS2,PS2_BTN_START)){
LED_off(&led);
delay_ms(500);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_X)){
displayGoto(&LCD,0,0);
rprintf("X");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_TRIANGLE)){
displayGoto(&LCD,0,0);
rprintf("TRIANGLE");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_SQUARE)){
displayGoto(&LCD,0,0);
rprintf("SQUARE");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_CIRCLE)){
displayGoto(&LCD,0,0);
rprintf("CIRCLE");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_L3)){
displayGoto(&LCD,0,0);
rprintf("L3");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_L2)){
displayGoto(&LCD,0,0);
rprintf("L2");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_L1)){
displayGoto(&LCD,0,0);
rprintf("L1");

delay_ms(500);
displayClear(&LCD);
}



if(sonyPS2_buttonPressed(&PS2,PS2_BTN_R3)){
displayGoto(&LCD,0,0);
rprintf("R3");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_BTN_R2)){
displayGoto(&LCD,0,0);
rprintf("R2");

delay_ms(500);
displayClear(&LCD);
}
if(sonyPS2_buttonPressed(&PS2,PS2_BTN_R1)){
displayGoto(&LCD,0,0);
rprintf("R1");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_DPAD_UP)){
displayGoto(&LCD,0,0);
rprintf("UP");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_DPAD_DOWN)){
displayGoto(&LCD,0,0);
rprintf("DOWN");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_DPAD_RIGHT)){
displayGoto(&LCD,0,0);
rprintf("RIGHT");

delay_ms(500);
displayClear(&LCD);
}

if(sonyPS2_buttonPressed(&PS2,PS2_DPAD_LEFT)){
displayGoto(&LCD,0,0);
rprintf("LEFT");

delay_ms(500);
displayClear(&LCD);
}


//----------End Button test-------------

//----------Start Analog---------
sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);
displayGoto(&LCD,0,1);
rprintf("%u",PS2_STICK_RIGHT_X);

//int8_t sonyPS2_joystick(const SONY_PS2* controller, PS2_STICK_LEFT_X);


if(sonyPS2_isAnalogMode(&PS2)){
displayGoto(&LCD,10,3);
rprintf("true");
//
delay_ms(500);
displayClear(&LCD);

}

// -------- End   Sony PS2-------
}
}
return 0;
}

« Last Edit: February 09, 2011, 08:19:37 PM by BANE »

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #1 on: February 06, 2011, 02:24:13 AM »
It would be helpful if you specified and provided links to the PS2 controller library you are using... ;)

Off the top of my head I would guess that
Code: [Select]
sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);
is supposed to return a value. So the code should look something like:
Code: [Select]
right_stick_x_value = sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);
displayGoto(&LCD,0,1);  
rprintf("%u",right_stick_x_value);

Declare right_stick_x_value as the sonyPS2_joystick() return variable type.

edit: - I see you are using Webbotlib, so sonyPS2_joystick() returns a signed short int:

You have the function declaration line in your code as a comment.
Code: [Select]
int8_t sonyPS2_joystick(const SONY_PS2* controller, PS2_STICK_LEFT_X);
int8_t is the return type for the function. You need to declare a new variable of the same type, and then assign the value to that returned by the function
 
Code: [Select]
int8_t right_stick_x_value;     //declare new variable
right_stick_x_value = sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);    //assign the functions return value to your new variable
displayGoto(&LCD,0,1);  
rprintf("%u",right_stick_x_value);      // use your variable value here

To make it more confusing you can declare and assign in one line:
Code: [Select]
int8_t right_stick_x_value = sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X);    //declare  and assign the functions return value to your new variable
displayGoto(&LCD,0,1);   
rprintf("%u",right_stick_x_value);      // use your variable value here

Or even use the sonyPS2_joystick() return value directly in your rprintf() call:
Code: [Select]
displayGoto(&LCD,0,1);   
rprintf("%u",sonyPS2_joystick(&PS2, PS2_STICK_RIGHT_X));      // use the return value directly here
« Last Edit: February 06, 2011, 05:50:57 AM by hopslink »

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #2 on: February 06, 2011, 12:41:50 PM »
thanks hopslink, getting readings now :)
Quote
int8_t is the return type for the function.
i knew it was something simple.

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #3 on: February 07, 2011, 08:03:46 PM »
i have another easy question.  Does C have a "GOSUB" and "return" function like BASIC?  If so, what is the substitution?

thanks,

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #4 on: February 07, 2011, 10:41:46 PM »
Hmm, not that easy... :P There is no specific command equivalent of GOSUB in C, with good reason. GOSUB allows unseen changes to existing variable values and can make code incredibly difficult to follow. Some BASIC variants have dropped GOSUB along with GOTO.

The equivalent of the BASIC subroutine is the C function, and the GOSUB substitution is the 'C function call' (do a google search for more information) , also look at 'C variable scope' which puts restrictions on where a variable can be used/modified (BASIC variables are equivalent in scope to 'C Global Variables' ). You have to specify variables you want to use in a function ('C variable passing').

Though this seems restrictive compared to BASIC it is very useful for any complex program. More modern languages like Java, Python and C# are more restrictive again - see 'object oriented programming'.

You are already using C function calls in your code. Keep going, it will soon be second nature!

 

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #5 on: February 09, 2011, 08:18:24 PM »
thanks hopslink,
after looking into it i had no idea that GOSUB would be that complicated being that in BASIC that function is used a lot in the simplest programs.  However, for simplicity reasons right now GOTO's seem to get the job done.

Btw, would know how to print special characters on a LCD?  Right now all i can print to the LCD is an keyboard characters but i would like to print a black box for a battery meter.  I've googled around for a sheet with the characters that can be displayed but no luck.  any ideas?

thanks

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #6 on: February 10, 2011, 12:08:05 PM »
You don't say what type of display, I will assume you have an Nx2 driven by a Hitachi HD44870 or compatible driver since this is the most common type.

A typical character set for these displays can be seen in this HD44780 Datasheet (p.17). Here is a guide for creating custom characters, but....

Since you are using Webbotlib you should check out the displayHorizGraph() and displayVertGraph() functions in the Displays library as these should do what you want. See the description of the functions here at the bottom of the page.

HTH

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: newby C programming question
« Reply #7 on: February 10, 2011, 12:35:00 PM »
Yep, I was going to also mention that you should look at the HD44780 data sheet.  Which you can download from sparkfun: http://www.sparkfun.com/datasheets/LCD/HD44780.pdf

Also don't let functions/subroutines scare you off.  They are quite easy to use and depending on which version of basic you have used, not that different...  As Hopslink mentioned you are using them already...

That is you can simply define your subroutine, the simplest takes no parameters and returns nothing.  It still has access to all your global variables.
Code: [Select]
void MySubroutine(void) {
// do whatever you need
}

You can call this by simply doing:
Code: [Select]
   MySubroutine();
Note: most C's require you to define a function/Subroutine before you can use them.  So often you will see a section at the start of a file or in a header file that has a bunch of definitions like:
Code: [Select]
extern void MySubroutine(void);
A nice thing about C is that you can define your own functions that return something like:
Code: [Select]
int MyFunc(void) {
   // do something
   return Somevalue;
}
You then can use this function directly within expressions like:
Code: [Select]
MyVal = MyFunc();Again you may need to define some forward references for this...

You can then expand to passing parameters and the like... 

Good Luck
Kurt

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #8 on: February 10, 2011, 02:24:05 PM »
thanks guys,
Im using the serial enabled 20x4 LCD from sparkfun http://www.sparkfun.com/products/9568
Quote
Since you are using Webbotlib you should check out the displayHorizGraph() and displayVertGraph() functions in the Displays library as these should do what you want. See the description of the functions here at the bottom of the page.

I read about this from project designers link and couldn't get the code to display anything.  but i probably just didnt understand how to use it

@ KurtEck.  thank kurt, im going to try that out in my next program

here is some test code i just wrote:
Code: [Select]
#include "hardware.h"
// Initialise the hardware
void appInitHardware(void) {
initHardware();


}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){

return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
int i=1;
delay_ms(500);
// -------- Start Sparkfun serLCD-------
//turn on LCD
LED_on(&led);//turn on LCD relay
delay_ms(500);

rprintfInit( displayGetWriter(&LCD));
    //displayBacklight(&LCD,TRUE);
displayBacklight(&LCD,FALSE);
delay_ms(1000);

displayGoto(&LCD,0,1);
rprintf("  LCD Initialized");
delay_ms(1000);

displayClear(&LCD);
// -------- End   Sparkfun serLCD-------

while(i){
//----------------graph start---------------
displayHorizGraph(&LCD,0,0,2,19,1);
displayVertGraph(&LCD,0,0,2,3,1);
//----------------end graph-----------------
delay_ms(500);
displayClear(&LCD);
}
return 0;
}
Quote
displayHorizGraph(&LCD,0,0,2,19,1);
displayVertGraph(&LCD,0,0,2,3,1);

i was trying to display three black boxes in an "L" shape starting at (0,0) but nothing gets displayed. I've tried playing with the numbers but no luck.  how do i use this code to display something?
much thanks


Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #9 on: February 10, 2011, 03:03:58 PM »
From the Webbotlib docs
"Once defined you should call displayInit from appInitHardware and then you can use other display commands as listed in _display_common.h"

So try:
Code: [Select]
// Initialise the hardware
void appInitHardware(void) {
initHardware();
        displayInit();

}

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #10 on: February 10, 2011, 04:46:52 PM »
i just added that code and still not getting anything.  I hooked up the oscope and data is being sent to the screen but .  rprintf works great before and after i added that piece of code, but still not displaying anything from the displayHorizGraph() and displayVertGraph() functions.

just to see if i understand what should be displayed using:
Code: [Select]
displayHorizGraph(&LCD,0,0,17,19,10);
displayVertGraph(&LCD,0,0,2,3,10);
starting at (0,0) it should display 3 black boxes because the screen has 4 rows and then display 18 black boxes because the screen has 20 columns.
is this correct?

Code: [Select]
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
initHardware();


}
// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){

return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

// Initialise the hardware
void appInitHardware(void) {
initHardware();
        displayInit(&LCD);

}

int i=1;
delay_ms(500);
// -------- Start Sparkfun serLCD-------
//turn on LCD
LED_on(&led);//turn on LCD relay
delay_ms(500);


rprintfInit( displayGetWriter(&LCD));
    //displayBacklight(&LCD,TRUE);
displayBacklight(&LCD,FALSE);
delay_ms(1000);

displayGoto(&LCD,0,1);
rprintf("  LCD Initialized");
delay_ms(1000);

displayClear(&LCD);
// -------- End   Sparkfun serLCD-------

while(i){
//----------------graph start---------------
displayHorizGraph(&LCD,0,0,17,19,10);
displayVertGraph(&LCD,0,0,2,3,10);
//----------------end graph-----------------
delay_ms(500);
displayClear(&LCD);
}
return 0;
}

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #11 on: February 10, 2011, 06:29:02 PM »
Remove the extra code you have just added. appInitHardware() already exists at the top of your code, I just meant you to add the displayInit() call line into there, the rprintfInit() call also belongs there. That way it only gets called once as the system initialises not every time through your main loop. Sorry I did not make that clear.

Webbotlib programs have a hardware initialisation section and software initialisation section where you do all the setup for your main loop. It is also different to normal C code in that it uses a scheduler that runs the main loop every so often so you don't necessarily need your while(1) loop at all. 

I would also lose the displayVertGraph() call for now since you don't have a lot of range for vertical graphing on a 20x2 display.

I would start trying:
Code: [Select]
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {

// Place code you use to initialise hardware here

initHardware();
        //add the LCD hardware initialisation to this section
        displayInit(&LCD);           
        rprintfInit( displayGetWriter(&LCD));
}

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){

// Place code you only need to run once here

        // -------- Start Sparkfun serLCD-------
//turn on LCD
LED_on(&led); //turn on LCD relay
delay_ms(500);

        //displayBacklight(&LCD,TRUE);
displayBacklight(&LCD,FALSE);

displayGoto(&LCD,0,1);
rprintf("  LCD Initialized");
delay_ms(1000);

displayClear(&LCD);
// -------- End   Sparkfun serLCD-------

return 0;
}


// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

// put your looping code here

        //Draw Graph
        displayHorizGraph(&LCD, 1, 1, 50, 100, 18);

delay_ms(2000);
displayClear(&LCD);

return 0;
}
See what that gets you.

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #12 on: February 10, 2011, 07:03:48 PM »
urrg, still getting nothing.  I've tried adding the code where instructed and coping pasting the code you posted directly and still not displaying anything  :P 
Also, not that it makes a difference, this LCD is 20x4.  it honestly wouldn't surprise me if there is an issue with this LCD because it has a history of being a problem child :'(

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: newby C programming question
« Reply #13 on: February 10, 2011, 08:42:42 PM »
it honestly wouldn't surprise me if there is an issue with this LCD because it has a history of being a problem child :'(
Would not surprise me either, awhile ago (maybe 1-2 years) I had one of their 16x2 serial LCDs and it worked OK for awhile and died.  Keep meaning to pick up another one, but these days I try to find other ways to debug things...  Never did any of the graphics with it so for this part I will probably just lurk...

Kurt

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #14 on: February 13, 2011, 06:19:52 PM »
Just fetched a HD44870 driven 16x2 display (closest thing I have) out of my parts bin and gave it a go, the code I posted works ok for me... :P

Not sure what to suggest from here, if your display was working ok with text but not the graphing functions then it may be a Webbotlib bug in the Sparkfun display library graph routines... or more likely just something I have overlooked :-[. Ensure you haven't changed anything in the hardware.h and xhardware.h files that are automatically generated.

If you are still having troubles then you could try adding Webbotlib to your post title, that will pretty much guarantee a response from Webbot or another expert. Ease of use and support for the library are impressive!

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #15 on: February 14, 2011, 01:54:56 PM »
Quote
the graphing functions then it may be a Webbotlib bug in the Sparkfun display library graph routines... or more likely just something I have overlooked

you may be on to something hopslink.  awhile back i was having trouble getting this screen running in this post http://www.societyofrobots.com/robotforum/index.php?topic=13083.0  However, it was resolved and i ended up having a problem with my 20x4 acting like a 16x2 as far as printing to it.  Webbot fixed something in the libWebbot-ATMega640.a file and sent it to me and the screen behaved like a 20x4; so problem solved.  Perhaps what ever was changed caused the graph function to not work. 

I saved the original libWebbot-ATMega640.a so ill give it a try.

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #16 on: February 22, 2011, 06:40:17 PM »
nope, that didnt work ether.  i hate to bother webbot about this since everything besides the graphing function works really well :P

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: newby C programming question
« Reply #17 on: February 22, 2011, 08:38:37 PM »
First off - you should note that you can't use the HorizGraph and VertGraph functions simultaneously -use one or the other. This is because it makes use of the programmable characters to define either 'vertical bar' or 'horizontal bar' characters. The display doesn't have enough space to define both simultaneously.

Bane - the 'fix' you refer to is included in the latest version 1.29 release.

Here is some code that I use for testing. This assumes you have used Project Designer and named your lcd display as 'display'.
This code will plot a graph of each ADC input pin. You should find that placing your finger on the ADC pins will cause enough noise to change the values. Alternatively plug in an analogue sensor like a Sharp GP2 or a photocell.

Code: [Select]
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
initHardware();


}

int cols;

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
displayAutoScroll(&display,FALSE);
displayLineWrap(&display,FALSE);

cols = MIN(NUM_ADC_CHANNELS, displayColumns(&display));
for(int n=0; n<cols;n++){
rprintf("%d",n % 10);
}
return 0;
}


// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
int depth = displayLines(&display)-1;

for(int ch = 0; ch<cols; ch++){
uint8_t val = a2dConvert8bit(ADC_NUMBER_TO_CHANNEL(ch));
displayVertGraph(&display,ch,1, val, 255, depth);
}

return 0;
}
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: newby C programming question
« Reply #18 on: February 22, 2011, 08:53:36 PM »
And one last point which may be a killer - the Sparkfun serLCD doesn't have any commands for setting up the programmable characters. (Must put that in my manual!).

If anyone knows better then let me know (I find their manual rather confusing!)
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #19 on: February 23, 2011, 03:01:02 PM »
thanks for the response webbot

I have the lcd named "display"(F4) and three IR sensor (F0-F2) and copy/pasted your code.  However, still not getting any thing on the display. 

In an attempt to get something to work, I've added a couple lines to display the IR values for debugging purposes just to make sure the sensors and screen are working. Which displays the three IR sensors values on row 4, but still not getting any graphs in the first three rows.
Code: [Select]
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
initHardware();
delay_ms(1000);

//----LCD power relay output--------------
pin_make_output(G5,FALSE);
pin_high(G5);//turn relay on
//----------------------------------------
delay_ms(1000);

//---Initialize LCD/enable rprintf--------
displayInit(&display);           
rprintfInit( displayGetWriter(&display));
//----------------------------------------
}

int cols;

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
displayAutoScroll(&display,FALSE);
displayLineWrap(&display,FALSE);

cols = MIN(NUM_ADC_CHANNELS, displayColumns(&display));
for(int n=0; n<cols;n++){
rprintf("%d",n % 10);
}
delay_ms(1000);
return 0;
}


// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
int depth = displayLines(&display)-1;
for(int ch = 0; ch<cols; ch++){
uint8_t val = a2dConvert8bit(ADC_NUMBER_TO_CHANNEL(ch));
displayVertGraph(&display,ch,1, val, 255, depth);
//----------print IR values (DEBUG)-----------------
if(ch==0){
displayGoto(&display,0,3);
rprintf("1:%d",val);
}
if(ch==1){
displayGoto(&display,6,3);
rprintf("2:%d",val);
}
if(ch==2){
displayGoto(&display,12,3);
rprintf("3:%d",val);
}
//-------------end print(DEBUG)---------------------------
}
delay_ms(100);
return 0;
}

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #20 on: February 23, 2011, 03:11:44 PM »
I've simplified this program as much as possible to try and get the something but this code doesn't work either :P
Code: [Select]
#include "hardware.h"

// Initialise the hardware
void appInitHardware(void) {
initHardware();
delay_ms(1000);

//----LCD power relay output--------------
pin_make_output(G5,FALSE);
pin_high(G5);//turn relay on
//----------------------------------------
delay_ms(1000);
}


// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
displayAutoScroll(&display,FALSE);
displayLineWrap(&display,FALSE);
delay_ms(1000);
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
displayVertGraph(&display,1,1, 128, 255, 1);
delay_ms(100);
return 0;
}

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: newby C programming question
« Reply #21 on: February 23, 2011, 04:05:03 PM »
BANE - see my last post on this thread. The 'Sparkfun serLCD' doesn't support the horiz/vertGraph function because Sparkfun, in their infinite wisdom, didn't provide a way to create custom characters - and they are essential for the WebbotLib graphing functions.
So it seems you may have used the one display type from the list supported by WebbotLib where the functionality isn't possible (and hence the code does nothing).

You are using the Sparkfun serLCD right?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #22 on: February 23, 2011, 04:11:44 PM »
Quote
You are using the Sparkfun serLCD right?
unfortunately yes
Quote
The 'Sparkfun serLCD' doesn't support the horiz/vertGraph function
Quote
it honestly wouldn't surprise me if there is an issue with this LCD because it has a history of being a problem child
i suspected this but gave it a shoot just for the records............

thanks webbot

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: newby C programming question
« Reply #23 on: February 24, 2011, 06:56:57 PM »
And they just bought out a new version of a 16x2 serial LCD http://www.sparkfun.com/products/9877

And they still don't support the use of programmable characters! Doh !
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #24 on: February 26, 2011, 10:56:26 AM »
the fix works webbot :D and will suffice for what i wanted to do.  If possible, this should be in webbotlib 1.30

Quote
And they still don't support the use of programmable characters! Doh !
do you have any recommendations for my next LCD screen?

thnx

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: newby C programming question
« Reply #25 on: February 26, 2011, 11:12:23 AM »
the fix works webbot :D and will suffice for what i wanted to do.  If possible, this should be in webbotlib 1.30
It will be - any interim releases I gave out to individuals are just pre-releases of the next version

Quote
do you have any recommendations for my next LCD screen?
I use a mixture of basic HD47780 controlled displays of different sizes - all using the parallel interface - ie 7, or so, I/O pins.
If you want to go serial then I have also used the Matrix Orbital serial displays mentioned in the WebbotLib docs - but the serial ones tend to more expensive.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #26 on: February 26, 2011, 10:49:14 PM »
 
Quote
Matrix Orbital serial displays
thanks webbot, will def add one of these to my wishlist :D

On another note, I'm trying to use functions in C and apparently having some compiling issues.  Here is the code i want to try to rap up in a function if possible

//this program takes reading from a ps2 controller and drives a sabertooth motor controller using tank steering//

In a nut shell i would like to try to make a function that takes in the x and y min and max and actual x and y values, then scales them and using the mixer logic to produce the final values that the sabertooth needs.
 



given:
int left_joy_x_min;//i have a initial calibration that determines this values//
int left_joy_x_max;
int left_joy_y_min;
int left_joy_y_max;

int motors_max = 128;
int motors_min = -128;
Code: [Select]
steering=sonyPS2_joystick(&PS2, PS2_STICK_LEFT_X);//grab ps2 joy stick value
throttle=sonyPS2_joystick(&PS2, PS2_STICK_LEFT_Y);//grab ps2 joy stick value

//-----------scaling code----------------
if(steering<0){
steering=(steering*(100*motors_min/left_joy_x_min))/100;
}
if(steering>0){
steering=(steering*(100*motors_max/left_joy_x_max))/100;
}
if(throttle<0){
throttle=(throttle*(100*motors_min/left_joy_y_min))/100;
}
if(throttle>0){
throttle=(throttle*(100*motors_max/left_joy_y_max))/100;
}
//----------auto scaling end--------------------

//------------speed calculater-------------
velocity=sqrt(throttle*throttle+steering*steering);
if(velocity>=99){
velocity=99;}
if(throttle<0){
velocity=-velocity;
}

//------------end speed-----------------

//=======================================================================================
//------------analog tank mixing-----------------


if(steering>0&&throttle<0){//sector1
act_setSpeed(&motor1,velocity+steering*2);
act_setSpeed(&motor2,velocity);
delay_ms(5);}

else if(steering<0&&throttle<0){//sector2
act_setSpeed(&motor1,velocity);
act_setSpeed(&motor2,velocity-steering*2);
delay_ms(5);}

else if(steering<0&&throttle>0){//sector3
act_setSpeed(&motor1,velocity+steering*2);
act_setSpeed(&motor2,velocity);
delay_ms(5);}

else if(steering>0&&throttle>0){//sector4
act_setSpeed(&motor1,velocity);
act_setSpeed(&motor2,velocity-steering*2);
delay_ms(5);}

else if(steering>0&&throttle==0){//left power spin
act_setSpeed(&motor1,velocity);
act_setSpeed(&motor2,-velocity);
delay_ms(5);}

else if(steering<0&&throttle==0){//right power spin
act_setSpeed(&motor1,-velocity);
act_setSpeed(&motor2,velocity);
delay_ms(5);}

else{//fowards/backwards/stop
act_setSpeed(&motor1,velocity);
act_setSpeed(&motor2,velocity);
delay_ms(5);}
//---------------exit mixer-----------------


Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #27 on: February 27, 2011, 06:41:07 AM »
Guess what.....

Webbotlib has a function for that (surprise!!!   :D ).

Take a peek at the interpolate() and interpolateU() functions in core.h. These do what you are looking for, mapping a value from one range to its equivalent in another.
« Last Edit: February 27, 2011, 01:24:45 PM by hopslink »

Offline BANETopic starter

  • Supreme Robot
  • *****
  • Posts: 639
  • Helpful? 4
  • E=roboticsC^2
Re: newby C programming question
« Reply #28 on: February 27, 2011, 05:17:21 PM »
from core.h
Quote
Mathematically it will return:
minRtn+((value-minVal)*(maxRtn-minRtn)/(maxVal-minVal))
interpolate just scales the value within the parameters, it doesn't preform the tank steering logic as seen in my code, right?

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: newby C programming question
« Reply #29 on: February 27, 2011, 05:23:33 PM »
That's right, sorry were you were looking more for help with the logic side?

 


Get Your Ad Here