[c/rtos/fpga] Uitlezen array eerste en laatste index

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • RareAMV
  • Registratie: November 2012
  • Laatst online: 05-10 18:36
Hallo,

Was vandaag bezig met een project op de FPGA (Altera De2-115). Hierop draait Nios II waarop RTOS draait.
Ik was bezig met een for loopje waarin we in 2 float arrays alle sin en cosinus waarden in opslaan voor de waarde 0 tm 360. Als we die dan vervolgens in een oneindige loop proberen uit te lezen gaat dat goed voor de index 1 tm 359, maar bij 0 en 360 lijkt het alsof de waarde hier niet in gezet is. We krijgen bijvoorbeeld voor de sin array met index 0 de waarde 0 (klopt) en voor de cosinus array krijgen we bij de index 0 de waarde -0, terwijl dat 1 hoort te zijn (duh). Dit is wat ik tot nu toe heb (gedeelte waarvan ik dacht dat het relevant is):

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        float sinArray[360];
    float cosArray[360];

    int i;
    for(i = 0; i <= 360; i++){

        sinArray[i] = sin(i / (180 / M_PI));
        cosArray[i] = cos(i / (180 / M_PI));

        printf("\nROTATION: %d,  SIN: %.2f, COS: %.2f \n", i, sinArray[i], cosArray[i]);

    }

    int test[VALUES] = {-48,0,-2,-48,39,-1,-48,39,-1,-30,0,-2,-30,0,-2,-32,39,-1,-20,39,0,-19,-1,1,-8,38,-1,-20,39,0,-8,18,0,-21,18,0,-8,-1,-1,-19,-1,1,6,37,-1,6,-1,-1,25,-2,-1,6,-1,-1,25,-2,-1,25,37,0,6,37,-1,25,37,0,39,-1,-2,38,37,-1,38,37,-1,56,-1,-1,56,-1,-1,56,37,-1,-1,-53,-4,-24,-53,-1,-1,-53,-4,-1,-37,0,-1,-37,0,-23,-33,1,-1,-13,-1,-23,-12,1,-1,-37,0,-1,-13,-1,5,-55,1,20,-56,0,7,-36,-1,22,-35,-4,5,-55,1,7,-36,-1,22,-35,-4,20,-56,0,22,-13,-1,22,-35};

    startEndnodes = test;


    //local array to perform rotation and zoom calculations on
    int temporaryStartEndNodes[VALUES];

    while(1){
        //wait for other task to stop accessing global values such as rotation, zoom and startendnodes array
        ALT_SEM_PEND(sem_objectDrawn, 0);

        //local start and end node values
        float startx = 0;
        float starty = 0;
        float startz = 0;
        float endx = 0;
        float endy = 0;
        float endz = 0;

        //local rotation values
        int localRotationX = rotation[0];
        int localRotationY = rotation[1];
        int localRotationZ = rotation[2];
        float zoomscreen = *zoomscreenpointer;

        //check if it should stop drawing/stop performing calculations on array (set in TaskSerialInput)
        if(!stopDrawing){

            int linesDrawn;
            for (linesDrawn = 0; linesDrawn < VALUES/6; linesDrawn++)
            {

                //skips index every interation by 6
                int indexOfArray = linesDrawn * 6;

                //read start node from global array
                float x1 = startx = (startEndnodes)[indexOfArray];
                float y1 = starty = (startEndnodes)[indexOfArray + 1];
                float z1 = startz = (startEndnodes)[indexOfArray + 2];

                //read end node from global array
                float x2 = endx = (startEndnodes)[indexOfArray + 3];
                float y2 = endy = (startEndnodes)[indexOfArray + 4];
                float z2 = endz = (startEndnodes)[indexOfArray + 5];

                //if start and end node are both zero (if array has less then 240 elements) stop the draw loop and do not perform calculations
                if(x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0 && z1 == 0 && z2 == 0){
                    break;
                }

                //if a change happend in either the rotation or zoom, remove the previous known object calculated with the previous known zoom and rotation from screen
                if(previousXRotation != localRotationX || previousYRotation != localRotationY || previousZRotation != localRotationZ || zoomscreen != prevousZoomScreenBR)
                {
                     //perform rotation calculations on the 3 dimensonal nodes and convert them to 2D nodes and then perform zoom calculation.

                     x1 = (((startx * cosArray[previousXRotation] - (starty * cosArray[previousZRotation] - startz * sinArray[previousZRotation]) * sinArray[previousXRotation]) * (cosArray[previousYRotation])) + (starty * sinArray[previousZRotation] + startz * cosArray[previousZRotation]) * sinArray[previousYRotation]) * prevousZoomScreenBR;
                     x2 = (((endx * cosArray[previousXRotation] - (endy * cosArray[previousZRotation] - endz * sinArray[previousZRotation]) * sinArray[previousXRotation]) * (cosArray[previousYRotation])) + (endy * sinArray[previousZRotation] + endz * cosArray[previousZRotation]) * sinArray[previousYRotation]) * prevousZoomScreenBR;

                     y1 = (((-(startx * cosArray[previousXRotation] - (starty * cosArray[previousZRotation] - startz * sinArray[previousZRotation]) * sinArray[previousXRotation])) * (sinArray[previousYRotation]) + (starty * sinArray[previousZRotation] + startz * cosArray[previousZRotation]) * cosArray[previousYRotation])) * prevousZoomScreenBR;
                     y2 = (((-(endx * cosArray[previousXRotation] - (endy * cosArray[previousZRotation] - endz * sinArray[previousZRotation]) * sinArray[previousXRotation])) * (sinArray[previousYRotation]) + (endy * sinArray[previousZRotation] + endz * cosArray[previousZRotation]) * cosArray[previousYRotation])) * prevousZoomScreenBR;

                     //if offscreen benchmark mode is enabled, do not execute draw functions (which is the current bottleneck)
                     if(!offscreen){
                         drawLine(x1,y1,x2,y2, midX, midY,1 , 1, color);
                     }

                }

               //perform rotation calculations on the 3 dimensonal nodes and convert them to 2D nodes and then perform zoom calculation.
                 x1 = (((startx * cosArray[localRotationX] - (starty * cosArray[localRotationZ] - startz * sinArray[localRotationZ]) * sinArray[localRotationX]) * (cosArray[localRotationY])) + (starty * sinArray[localRotationZ] + startz * cosArray[localRotationZ]) * sinArray[localRotationY]) * zoomscreen;
                 x2 = (((endx * cosArray[localRotationX] - (endy * cosArray[localRotationZ] - endz * sinArray[localRotationZ]) * sinArray[localRotationX]) * (cosArray[localRotationY])) + (endy * sinArray[localRotationZ] + endz * cosArray[localRotationZ]) * sinArray[localRotationY]) * zoomscreen;

                 y1 = (((-(startx * cosArray[localRotationX] - (starty * cosArray[localRotationZ] - startz * sinArray[localRotationZ]) * sinArray[localRotationX])) * (sinArray[localRotationY]) + (starty * sinArray[localRotationZ] + startz * cosArray[localRotationZ]) * cosArray[localRotationY]))* zoomscreen;
                 y2 = (((-(endx * cosArray[localRotationX] - (endy * cosArray[localRotationZ] - endz * sinArray[localRotationZ]) * sinArray[localRotationX])) * (sinArray[localRotationY]) + (endy * sinArray[localRotationZ] + endz * cosArray[localRotationZ]) * cosArray[localRotationY]))* zoomscreen;

                //if offscreen benchmark mode is enabled, do not execute draw functions (which is the current bottleneck)

                 if(localRotationX == 0 && localRotationY == 0 && localRotationZ == 0){
                     printf("\n ROTATION X,Y,Z:%d,%d,%d, is for x1,y1,x2,y2: %d, %d, %d, %d of which sin/cos = %f, %f \n", localRotationX, localRotationY, localRotationZ, x1, y1, x2, y2, sinArray[1], cosArray[1]);
                 }
                if(!offscreen){
                    drawLine(x1,y1,x2,y2, midX, midY, 1 , 0, color);
                }
            }

            //save previous zoomscreen if its different then before
            if (zoomscreen != prevousZoomScreenBR)
            {
                prevousZoomScreenBR = zoomscreen;
            }

            //save previous rotation
            previousXRotation = localRotationX;
            previousYRotation = localRotationY;
            previousZRotation = localRotationZ;

            //update fps counter (after for loop is finished peforming calculation, a complete frame has been drawn)
            ALT_SEM_PEND(sem_updateFps, 0);
            fpsCounter++;
            ALT_SEM_POST(sem_updateFps);

        }


Ik ben nog beginnende met c/rtos, maar ik snap er geen hout van waarom hij juist alleen bij die twee index waarden complete onzin waardes uitleest. Op internet is hier helaas weinig over te vinden. Misschien maak ik ergens een domme fout waar ik overheen lees, bij voorbaat dank als u dit kunt aanwijzen(en misschien ook uitleggen _/-\o_ )!

Beste antwoord (via RareAMV op 20-05-2016 17:04)


  • marc1990
  • Registratie: Februari 2013
  • Laatst online: 11:20
Je loop is van 0 t/m 360 maar je array is allocated voor 0 t/m 359.

[ Voor 16% gewijzigd door marc1990 op 20-05-2016 17:05 ]

Alle reacties


Acties:
  • Beste antwoord
  • 0 Henk 'm!

  • marc1990
  • Registratie: Februari 2013
  • Laatst online: 11:20
Je loop is van 0 t/m 360 maar je array is allocated voor 0 t/m 359.

[ Voor 16% gewijzigd door marc1990 op 20-05-2016 17:05 ]


Acties:
  • +1 Henk 'm!

  • RareAMV
  • Registratie: November 2012
  • Laatst online: 05-10 18:36
[quote]marc1990 schreef op vrijdag 20 mei 2016 @ 16:59:
Je loop van 0 t/m 360 maar je array is 0 t/m 359
[/quote

RIP Brein. Ik heb altijd met het idee gelopen dat je daadwerkleijk bij een array met [x] 0 tot x elementen had om te gebruiken, bedankt voor het aanwijzen van deze stommiteit. Het werkt :D.

Acties:
  • 0 Henk 'm!

  • Damic
  • Registratie: September 2003
  • Laatst online: 08:40

Damic

Tijd voor Jasmijn thee

Maar 0 en 360, 720, 1080,... zijn normaal toch hetzelfde of je doet nog iets speciaal er mee dan heb je telkens de ° nodig.

[ Voor 37% gewijzigd door Damic op 20-05-2016 17:13 ]

Al wat ik aanraak werk niet meer zoals het hoort. Damic houd niet van zijn verjaardag


Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
RareAMV schreef op vrijdag 20 mei 2016 @ 17:04:
Ik heb altijd met het idee gelopen dat je daadwerkleijk bij een array met [x] 0 tot x elementen had om te gebruiken
Dat heb je ook :? Niet tot en met maar tot x elementen. Ofwel:
float myArray[360];
geeft je 360 elementen, namelijk:
myArray[0]
myArray[1]
..
..
myArray[358]
myArray[359]

Dat zijn in totaal 360 elementen (de 0 telt mee ;) ), maar myArray[360] bestaat dus niet.

Dus let even op; je for-loop heeft een <= 360 waar dat < 360 zou moeten zijn. Een 'lookup' van een willekeurige (integer) value van v° doe je dus: result = myArray[v % 360] en blijf je altijd netjes de 0..359 gebruiken (0 based index). Ga je dit 'verwateren' naar een "1-based index" dan blijf je tegen off-by-one errors aanlopen ;)

* RobIII heeft genoeg gevloekt op 1-based indices in het VB6 tijdperk (waar wel een Option Base 0 statement voor bestond maar waar, bijv, allerlei COM meuk van Office (nu nog AFAIK) en andere 3rd party componenten besloten van af te wijken) :X

[ Voor 45% gewijzigd door RobIII op 20-05-2016 17:56 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • farlane
  • Registratie: Maart 2000
  • Laatst online: 11-10 14:49
RobIII schreef op vrijdag 20 mei 2016 @ 17:41:
[...]
Dat zijn in totaal 360 elementen (de 0 telt mee ;) ), maar myArray\[360] bestaat dus niet.
Kleine kanttekening : je mag het adres er van wel gebruiken (om het einde van de sequence aan te geven) maar dereferencen is undefined. Je mag zelfs zeggen &myArray[360]

Somniferous whisperings of scarlet fields. Sleep calling me and in my dreams i wander. My reality is abandoned (I traverse afar). Not a care if I never everwake.

Pagina: 1