Академический Документы
Профессиональный Документы
Культура Документы
Math:
NXTDisplay:
SensorValue[S1] //to get the value of the touch sensor at S1
SensorRaw[S1]
//to get raw data from the sensor
Usage:
Notes:
eraseDisplay();
//4 sensor
slots in the NXT
available:
S2,//displays
S3, S4 on the
nxtDisplayString(0,
Num
= %d,S1,
num);
//8 basic
sensor
types
available:
first line,
where
num
is an integer
SensorTypes:
Notes:
sensorLightActive
//for intensity
sensorstring],
to detect
grayscaleforcolour
//nxtDisplayString([line#],
[display
[variables
sensorSONAR
//for the sonar sensor (0 to 255cm)
tags])
//When
using
the sonar,
to consider
the distance
from
//need
to specify
typeremember
being displayed
in string
by tag: %f
the sonar sensor to the front of the vehicle
sensorTouch
//for
touch
float, %i or
%dthe
int,
%ssensor
string, %c char
Colour
sensor:
//to
specify
precision use: %1.2f so that 1 is the number of
sensorCOLORFULL
//totodetect
multiple
digits
before the decimal
display
and 2 colours
is the number after.
//1-black,
2-blue,
3-green,
4-yellow,
5-red,
6-white
Timers & wait:
sensorCOLORNONE
//to
work
as
an
intensity
sensor
Usage:
//0-black,
1023-white
time1[T1] = 0; //resets timer
sensorCOLORRED,
sensorCOLORGREEN,
time1[T1] //gets time
for T1 in millisecondssensorCOLORBLUE //each one of these will only detect that specific color
time10[T2] //gets time for T2 in hundredths of a second
wait1Msec(2000);
//wait 2 seconds
Motor
Encoders:
Notes:
Usage:
//4 timers available: T1,
T2,//resets
T3, T4and initializes motor
nMotorEncoder[motorA]
= 0;
//3 functions=available:
time1[]totime10[]time100[]
motor[motorA]
20; //set power
20
//wait
wait1Msec(), wait10Msec();
while functions:
(nMotorEncoder[motorA]
< 720); //wait for 2 full rotations b/c 360 is full spin
Button
Pressed:
motor[motorA]
= 0; //stop motor
intUsage:
dist = nMotorEncoder[motorA]/360*(2*PI*radius); //radius means radius of the wheel
Notes:
while(nNxtButtonPressed == -1); //wait until a button is
//each
motor has a motor encoder
pressed
//value
Notes:of encoders represents degrees of revolution. +ve or -ve
//must
initialize before reading
//nNxtButtonPressed
is a constantly updated system
//NXT
has 3that
motors
for use.
motorA,
motorB, motorC
variable
tellsavailable
which button
is being
pressed
//Always remember to debounce this.
//NXT has 5 button states: -1 nothing pressed; 0 lower; 1
right; 2 left; 3 middle/orange
Graphics:
Point(x,y)
p.get_x(), p.get_y(), p.move(dx,dy)
Circle(p,r) ;p is Point, r is Radius
c.get_center(), c.get_radius(), c.move(dx,dy)
Moves circle c by (dx,dy)
Line(p,q) ;p,q are Points
L.get_start(); ret Point
L.get_end(); ret Point
L.move(dx, dy); moves by (dx,dy)
Message(P,s); P is point, s is string
m.get_start(); ret Point
m.get_text(); ret text
m.move(dx,dy) Moves message m by(dx,dy)
PLC
C++
Tolerance:
if (fabs(num1 num2) <
tolerance)
cout << num1 == num2;
FSTREAM:
Arrays:
void func(int array[][size]);
//the compiler needs the size of
the dimensions after the first
int arr[5] = {}; //fill arr with 0s
For Loop:
int i;
for (i = 0; i < 3; i+
+);
cout << i; //outputs
3
Fstream:
Usage:
ifstream fin(data.txt);
//input file stream type
if (!fin) //if file fails to
open.
return 0;
ofstream fout; //output
file stream type using
optional method of
declaring
fout.open(output.txt);
int read;
while (fin int
>> var;
read) {
//continue cin
until
>>end
var;of
file is readcout << var;
fout << read << endl;
//writes to aInput
line in the1.4
file
}
Outpu 1
fin.close(); //always
det
allocate resources
fout.close();
IOstream:
Usage:
cout << setprecision(4) <<
1.0028; //outputs 1.003 //4 sig
figs in total
cout << setprecision (4) << fixed
<< 1.0028; // outputs
1.0028 //4 sig figs after the
decimal
//note that fixed works
differently by itself. Alone it
forces non-scientific notation.
Special Behaviors:
bool var;
cin >> var;
cout << var;
J
0
Loops:
do { //stuff} while(//condition);
//NOTICE THE SEMICOLON
while(//condition) {//do stuff}
for(//var set to literal; //condition
to check; //action after iteration)
{//do stuff}
i.e. for (int i = 0; i < 2; i++)
4
4
Input
Output
1.4
1
J
0
-1
1
Pointers:
int * pointer = NULL, int test = 3, int * p2 = &test; //pointer to NULL //p2 to test
* pointer = new int [2]; //pointer now points to an integer array of size 2
cout << pointer; //outputs address pointed to by pointer //this is the value of the variable pointer
cout << * pointer << pointer[0]; //both examples output the first element in array
cout << *(pointer+1) << pointer[1] ; //both examples output the second element in the array
cout << &test; //outputs address of test (location of test)
delete [] pointer; //de-allocate space when done. //use [] for arrays
int * p3 = test; //DOES NOT COMPILE because int* to int
Math:
Classes:
class SuperHero { //no brackets when declaring class //capitalize names of classes
public: //accessible outside class
SuperHero () {//default constructor}
SuperHero(string name0, int xp0, int health0) {//constructor}
SuperHero(SuperHero const & src) { //copy constructor } // called when: SuperHero batman = superman; ONLY FOR
INITIALIZATION
~SuperHero() { delete [] inventoryItemIDs; } //deconstructor // this is called when object goes out of scope or program ends
SuperHero & operator=(SuperHero const & src) { // for copying object operations
if (this != &src) {
delete [] inventoryItemIDs; //deallocate previous space
inventoryItemIDs = new double [src.invItems]; //allocate a new sized space
copy(src); //this is supposed to be a copy function to copy the array of inventory items
}
return *this; // very important
}
bool readInput(ifstream & in) { //good for while loop condition in main
if(in >> name >> xp >> health)
return true;
return false;
}
void pickUpHealth(); //can do prototypes in classes and later define the function, see below
int getXp() const { return xp; } //remember to use const for accessors
void increaseXp(int xp0) { xp += xp0; } //mutator
private: //accessible inside class
int health, xp, invItems, * inventoryItemIDs; //note that this is an allowable syntax to declare pointers and non-pointers of the same type
string name;
}; //need a semicolon to end class calls
SuperHero::pickUpHealth() { //do stuff } //This is useful for declaring prototypes in a header and coding functions in a separate cpp file.
Header Files:
Header files often contain the class definitions with their data members and prototype functions. In a separate file the code can be placed;
Point.h
Point.cpp
Main.cpp
#ifndef POINT_H //by conv. use filename
#include point.h
#include point.h
#define POINT_H
Point::Point() {} //code functions etc.
int main() {
Point c;
}