L3G4200D Gyroscope Only Outputting Negative Values
amalfiCoast
Posts: 132
in Robotics
Hello,
My gyro is only outputting negative values and also seems kind of noisy. Could someone please take a look at my code below? Also, I've attached an image of some plotted data when I was rotating the gyro back and forth rather quickly. I would expect the values to go positive and be less noisy. What do you think?
[img][/img][img][/img]
My gyro is only outputting negative values and also seems kind of noisy. Could someone please take a look at my code below? Also, I've attached an image of some plotted data when I was rotating the gyro back and forth rather quickly. I would expect the values to go positive and be less noisy. What do you think?
[img][/img][img][/img]
/* 0 as the MSB is write 1 as the MSB is read */ #include "simpletools.h" signed int whoAmI; signed int zL, zH, z, zSum, zAvg; signed int statusReg; int main(){ high(14); //CS line high (SPI inactive) low(13); //CLK line low (prepares chip to send brief high signals or clock pulses) low(14); shift_out(12, 13, MSBFIRST, 8, 0b10001111); //Send read register address (whoAmI) whoAmI = shift_in(12, 13, MSBPRE, 8); high(14); if(whoAmI == 0b11010011){ print("Device ID Correct: %b\n", whoAmI); } else{ print("Device ID Incorrect: %b\n", whoAmI); } low(14); //CS line low (start SPI) shift_out(12, 13, MSBFIRST, 8, 0b00100011); //Write MCTL register (register 4)to set 3-wire SPI shift_out(12, 13, MSBFIRST, 8, 0b00010001); //Value for MCTL register (register 4) to set 3-wire SPI high(14); // Set 100Hz output rate, 25Hz cutoff freq, normal mode, enable all axes low(14); shift_out(12, 13, MSBFIRST, 8, 0b01100000); //Write MCTL register (register 1) shift_out(12, 13, MSBFIRST, 8, 0b00011111); //Value for MCTL register (register 1) high(14); //CS high(stop SPI) pause(1); //Get z-axis zero-rate offset (zAvg) for(int i=0; i<1000; i++){ /* //Poll status register low(14); //CS low selects chip shift_out(12, 13, MSBFIRST, 8, 0b10100111); //Send read register address (statusReg) statusReg = shift_in(12, 13, MSBPRE, 8); //Get value from register high(14); //De-select chip with CS line high //if(dataReady == 1){ print("statusReg = %b\n", statusReg); */ low(14); //CS low selects chip shift_out(12, 13, MSBFIRST, 8, 0b11101100); //Send read register address (OUT_Z_L) zL = shift_in(12, 13, MSBPRE, 8); //Get value from register high(14); //De-select chip with CS line high low(14); shift_out(12, 13, MSBFIRST, 8, 0b10101101); //Send read register address (OUT_Z_H) zH = shift_in(12, 13, MSBPRE, 8); //Get value from register high(14); z = (zL<<8 | zH); zSum += z; //print("z = %d\n", z); } zAvg = zSum/1000; print("zAvg = %d\n", zAvg); while(1){ //Main loop low(14); //CS low selects chip shift_out(12, 13, MSBFIRST, 8, 0b11101100); //Send read register address (OUT_Z_L) zL = shift_in(12, 13, MSBPRE, 8); //Get value from register high(14); //De-select chip with CS line high low(14); shift_out(12, 13, MSBFIRST, 8, 0b10101101); //Send read register address (OUT_Z_H) zH = shift_in(12, 13, MSBPRE, 8); //Get value from register high(14); z = (zL<<8 | zH)-zAvg; //2's complement operation float zf = (float) z; //Change to a float zf = zf*0.0175; //Multiply by sensitivity value for 500deg/s print("%f\n",zf); //print("zf = %f\n", zf); pause(50); //Cycle at 20Hz } }
Comments
Respectfully,
David
to
Noise is definitely less now, however, it still only gives negative values and if I turn the gyro CCW, the values immediately jump to -1000 and below. Definitely something weird is going on because I hooked the gyro up to my Arduino using the provided Arduino sketch and it worked beautifully. Can someone please help?
Thanks,
David
Many thanks,
David
The gyro seems to work properly when I rotate it in the clockwise direction (gives negative values that start around zero and become more negative). However, when I rotate it counterclockwise it seems like it "wraps around" to -1146 and starts increasing in the positive direction from there. I hope that makes sense. What could be causing this?
Thank you,
David
Tom
Thank you very much for your reply. I am certain that the sensor works fine because I tested it on an Arduino as well as on the Propeller with some code I found for I2C interfacing. My code uses SPI and I prefer to stick with that to be consistent with the accelerometer tutorial on the Parallax Learn site. I've compared my code to the I2C code and it seems my code should work but something is still not right. What do you mean by porting from Spin to C?
Thanks,
David
It's just translating what the Spin code does into what the C code would do, not necessarily a Spin operator to C operator translation.
A lot of sensors use i2c, but I pefer to use spi where possible. I made a SimpleIDE library using propeller assembly (from the Proptool library) with C. The library includes functions for generic spi as well as some specific sensors (but not the 4200). The link to that discussion is here:
https://forums.parallax.com/discussion/comment/1315065/#Comment_1315065
I have had problems when using the standard SimpleTools library spi functions. The link below shows how I used the library I made to interface with the 5607 altimiter. You can see how the spi functions are called.
https://forums.parallax.com/discussion/163486/altimeter-module-ms5607-29124-c-driver-using-spi
Hope this helps.
Tom
Also, for a sensitivity value of 500 deg/s, the datasheet says to multiply by 0.0175. In other code I see a division by 114 (1/114=.00877). Which one is correct and where does the division by 114 come from?
Thank you.
David